package com.yidian.shenghuoquan.newscontent.ui.auth

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import com.yidian.bcommon.XRouterPathConstants
import com.yidian.bcommon.base.BaseActivity
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.databinding.ActivityLifeAccountAuthBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.callback.IAuthAuthenticationCallback
import com.yidian.shenghuoquan.newscontent.http.callback.IAuthMerchantCheckCallback
import com.yidian.shenghuoquan.newscontent.http.callback.IUserLogoutCallBack
import com.yidian.shenghuoquan.newscontent.http.httpbean.AuthAuthenticationBean
import com.yidian.shenghuoquan.newscontent.http.httpbean.AuthMerchantCheckBean
import com.yidian.xpage.XPageManager

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

  companion object {
    // 从哪个页面跳转
    const val EXTRA_PAGE_FROM = "from"

    const val FROM_AUTH = "auth"
  }

  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
    )
    if (intent.getSerializableExtra(XRouterPathConstants.ParamsKey) != null) {
      val paramsMap = intent.getSerializableExtra(XRouterPathConstants.ParamsKey) as HashMap<*, *>
      val fromTag = paramsMap[EXTRA_PAGE_FROM] as String
      if (fromTag == FROM_AUTH) {
        // 如果由商户基本信息跳转至此页面 则此页面可返回
        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.tv_join_strategy -> {
        // TODO: 5/26/21 入驻攻略
      }
      R.id.view_personal_auth -> {
        // 请求个人认证信息 用于回显已填入的信息
        ApiService.authAuthentication(this)
      }
      R.id.view_enterprise_auth -> {
        // 请求企业认证信息 用于回显已填入的信息
        ApiService.authMerchantCheck(this, hashMapOf())
      }
      R.id.btn_logout -> {
        // 退出登录
        ApiService.userLogout(this)
      }
    }
  }

  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))
    )
  }

  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))
    )
  }

  override fun authMerchantCheckFailure(message: String?) {

  }

  override fun userLogoutSuccess() {
    // 退出登录成功 跳转登录页面
    XPageManager.push(XRouterPathConstants.LOGIN_LIFE_CIRCLE, null)
  }
}
