package com.yidian.shenghuoquan.newscontent.ui

import android.os.Bundle
import com.yidian.common.XRouterPathConstants
import com.yidian.common.XRouterPathConstants.Companion.PROTOCOL
import com.yidian.common.base.BaseActivity
import com.yidian.common.extensions.initTitleBar
import com.yidian.common.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"
  }

}
