package com.yidian.shenghuoquan.newscontent.ui.center

import android.os.Bundle
import androidx.core.view.isVisible
import com.yidian.common.XEventConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.base.BaseActivity
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.constant.Constant
import com.yidian.shenghuoquan.newscontent.databinding.ActivityMerchantInfoBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.callback.IAuthMerchantCheckCallback
import com.yidian.shenghuoquan.newscontent.http.httpbean.AuthMerchantCheckBean
import com.yidian.shenghuoquan.newscontent.http.httpbean.GetLifeAccountInfoByIdBean
import com.yidian.shenghuoquan.newscontent.ui.auth.LifeAccountAuthDataManager
import com.yidian.shenghuoquan.newscontent.ui.auth.LifeAccountEnterpriseAuthActivity
import com.yidian.shenghuoquan.newscontent.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 {

  companion object {
    const val EXTRA_LIFE_ACCOUNT_INFO = "lifeAccountInfo"
    const val PARAMS_LIFE_ACCOUNT_ID = "life_account_id"
  }

  /**
   * 当前生活号信息
   */
  private val lifeAccountInfo by lazy {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { params ->
      (params as HashMap<*, *>)[EXTRA_LIFE_ACCOUNT_INFO]?.let {
        it as GetLifeAccountInfoByIdBean.Response
      }
    }
  }

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

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

  override fun getXPageName(): String {
    return XRouterPathConstants.MERCHANT_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() {
    // 生活号名称
    viewBind.ivLifeAccountName.fillContent(lifeAccountInfo?.life_account_name)
    viewBind.ivLifeAccountName.showDetailEntry()
    // 生活号认证信息
    // 审核状态
    if (lifeAccountInfo?.audit_status == Constant.STATUS_AUDIT_REJECT) {
      // 认证驳回
      viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.auth_reject))
      viewBind.ivLifeAccountAuth.showAuthEntry(MerchantInfoView.LifeAccountAuthStatus.MODIFY_AUTH)
      return
    }
    // 认证状态
    if (lifeAccountInfo?.life_account_auth_status == Constant.STATUS_AUTH_NOT && lifeAccountInfo?.enterprise_auth_record_id == null) {
      // 个人未认证生活号 无企业审核记录id
      viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.not_auth))
      viewBind.ivLifeAccountAuth.showAuthEntry(MerchantInfoView.LifeAccountAuthStatus.NOT_AUTH)
      return
    }
    // 认证类型
    when (lifeAccountInfo?.id_type) {
      Constant.TYPE_AUTH_PERSONAL -> {
        // 个人认证
        viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.personal_auth))
        viewBind.ivLifeAccountAuth.showDetailEntry()
        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))
        viewBind.ivLifeAccountAuth.showDetailEntry()
      }
      Constant.TYPE_AUTH_COMMON_ENTERPRISE -> {
        // 普通企业认证
        viewBind.ivLifeAccountAuth.fillContent(resources.getString(R.string.common_enterprise))
        viewBind.ivLifeAccountAuth.showDetailEntry()
      }
    }
  }

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

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

    }
  }

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

  override fun authMerchantCheckFailure(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)
      }
    }
  }

}
