package com.yidian.shenghuoquan.bmanagercenter.ui.auth

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.constant.IntentConstants
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.sdk.ClientType
import com.yidian.bcommon.sdk.JudgeClientUtils
import com.yidian.bcommon.services.ZapServiceActionConstants
import com.yidian.bcommon.services.ZapServiceNameConstants
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityLifeAccountAuthBinding
import com.yidian.shenghuoquan.bmanagercenter.http.ApiService
import com.yidian.shenghuoquan.bmanagercenter.http.callback.IAuthAuthenticationCallback
import com.yidian.shenghuoquan.bmanagercenter.http.callback.IAuthMerchantCheckCallback
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.AuthAuthenticationBean
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.AuthMerchantCheckBean
import com.yidian.xpage.XPageManager
import com.yidian.yac.core.zap.ZapTicket

/**
 * author: yinjiacheng
 * date: 5/20/21 6:26 PM
 * description: 生活号认证
 */
class LifeAccountAuthActivity : BaseActivity<ActivityLifeAccountAuthBinding>(), View.OnClickListener, IAuthAuthenticationCallback,
  IAuthMerchantCheckCallback {

  companion object {
    // 由B端管理中心跳转
    const val FROM_B_MANAGER_CENTER = "fromBManagerCenter"

    // 生活号id
    const val PARAMS_LIFE_ACCOUNT_ID = "life_account_id"
  }

  /**
   * 生活号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
      }
    }
  }

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

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

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initView()
    initListener()
  }

  private fun initView() {
    // 此页面默认不可返回
    viewBind.bvTopBar.setBackVisibility(false)
    viewBind.viewPersonalAuth.setData(
      resources.getString(R.string.personal_auth),
      resources.getString(R.string.personal_auth_condition),
      resources.getString(R.string.personal_auth_description), false
    )
    viewBind.viewEnterpriseAuth.setData(
      resources.getString(R.string.enterprise_auth),
      resources.getString(R.string.enterprise_auth_condition),
      resources.getString(R.string.enterprise_auth_description),
      false
    )
    val from = intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { params ->
      (params as HashMap<*, *>)[IntentConstants.KEY_EXTRA_LIFE_ACCOUNT_AUTH_FROM]?.let {
        it as String
      }
    }
    if (from == FROM_B_MANAGER_CENTER || JudgeClientUtils.judgeClient(packageName) == ClientType.MERCHANT_C) {
      // 由B端管理中心及相关页面或C端跳转至此页面 页面可返回且隐藏退出登录按钮
      viewBind.bvTopBar.setBackVisibility(true)
      viewBind.btnLogout.isVisible = false
    }
  }

  private fun initListener() {
    viewBind.tvJoinStrategy.setOnClickListener(this)
    viewBind.viewPersonalAuth.setOnClickListener(this)
    viewBind.viewEnterpriseAuth.setOnClickListener(this)
    viewBind.btnLogout.setOnClickListener(this)
  }

  override fun onClick(v: View?) {
    when (v?.id) {
      R.id.view_personal_auth -> {
        // 请求个人认证信息 用于回显已填入的信息
        ApiService.authAuthentication(this)
      }
      R.id.view_enterprise_auth -> {
        // 请求企业认证信息 用于回显已填入的信息
        ApiService.authMerchantCheck(this, lifeAccountId?.let { hashMapOf(Pair(PARAMS_LIFE_ACCOUNT_ID, it)) } ?: hashMapOf())
      }
      R.id.btn_logout -> {
        // 退出登录
        requestLogout()
      }
    }
  }

  private fun requestLogout() {
    ZapTicket(ZapServiceNameConstants.SettingService).withAction(ZapServiceActionConstants.ActionLogout)
      .onResult { result ->
        if (result.result as Boolean) {
          // 退出登录成功 跳转登录页面
          XPageManager.push(XRouterPathConstants.LOGIN_LIFE_CIRCLE, null)
        }
      }.ship()
  }

  override fun authAuthenticationSuccess(result: AuthAuthenticationBean.Response?) {
    viewBind.viewPersonalAuth.isSelected = true
    viewBind.viewEnterpriseAuth.isSelected = false
    // 跳转个人认证页
    XPageManager.push(
      XRouterPathConstants.LIFE_ACCOUNT_PERSONAL_AUTH,
      hashMapOf(
        Pair(LifeAccountPersonalAuthActivity.EXTRA_AUTH_DATA, result),
        Pair(LifeAccountPersonalAuthActivity.EXTRA_LIFE_ACCOUNT_ID, lifeAccountId)
      )
    )
  }

  override fun authAuthenticationFailure(message: String?) {

  }

  override fun authMerchantCheckSuccess(result: AuthMerchantCheckBean.Response?) {
    viewBind.viewPersonalAuth.isSelected = false
    viewBind.viewEnterpriseAuth.isSelected = true
    // 跳转企业认证页
    XPageManager.push(
      XRouterPathConstants.LIFE_ACCOUNT_ENTERPRISE_AUTH,
      hashMapOf(
        Pair(LifeAccountEnterpriseAuthActivity.EXTRA_AUTH_DATA, result),
        Pair(LifeAccountEnterpriseAuthActivity.EXTRA_LIFE_ACCOUNT_ID, lifeAccountId)
      )
    )
  }

  override fun authMerchantCheckFailure(message: String?) {

  }

}
