package com.yidian.shenghuoquan.newscontent.ui

import android.os.Bundle
import com.orhanobut.hawk.Hawk
import com.yidian.common.HawkConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.XRouterPathConstants.Companion.FLASH
import com.yidian.common.base.BaseActivity
import com.yidian.shenghuoquan.newscontent.databinding.ActivityFlashBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.callback.IGetLifeAccountListCallback
import com.yidian.shenghuoquan.newscontent.http.httpbean.LifeAccountItemBean
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.xpage.XPageManager

class FlashActivity : BaseActivity<ActivityFlashBinding>(), IGetLifeAccountListCallback {

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

  override fun getXPageName(): String {
    return FLASH
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    launcher()
  }

  private fun launcher() {
    val isAgree = Hawk.get(HawkConfig.ProtocolIsAgree, true)
    if (isAgree) {
      val loginStatus = Hawk.get(HawkConfig.LoginStatus, false)
      if (loginStatus) {
        // 已登录 请求生活号列表
        ApiService.getLifeAccountList(this)
      } else {
        // 未登录 跳转登录
        XPageManager.push(XRouterPathConstants.LOGIN_LIFE_CIRCLE, null)
        XPageManager.pop(null)
        finish()
      }
    }
  }

  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 {
          if (isNotEmpty()) {
            // 如果本地有上次操作的生活号则遍历生活号列表进入对应生活号的管理中心
            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)
                  )
                )
              }
            }
          } else {
            // 进入生活号选择页面
            XPageManager.push(
              XRouterPathConstants.LIFE_ACCOUNT_IDENTITY,
              hashMapOf(Pair(LifeAccountIdentityActivity.EXTRA_LIFE_ACCOUNT_LIST, result))
            )
          }
        }
      }
    }
    XPageManager.pop(null)
  }

  override fun getLifeAccountListFailure(message: String?) {

  }
}
