package com.yidian.shenghuoquan.bmanagercenter.service

import com.yidian.bcommon.bean.LifeAccountItemBean
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.services.AppUpgradeService
import com.yidian.bcommon.services.ZapServiceActionConstants
import com.yidian.bcommon.utils.StorageUtil
import com.yidian.bcommon.utils.ToastUtils
import com.yidian.shenghuoquan.bmanagercenter.http.ApiService
import com.yidian.shenghuoquan.bmanagercenter.http.callback.IGetLifeAccountListCallback
import com.yidian.shenghuoquan.bmanagercenter.ui.auth.LifeAccountAuthDataManagerV2
import com.yidian.shenghuoquan.bmanagercenter.ui.auth.LifeAccountIdentityActivity
import com.yidian.xarc.xrouter.ResultResolver
import com.yidian.xpage.XPageManager
import com.yidian.yac.core.zap.ZapService
import com.yidian.yac.core.zap.ZapTicket
import org.json.JSONObject
import timber.log.Timber

/**
 * 生活号列表服务
 */
class LifeNumberListService : ZapService() {

  private lateinit var resolver: ResultResolver

  override fun onAction(name: String, action: String, params: Any, options: Any, resolver: ResultResolver) {
    this.resolver = resolver
    params as JSONObject
    when (action) {
      ZapServiceActionConstants.ActionRequestList -> requestLifeNumberList()
//      ZapServiceActionConstants.ActionRequestSmsCode -> requestSmsCode(params)
      ZapServiceActionConstants.ActionClearAuthenticationData -> clearAuthenticationData()
    }
  }

  override fun onExit() {

  }

  override fun onInit() {

  }

  /**
   * 请求生活号列表
   */
  private fun requestLifeNumberList() {
    Timber.tag("测试启动").e("使用服务，请求生活号列表")
    ApiService.getLifeAccountList(object : IGetLifeAccountListCallback {
      override fun getLifeAccountListSuccess(result: ArrayList<LifeAccountItemBean.Response>?) {
        when (result?.size) {
          0 -> {
            // 无生活号 进入认证流程
            XPageManager.push(XRouterPathConstants.LIFE_ACCOUNT_AUTH, null)
          }
          1 -> {
            // 此处处理用户已经关联一个生活号但本地无存储/app数据被清除的情况
            StorageUtil.putLifeAccountId(result[0].life_account_id)
            // 关联一个生活号 进入商户管理中心
            XPageManager.push(XRouterPathConstants.MERCHANT_CENTER, null)
          }
          else -> {
            // 关联多个生活号
            StorageUtil.getLifeAccountId()?.apply {
              // 如果本地有上次操作的生活号则遍历生活号列表进入对应生活号的管理中心
              result?.forEach {
                if (it.life_account_id == this) {
                  XPageManager.push(XRouterPathConstants.MERCHANT_CENTER, null)
                }
              }
            } ?: let {
              // 进入生活号选择页面
              XPageManager.push(
                XRouterPathConstants.LIFE_ACCOUNT_IDENTITY,
                hashMapOf(Pair(LifeAccountIdentityActivity.EXTRA_LIFE_ACCOUNT_LIST, result))
              )
            }
          }
        }
        // 关闭启动页
        resolver.success(true)
        // 检查app更新
        ZapTicket(AppUpgradeService.SERVICE_NAME).withAction(AppUpgradeService.ACTION_CHECK_APP_UPGRADE).ship()
      }

      override fun getLifeAccountListFailure(message: String?) {
        ToastUtils.showShortSafe(message)
      }
    })

  }

  /**
   * 清除内存中的认证数据
   */
  private fun clearAuthenticationData() {
    LifeAccountAuthDataManagerV2.personalAuthData.clearData()
    LifeAccountAuthDataManagerV2.merchantAuthData.clearData()
  }

}
