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

import android.app.Activity
import android.os.Bundle
import com.yidian.bcommon.constant.IntentConstants
import com.yidian.bcommon.constant.XRouterPathConstants
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.manager.LifeAccountManager
import com.yidian.shenghuoquan.bmanagercenter.ui.auth.LifeAccountAuthActivity
import com.yidian.shenghuoquan.bmanagercenter.ui.auth.LifeAccountEnterpriseAuthActivity
import com.yidian.xpage.XPageManager
import com.yidian.xpage.XPageViewProtocol

/**
 * author: yinjiacheng
 * date: 7/13/21 7:45 PM
 * description: 商户认证管理
 */
class MerchantAuthManagementActivity : Activity(), XPageViewProtocol, IGetLifeAccountInfoByIdCallback, IAuthMerchantCheckCallback {

  companion object {
    const val PARAMS_LIFE_ACCOUNT_ID = "life_account_id"
  }

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

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // 请求生活号认证信息
    ApiService.getLifeAccountInfoById(this, hashMapOf(PARAMS_LIFE_ACCOUNT_ID to lifeAccountId))
  }

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

  override fun getLifeAccountInfoByIdSuccess(result: GetLifeAccountInfoByIdBean.Response?) {
    when (LifeAccountManager.getLifeAccountTag(result?.tag?.tag_key)) {
      LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_NOT_AUTH -> {
        // 未认证跳转认证首页
        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 lifeAccountId
          )
        )
        finish()
      }
      LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_AUTH_REJECT -> {
        // 认证驳回 请求企业认证信息
        ApiService.authMerchantCheck(this, hashMapOf(PARAMS_LIFE_ACCOUNT_ID to lifeAccountId))
      }
      else -> {
        // 跳转认证信息详情页
        XPageManager.push(
          XRouterPathConstants.LIFE_ACCOUNT_AUTH_INFO,
          hashMapOf(MerchantAuthInfoActivity.EXTRA_LIFE_ACCOUNT_INFO to result)
        )
        finish()
      }
    }
  }

  override fun getLifeAccountInfoByIdFailure(message: String?) {

  }

  override fun authMerchantCheckSuccess(result: AuthMerchantCheckBean.Response?) {
    // 跳转企业认证流程
    XPageManager.push(
      XRouterPathConstants.LIFE_ACCOUNT_ENTERPRISE_AUTH,
      hashMapOf(
        LifeAccountEnterpriseAuthActivity.EXTRA_LIFE_ACCOUNT_ID to lifeAccountId,
        LifeAccountEnterpriseAuthActivity.EXTRA_AUTH_DATA to result,
        LifeAccountEnterpriseAuthActivity.EXTRA_IS_AUTH_MODIFY to true
      )
    )
    finish()
  }

  override fun authMerchantCheckFailure(message: String?) {

  }
}
