package com.yidian.shenghuoquan.newscontent.service

import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.bean.LifeAccountItemBean
import com.yidian.bcommon.services.AppUpgradeService
import com.yidian.bcommon.services.ZapServiceActionConstants
import com.yidian.bcommon.utils.ToastUtils
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.callback.IGetLifeAccountListCallback
import com.yidian.shenghuoquan.newscontent.ui.auth.LifeAccountIdentityActivity
import com.yidian.shenghuoquan.newscontent.ui.center.MerchantCenterActivity
import com.yidian.shenghuoquan.newscontent.utils.StorageUtil
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
import kotlin.collections.ArrayList

/**
 * 生活号列表服务
 */
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.LifeNumberListServiceRequestList -> requestLifeNumberList()
    }
  }

  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 -> {
            // 关联一个生活号 进入商户管理中心
            XPageManager.push(
              XRouterPathConstants.MERCHANT_CENTER,
              hashMapOf(
                Pair(MerchantCenterActivity.EXTRA_LIFE_ACCOUNT_INFO, result[0]),
                Pair(MerchantCenterActivity.EXTRA_IS_MULTI, false)
              )
            )
          }
          else -> {
            // 关联多个生活号
            StorageUtil.getLifeAccountId()?.apply {
              // 如果本地有上次操作的生活号则遍历生活号列表进入对应生活号的管理中心
              result?.forEach {
                if (it.life_account_id == this) {
                  XPageManager.push(
                    XRouterPathConstants.MERCHANT_CENTER,
                    hashMapOf(
                      Pair(MerchantCenterActivity.EXTRA_LIFE_ACCOUNT_INFO, it),
                      Pair(MerchantCenterActivity.EXTRA_IS_MULTI, result.size > 1)
                    )
                  )
                }
              }
            } ?: 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)
      }
    })

  }
}
