package com.yidian.shenghuoquan.bmanagercenter.ui.center

import android.os.Bundle
import androidx.core.view.isVisible
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.constant.IntentConstants
import com.yidian.bcommon.constant.XEventConfig
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.utils.StorageUtil
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.constant.Constant
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityMerchantInfoBinding
import com.yidian.shenghuoquan.bmanagercenter.http.ApiService
import com.yidian.shenghuoquan.bmanagercenter.http.callback.IAuthMerchantCheckCallback
import com.yidian.shenghuoquan.bmanagercenter.http.callback.IGetLifeAccountInfoByIdCallback
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.AuthMerchantCheckBean
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.GetLifeAccountInfoByIdBean
import com.yidian.shenghuoquan.bmanagercenter.ui.auth.LifeAccountAuthActivity
import com.yidian.shenghuoquan.bmanagercenter.ui.auth.LifeAccountEnterpriseAuthActivity
import com.yidian.shenghuoquan.bmanagercenter.widget.MerchantInfoView
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.xarc.xevent.XEventManager
import com.yidian.xpage.XPageManager
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * author: yinjiacheng
 * date: 6/11/21 12:22 PM
 * description: 商户基本信息
 */
class MerchantInfoActivity : BaseActivity<ActivityMerchantInfoBinding>(), MerchantInfoView.OnMerchantInfoViewListener,
  IAuthMerchantCheckCallback, IGetLifeAccountInfoByIdCallback {

  companion object {
    const val PARAMS_LIFE_ACCOUNT_ID = "life_account_id"
  }

  /**
   * 当前生活号详情
   */
  private lateinit var lifeAccountInfo: GetLifeAccountInfoByIdBean.Response

  /**
   * 外部传入生活号id
   */
  private val lifeAccountId by lazy {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { params ->
      (params as HashMap<*, *>)[IntentConstants.KEY_EXTRA_LIFE_ACCOUNT_ID]?.let {
        it as String
      }
    }
  }

  /**
   * 修改后的生活号名称
   */
  private var modifyAccountName: String? = null

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

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

  override fun onDestroy() {
    super.onDestroy()
    XEventManager.unRegister(this)
  }

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

  private fun initView() {

  }

  private fun initListener() {
    viewBind.ivLifeAccountName.setOnMerchantInfoViewListener(this)
    viewBind.ivLifeAccountAuth.setOnMerchantInfoViewListener(this)
    viewBind.ivLifeAccountAuthUpgrade.setOnMerchantInfoViewListener(this)
  }

  private fun initData() {
    // 请求生活号详情接口
    ApiService.getLifeAccountInfoById(
      this,
      hashMapOf(PARAMS_LIFE_ACCOUNT_ID to (lifeAccountId ?: StorageUtil.getLifeAccountId()))
    )
  }

  override fun onEnterDetail(from: MerchantInfoView) {
    // 进入生活号名称修改页或认证信息详情
    if (from == viewBind.ivLifeAccountName) {
      // 跳转生活号名称修改页
      XPageManager.push(
        XRouterPathConstants.LIFE_ACCOUNT_MODIFY_NAME,
        hashMapOf(
          LifeAccountModifyNameActivity.EXTRA_LIFE_ACCOUNT_ID to lifeAccountInfo.life_account_id,
          LifeAccountModifyNameActivity.EXTRA_LIFE_ACCOUNT_NAME to (modifyAccountName ?: lifeAccountInfo.life_account_name)
        )
      )
    } else if (from == viewBind.ivLifeAccountAuth) {
      // 跳转认证详情页
      XPageManager.push(
        XRouterPathConstants.LIFE_ACCOUNT_AUTH_INFO,
        hashMapOf(MerchantAuthInfoActivity.EXTRA_LIFE_ACCOUNT_INFO to lifeAccountInfo)
      )
    }
  }

  override fun onEnterAuth(from: MerchantInfoView) {
    // 认证或认证升级或修改认证
    if (from == viewBind.ivLifeAccountAuth) {
      if (lifeAccountInfo.life_account_type == Constant.TYPE_LIFE_ACCOUNT_PERSONAL
        && lifeAccountInfo.life_account_auth_status == Constant.STATUS_AUTH_NOT
        && lifeAccountInfo.audit_status == 0
      ) {
        // 未认证 进入认证流程首页 此处需要传参from控制认证流程首页返回按钮显示
        XPageManager.push(
          XRouterPathConstants.LIFE_ACCOUNT_AUTH,
          hashMapOf(
            IntentConstants.KEY_EXTRA_LIFE_ACCOUNT_AUTH_FROM to LifeAccountAuthActivity.FROM_B_MANAGER_CENTER,
            IntentConstants.KEY_EXTRA_LIFE_ACCOUNT_ID to lifeAccountInfo.life_account_id
          )
        )
      } else if (lifeAccountInfo.audit_status == Constant.STATUS_AUDIT_REJECT) {
        // 认证驳回 请求企业认证信息 此处为升级认证情况 需要传入生活号id
        ApiService.authMerchantCheck(
          this,
          hashMapOf(PARAMS_LIFE_ACCOUNT_ID to lifeAccountInfo.life_account_id)
        )
      }
    } else if (from == viewBind.ivLifeAccountAuthUpgrade) {
      // 认证升级 请求企业认证信息 此处为升级认证情况 需要传入生活号id
      ApiService.authMerchantCheck(
        this, hashMapOf(PARAMS_LIFE_ACCOUNT_ID to lifeAccountInfo.life_account_id)
      )

    }
  }

  override fun authMerchantCheckSuccess(result: AuthMerchantCheckBean.Response?) {
    // 跳转企业认证流程
    XPageManager.push(
      XRouterPathConstants.LIFE_ACCOUNT_ENTERPRISE_AUTH,
      hashMapOf(
        LifeAccountEnterpriseAuthActivity.EXTRA_LIFE_ACCOUNT_ID to lifeAccountInfo.life_account_id,
        LifeAccountEnterpriseAuthActivity.EXTRA_AUTH_DATA to result,
        LifeAccountEnterpriseAuthActivity.EXTRA_IS_AUTH_MODIFY to (lifeAccountInfo.audit_status == Constant.STATUS_AUDIT_REJECT)
      )
    )
  }

  override fun authMerchantCheckFailure(message: String?) {

  }

  override fun getLifeAccountInfoByIdSuccess(result: GetLifeAccountInfoByIdBean.Response?) {
    result?.let {
      lifeAccountInfo = it
      // 更新UI
      // 生活号名称
      viewBind.ivLifeAccountName.fillContent(lifeAccountInfo.life_account_name)
      if (lifeAccountInfo.audit_status != Constant.STATUS_AUDIT_UNDER && lifeAccountInfo.role_type != Constant.TYPE_ROLE_ORDINARY) {
        // 审核中和企业员工不允许修改生活号名称
        viewBind.ivLifeAccountName.showDetailEntry()
      }
      // 生活号认证信息
      // 审核状态
      if (lifeAccountInfo.audit_status == Constant.STATUS_AUDIT_REJECT) {
        // 认证驳回
        viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.auth_reject))
        if (lifeAccountInfo.role_type != Constant.TYPE_ROLE_ORDINARY) {
          // 企业员工不允许修改认证信息
          viewBind.ivLifeAccountAuth.showAuthEntry(MerchantInfoView.LifeAccountAuthStatus.MODIFY_AUTH)
        }
        return
      }
      // 认证状态
      if (lifeAccountInfo.life_account_auth_status == Constant.STATUS_AUTH_NOT && lifeAccountInfo.enterprise_auth_record_id == 0) {
        // 个人未认证生活号 企业审核记录id为0
        viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.not_auth))
        viewBind.ivLifeAccountAuth.showAuthEntry(MerchantInfoView.LifeAccountAuthStatus.NOT_AUTH)
        return
      }
      if (lifeAccountInfo.role_type != Constant.TYPE_ROLE_ORDINARY) {
        // 企业员工不允许查看认证信息
        viewBind.ivLifeAccountAuth.showDetailEntry()
      }
      // 认证类型
      when (lifeAccountInfo.id_type) {
        Constant.TYPE_AUTH_PERSONAL -> {
          // 个人认证
          viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.personal_auth))
          viewBind.ivLifeAccountAuthUpgrade.isVisible = true
          viewBind.ivLifeAccountAuthUpgrade.showAuthEntry(MerchantInfoView.LifeAccountAuthStatus.UPGRADE_AUTH)
        }
        Constant.TYPE_AUTH_INDIVIDUAL_BUSINESS -> {
          // 个体工商户认证
          viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.individual_business))
        }
        Constant.TYPE_AUTH_COMMON_ENTERPRISE -> {
          // 普通企业认证
          viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.common_enterprise))
        }
      }
    }
  }

  override fun getLifeAccountInfoByIdFailure(message: String?) {

  }

  @Subscribe(sticky = false, threadMode = ThreadMode.MAIN)
  fun onEvent(event: XBaseEvent?) {
    when (event?.name) {
      XEventConfig.LIFE_ACCOUNT_NAME_MODIFY -> {
        // 更新生活号名称
        val accountName = event.body as String
        modifyAccountName = accountName
        viewBind.ivLifeAccountName.fillContent(accountName)
      }
    }
  }

}
