package com.yidian.shenghuoquan.bxbirdbusiness.ui

import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.activity.viewModels
import com.yidian.bcommon.constant.XBirdPageConstants
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.extensions.initTitleBar
import com.yidian.bcommon.mvvm.BaseMvvmActivity
import com.yidian.bcommon.utils.ToastUtils
import com.yidian.bcommon.widget.CommonHintDialog
import com.yidian.shenghuoquan.bxbirdbusiness.R
import com.yidian.shenghuoquan.bxbirdbusiness.databinding.XbridActivityCommonWebViewBinding
import com.yidian.xarc.xbrid.XBridManager
import com.yidian.xarc.xbrid.XWebView
import com.yidian.xpage.XPageManager
import com.yidian.xpage.XPageViewProtocol
import timber.log.Timber


/**
 * 通用的WebView
 */
class CommonWebViewActivity : BaseMvvmActivity<XbridActivityCommonWebViewBinding, CommonWebViewModel>(), XPageViewProtocol {

  private lateinit var webView: XWebView
  private lateinit var url: String

  override val layoutId: Int = R.layout.xbrid_activity_common_web_view

  override val vm: CommonWebViewModel by viewModels()

  override fun getXPageName(): String {
    return XRouterPathConstants.COMMON_WEB_VIEW_ACTIVITY
  }

  override fun initView() {
    val serializableExtra = intent.getSerializableExtra(XRouterPathConstants.ParamsKey)
    if (serializableExtra == null) {
      // 提示页面必须传参才能进入
      ToastUtils.showShortSafe("需要传递pageUrl")
    } else {
      val paramsMap = serializableExtra as HashMap<*, *>
      val webViewUrl = paramsMap[XBirdPageConstants.PAGE_URL] as String?
      if (webViewUrl.isNullOrBlank()) {
        ToastUtils.showShortSafe("pageUrl不能为空")
        return
      }
      url = webViewUrl
      Timber.e("页面的URL ： $webViewUrl")
      // 判断目的页面，getXBridPageName 仅支持config文件中有的url，不支持ip+端口
      when (XBridManager.getXBridPageName(webViewUrl)) {
        XBirdPageConstants.COMMODITY_PUBLISH_COMMODITY -> initPublishCommodityWebView(webViewUrl)
        else -> {
          ToastUtils.showShortSafe("请传递pageUrl")
        }
      }
    }
  }

  /**
   * 初始化发布商品页面
   */
  private fun initPublishCommodityWebView(webViewUrl: String) {
    webView = XBridManager.getWebView(this, "testidenfier", binding.commonWebView)
    webView.loadUrl(webViewUrl)
    Timber.tag("CommonWebView").e(" 请求地址： $webViewUrl")
    webView.webChromeClient = object : WebChromeClient() {

      override fun onReceivedTitle(view: WebView?, title: String?) {

        super.onReceivedTitle(view, title)
        if (!title.isNullOrBlank()) {
          Timber.tag("CommonWebView").e(" 页面title： $title")
          initTitleBar(binding.include.toolbar, binding.include.tvTitle, title)
        }
      }

      override fun onProgressChanged(view: WebView?, newProgress: Int) {
        super.onProgressChanged(view, newProgress)
        if (newProgress == 100) {
          Timber.tag("CommonWebView").e(" CommonWebView 页面加载完成")
        }
      }
    }
    WebView.setWebContentsDebuggingEnabled(true)
  }

  override fun onDestroy() {
    super.onDestroy()
    if (::webView.isInitialized) {
      XBridManager.backWebView(webView)
    }
  }

  override fun onBackPressed() {
    when (XBridManager.getXBridPageName(url)) {
      XBirdPageConstants.COMMODITY_PUBLISH_COMMODITY -> {
        CommonHintDialog(this, "返回后，商品信息不会保存", arrayListOf("取消", "返回"), object : CommonHintDialog.OnHintOptionCallback {
          override fun onLeftOption() {

          }

          override fun onRightOption() {
            XPageManager.pop(null)
          }

        }, R.color.color_8F000000, R.color.color_FF3A3A).show()
      }
      else -> {
        super.onBackPressed()
      }
    }
  }

}
