package es.verdnatura.presentation.view.feature.controlador.fragment


import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.print.PrintAttributes
import android.print.PrintManager
import android.util.Log
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentWebBinding
import es.verdnatura.domain.ConstAndValues
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnOptionsSelectedListener
import es.verdnatura.presentation.common.ToolBarAdapterTooltip
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import org.json.JSONObject


@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class WebFragment(
    var entryPoint: String = ""
) : BaseFragment<FragmentWebBinding, ControladorViewModel>(ControladorViewModel::class) {

    override fun getLayoutId(): Int = R.layout.fragment_web
    private lateinit var customDialog: CustomDialog

    companion object {
        fun newInstance(entryPoint: String) = WebFragment(entryPoint)
    }

    override fun init() {


        customDialog = CustomDialog(requireContext())
        setToolbar()
        setWeb()
        super.init()
    }

    private fun setWeb() {

        binding.webView.setWebChromeClient (WebChromeClient())

        //binding.webView.getSettings (). setJavaScriptEnabled (true)
       // sergio:deprecated
        //webView.getSettings (). setPluginState (WebSettings.PluginState.ON)
        val webSettings = binding.webView.settings
        webSettings.javaScriptEnabled = true
        webSettings.domStorageEnabled = true
        webSettings.loadWithOverviewMode = true
        webSettings.useWideViewPort = true
        webSettings.builtInZoomControls = true
        webSettings.displayZoomControls = false
        webSettings.setSupportZoom(true)
        //webSettings.defaultTextEncodingName = "utf-8"

        var dataScanned = JSONObject(entryPoint)
        binding.webView.loadUrl(dataScanned.get("web").toString())


    }

    private fun setToolbar() {

        binding.mainToolbar.toolbarTitle.text = getString(R.string.item)+JSONObject(entryPoint).get("entryPoint").toString()

        val listIcons: ArrayList<ImageView> = ArrayList()
        val iconPrint = ImageView(context)
        iconPrint.setImageResource(R.drawable.ic_print_black_24dp)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            iconPrint.tooltipText = getTooltip(R.drawable.ic_print_black_24dp)

        }

        //listIcons.add(iconPrint)


        binding.mainToolbar.toolbarIcons.adapter =
            ToolBarAdapterTooltip(listIcons, object : OnOptionsSelectedListener {
                override fun onOptionsItemSelected(item: Drawable) {

                    when (item) {
                        iconPrint.drawable -> createWebPrintJob(binding.webView)
                    }

                }
            })
        binding.mainToolbar.toolbarIcons.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)


    }

    private fun createWebPrintJob(webView: WebView) {


            // Get a PrintManager instance
            (activity?.getSystemService(Context.PRINT_SERVICE) as? PrintManager)?.let { printManager ->

                val jobName = "${getString(R.string.app_name)} Document"

                // Get a print adapter instance
                val printAdapter = webView.createPrintDocumentAdapter(jobName)

                // Create a print job with name and adapter instance
                printManager.print(
                    jobName,
                    printAdapter,
                    PrintAttributes.Builder().build()
                ).also { printJob ->

                    // Save the job object for later status checking
                   // printJobs += printJob
                }
            }
        }


}

