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.ActivityLifeAccountEnterpriseAuthCompleteBinding
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:36 PM
 * description: 生活号企业认证提交成功页面
 */
class LifeAccountEnterpriseAuthCompleteActivity :
  BaseActivity<ActivityLifeAccountEnterpriseAuthCompleteBinding>(), View.OnClickListener, IGetLifeAccountListCallback {

  companion object {
    const val REGISTRATION_NAME = "registrationName"
    const val REGISTRATION_CODE = "registrationCode"
    const val ID_CARD_NAME = "idCardName"
    const val ID_CARD_NUM = "idCardNum"
    const val EXTRA_LIFE_ACCOUNT_ID = "life_account_id"

    // 是否审核驳回后修改认证
    const val EXTRA_IS_AUTH_MODIFY = "isAuthModify"
  }

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

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

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

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

  private fun initView() {
    // 此页面不可返回
    viewBind.bvTopBar.setBackVisibility(false)
    // 认证修改和正常认证显示不同入口文案
    viewBind.tvCreateLifeAccount.text = if (params?.get(EXTRA_IS_AUTH_MODIFY)
        ?.let { it as Boolean } == true
    ) resources.getText(R.string.enter_life_account) else resources.getText(R.string.create_life_account)
  }

  private fun initListener() {
    viewBind.tvCreateLifeAccount.setOnClickListener(this)
  }

  private fun initData() {
    viewBind.ivEnterpriseAuthInfo.fillAuthInfo(
      -1,
      -1,
      params?.get(REGISTRATION_NAME)?.let { it as String },
      params?.get(REGISTRATION_CODE)?.let { it as String },
      params?.get(ID_CARD_NAME)?.let { it as String },
      params?.get(ID_CARD_NUM)?.let { it as String }
    )
  }

  override fun onClick(v: View?) {
    if (v?.id == R.id.tv_create_life_account) {
      if (params?.get(EXTRA_IS_AUTH_MODIFY)?.let { it as Boolean } == true) {
        // 认证修改 请求生活号列表 跳转商户管理中心
        ApiService.getLifeAccountList(this)
        return
      }
      // 正常认证 跳转创建生活号页面
      XPageManager.push(
        XRouterPathConstants.LIFE_ACCOUNT_CREATE,
        hashMapOf(
          Pair(
            LifeAccountCreateActivity.LIFE_ACCOUNT_ID,
            params?.get(EXTRA_LIFE_ACCOUNT_ID)
          )
        )
      )
    }
  }

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

  override fun getLifeAccountListSuccess(result: ArrayList<LifeAccountItemBean.Response>?) {
    // 生活号列表请求成功
    val curLifeAccountId = params?.get(EXTRA_LIFE_ACCOUNT_ID)
    result?.forEach {
      if (it.life_account_id == curLifeAccountId) {
        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?) {

  }
}
