package com.yidian.shenghuoquan.bmanagercenter.ui.auth

import android.os.Bundle
import android.view.View
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.base.BaseActivity
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityLifeAccountPersonalAuthCompleteBinding
import com.yidian.shenghuoquan.bmanagercenter.http.ApiService
import com.yidian.shenghuoquan.bmanagercenter.http.callback.IGetLifeAccountListCallback
import com.yidian.bcommon.bean.LifeAccountItemBean
import com.yidian.shenghuoquan.bmanagercenter.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
  }
}
