package com.yidian.shenghuoquan.xbirdbusiness.ui

import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.activity.viewModels
import com.yidian.common.XBirdPageConstants
import com.yidian.common.XRouterPathConstants
import com.yidian.common.extensions.initTitleBar
import com.yidian.common.mvvm.BaseMvvmActivity
import com.yidian.common.utils.DialogUtils
import com.yidian.common.utils.ToastUtils
import com.yidian.shenghuoquan.xbirdbusiness.R
import com.yidian.shenghuoquan.xbirdbusiness.databinding.XbridActivityCommonWebViewBinding
import com.yidian.xarc.xbrid.XBridManager
import com.yidian.xarc.xbrid.XWebView
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 -> {
        DialogUtils.showConfirmDialog(this, "返回后商品将不保存？")
      }
      else -> {
        super.onBackPressed()
      }
    }
  }

}
