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

import android.app.Activity
import android.os.Bundle
import com.yidian.common.XRouterPathConstants
import com.yidian.shenghuoquan.newscontent.constant.Constant
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.callback.IAuthMerchantCheckCallback
import com.yidian.shenghuoquan.newscontent.http.callback.IGetLifeAccountInfoByIdCallback
import com.yidian.shenghuoquan.newscontent.http.httpbean.AuthMerchantCheckBean
import com.yidian.shenghuoquan.newscontent.http.httpbean.GetLifeAccountInfoByIdBean
import com.yidian.shenghuoquan.newscontent.ui.auth.LifeAccountAuthActivity
import com.yidian.shenghuoquan.newscontent.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))
        )
        XPageManager.pop(null)
      }
      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.MERCHANT_AUTH_MANAGEMENT
  }

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

  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)
      )
    )
    XPageManager.pop(null)
  }

  override fun authMerchantCheckFailure(message: String?) {

  }
}
