package com.yidian.shenghuoquan.newscontent.ui.auth

import android.os.Bundle
import android.view.View
import com.yidian.common.XRouterPathConstants
import com.yidian.common.base.BaseActivity
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.databinding.ActivityLifeAccountPersonalAuthCompleteBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.callback.IGetLifeAccountListCallback
import com.yidian.shenghuoquan.newscontent.http.httpbean.LifeAccountItemBean
import com.yidian.shenghuoquan.newscontent.ui.center.MerchantCenterActivity
import com.yidian.xpage.XPageManager

/**
 * author: yinjiacheng
 * date: 6/16/21 11:04 PM
 * description: 生活号个人认证成功页面
 */
class LifeAccountPersonalAuthCompleteActivity :
  BaseActivity<ActivityLifeAccountPersonalAuthCompleteBinding>(), IGetLifeAccountListCallback, View.OnClickListener {

  companion object {
    const val LIFE_ACCOUNT_NAME = "lifeAccountName"
    const val OCCUPATION = "occupation"
    const val REAL_NAME = "realName"
    const val ID_CARD_NUM = "idCardNum"
    const val PHONE_NUM = "phoneNum"
    const val EXTRA_LIFE_ACCOUNT_ID = "life_account_id" // 个人生活号升级用
  }

  /**
   * 由个人认证页传递的参数
   */
  private val params by lazy {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let {
      it as HashMap<*, *>
    }
  }

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

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

  override fun init(savedInstanceState: Bundle?) {
    initView()
    initListener()
    initData()
  }

  private fun initView() {
    // 此页面不可返回
    viewBind.bvTopBar.setBackVisibility(false)
  }

  private fun initData() {
    viewBind.ivPersonAuthInfo.fillAuthInfo(
      params?.get(LIFE_ACCOUNT_NAME)?.let { it as String },
      params?.get(OCCUPATION)?.let { it as String },
      params?.get(REAL_NAME)?.let { it as String },
      params?.get(ID_CARD_NUM)?.let { it as String },
      params?.get(PHONE_NUM)?.let { it as String }
    )
  }

  private fun initListener() {
    viewBind.tvEnterMerchantCenter.setOnClickListener(this)
    viewBind.tvUpgradeEnterpriseAuth.setOnClickListener(this)
  }

  override fun getLifeAccountListSuccess(result: ArrayList<LifeAccountItemBean.Response>?) {
    // 生活号列表请求成功 跳转商户管理中心
    result?.forEach {
      if (it.life_account_id == params?.get(EXTRA_LIFE_ACCOUNT_ID)) {
        XPageManager.push(
          XRouterPathConstants.MERCHANT_CENTER,
          hashMapOf(
            Pair(MerchantCenterActivity.EXTRA_IS_MULTI, result.size > 1),
            Pair(MerchantCenterActivity.EXTRA_LIFE_ACCOUNT_INFO, it)
          )
        )
        XPageManager.pop(null)
      }
    }
  }

  override fun getLifeAccountListFailure(message: String?) {

  }

  override fun onClick(v: View?) {
    if (v?.id == R.id.tv_enter_merchant_center) {
      // 跳转商户管理中心 先请求生活号列表 带参跳转
      ApiService.getLifeAccountList(this)
    } else if (v?.id == R.id.tv_upgrade_enterprise_auth) {
      // 传入生活号id 请求企业认证升级相关接口用
      XPageManager.push(
        XRouterPathConstants.LIFE_ACCOUNT_ENTERPRISE_AUTH,
        hashMapOf(
          Pair(
            LifeAccountEnterpriseAuthActivity.EXTRA_LIFE_ACCOUNT_ID,
            params?.get(EXTRA_LIFE_ACCOUNT_ID)
          )
        )
      )
    }
  }

  override fun onBackPressed() {
    // 禁止back event
  }
}
