package com.yidian.shenghuoquan.newscontent.ui

import android.os.Bundle
import com.yidian.bcommon.XRouterPathConstants
import com.yidian.bcommon.XRouterPathConstants.Companion.PROTOCOL
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.extensions.initTitleBar
import com.yidian.bcommon.utils.ToastUtils
import com.yidian.shenghuoquan.newscontent.databinding.ActivityProtocolBinding
import com.yidian.xarc.xbrid.XBridManager
import com.yidian.xarc.xbrid.XWebView

/**
 * 用户协议 & 隐私政策
 */
class ProtocolActivity : BaseActivity<ActivityProtocolBinding>() {

  private lateinit var webView: XWebView

  override fun createViewBinding(): ActivityProtocolBinding {
    return ActivityProtocolBinding.inflate(layoutInflater)
  }

  override fun getXPageName(): String {
    return PROTOCOL
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    val serializableExtra = intent.getSerializableExtra(XRouterPathConstants.ParamsKey)
    serializableExtra?.let {
      val paramsMap = serializableExtra as HashMap<*, *>
      val protocolTitle = paramsMap[ProtocolTitleKey] as String?
      val protocolUrl = paramsMap[ProtocolUrlKey] as String?
      initTitle(protocolTitle)
      initWebView(protocolUrl)
    }
  }

  private fun initTitle(protocolTitle: String?) {
    initTitleBar(viewBind.include.toolbar, viewBind.include.tvTitle, protocolTitle ?: "协议")
  }

  private fun initWebView(protocolUrl: String?) {
    if (protocolUrl.isNullOrBlank()) {
      ToastUtils.showShortSafe("链接地址为空")
      return
    }
    webView = XBridManager.getWebView(this, "testidenfier", viewBind.wvProtocol)
    webView.loadUrl(protocolUrl)
  }

  companion object {
    const val ProtocolTitleKey = "protocol_title"
    const val ProtocolUrlKey = "protocol_url"
  }

}
