package com.yidian.shenghuoquan.newscontent.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.core.os.bundleOf
import com.google.gson.Gson
import com.yidian.common.XEventConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.base.BaseFragment
import com.yidian.common.http.HttpResult
import com.yidian.shenghuoquan.newscontent.bean.ContentDetailBean
import com.yidian.shenghuoquan.newscontent.databinding.FragmentWebBinding
import com.yidian.xarc.xbrid.XBridManager
import com.yidian.xarc.xbrid.XWebView
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.xarc.xevent.XEventManager
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.json.JSONObject

class CommentWebFragment : BaseFragment<FragmentWebBinding>() {

    companion object {
        fun newInstance(paramsMap: Map<*, *>): CommentWebFragment {
            return CommentWebFragment().apply {
                arguments = bundleOf(Pair(XRouterPathConstants.ParamsKey, paramsMap))
            }
        }
    }

    private lateinit var webView: XWebView
    private var loadDataFinish = false
    private var loadWebViewFinish = false
    private var data: HttpResult<Any?>? = null
    private var isDestroy = false

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentWebBinding {
        return FragmentWebBinding.inflate(inflater, container, false)
    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        val paramsMap = arguments?.getSerializable(XRouterPathConstants.ParamsKey) as Map<*, *>
        initData()
        initWebView()
        setClickListener()
    }
    private fun initData() {

    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebView() {
        webView = XBridManager.getWebView(requireContext(), "testidenfier", viewBinding.commentWeb)
        webView.loadUrl("xbrid_article")
        webView.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                if (newProgress == 100) {
                    loadWebViewFinish = true
                    postDataToWeb()
                }
            }
        }
        WebView.setWebContentsDebuggingEnabled(true)
    }

    private fun postDataToWeb() {
        if (loadDataFinish && loadWebViewFinish) {
            val contentDetailBean = ContentDetailBean("getArticleData", data)
            val params = Gson().toJson(contentDetailBean)
            XBridManager.postEvent("ArticleService", JSONObject(params), webView)
        }
    }

    private fun setClickListener() {

    }

    override fun onDestroy() {
        super.onDestroy()
        isDestroy = true
        if (::webView.isInitialized) {
            XBridManager.backWebView(webView)
        }
        XEventManager.unRegister(this)
    }


    @Subscribe(sticky = false, threadMode = ThreadMode.MAIN)
    fun onEvent(event: XBaseEvent?) {
        when (event?.name) {
            XEventConfig.SCROLL_SETTING_BAR -> {
                val isShow = event.body as Boolean
            }
        }
    }


}