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

import android.app.Activity
import android.os.Bundle
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.shenghuoquan.bmanagercenter.constant.Constant
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.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 EXTRA_LIFE_ACCOUNT_ID = "life_account_id"
    const val EXTRA_LIFE_ACCOUNT_TAG_KEY = "life_account_tag_key"
  }

  /**
   * 生活号id
   */
  private val lifeAccountId by lazy {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { serializable ->
      (serializable as HashMap<*, *>)[EXTRA_LIFE_ACCOUNT_ID]?.let {
        it as String
      }
    }
  }

  /**
   * 生活号tag key
   */
  private val lifeAccountTagKey by lazy {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { serializable ->
      (serializable as HashMap<*, *>)[EXTRA_LIFE_ACCOUNT_TAG_KEY]?.let {
        it as Int
      }
    }
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    when (lifeAccountTagKey) {
      Constant.LIFE_ACCOUNT_TAG_NOT_AUTH -> {
        // 未认证跳转认证首页
        XPageManager.push(
          XRouterPathConstants.LIFE_ACCOUNT_AUTH,
          hashMapOf(
            Pair(LifeAccountAuthActivity.EXTRA_PAGE_FROM, LifeAccountAuthActivity.FROM_AUTH),
            Pair(LifeAccountAuthActivity.EXTRA_LIFE_ACCOUNT_ID, lifeAccountId)
          )
        )
        finish()
      }
      Constant.LIFE_ACCOUNT_TAG_AUTH_REJECT -> {
        // 认证驳回 请求企业认证信息
        ApiService.authMerchantCheck(this, hashMapOf(Pair(EXTRA_LIFE_ACCOUNT_ID, lifeAccountId)))
      }
      else -> {
        // 请求生活号认证信息
        ApiService.getLifeAccountInfoById(this, hashMapOf(Pair(EXTRA_LIFE_ACCOUNT_ID, lifeAccountId)))
      }
    }

  }

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

  override fun getLifeAccountInfoByIdSuccess(result: GetLifeAccountInfoByIdBean.Response?) {
    // 跳转认证信息详情页
    XPageManager.push(XRouterPathConstants.LIFE_ACCOUNT_AUTH_INFO, hashMapOf(Pair(MerchantAuthInfoActivity.EXTRA_LIFE_ACCOUNT_INFO, result)))
    finish()
  }

  override fun getLifeAccountInfoByIdFailure(message: String?) {

  }

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

  override fun authMerchantCheckFailure(message: String?) {

  }
}
