package com.yidian.shenghuoquan.newscontent.ui

import android.content.Intent
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.common.services.AppUpgradeService
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
import com.yidian.yac.core.zap.ZapTicket

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()
//    getPermissions()
  }

  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 {
        // 未登录 跳转登录
        // 此处没有使用XPage打开页面 原因：不能设置flag
        startActivity(Intent(this, LoginLifeCircleActivity::class.java))
        finish()
        // 检查app更新
        ZapTicket(AppUpgradeService.SERVICE_NAME).withAction(AppUpgradeService.ACTION_CHECK_APP_UPGRADE).ship()
      }
    }
  }

  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))
          )
        }
      }
    }
    XPageManager.pop(null)
    // 检查app更新
    ZapTicket(AppUpgradeService.SERVICE_NAME).withAction(AppUpgradeService.ACTION_CHECK_APP_UPGRADE).ship()
  }

  override fun getLifeAccountListFailure(message: String?) {

  }

  // 因为市场审核，不能在用户未同意用户协议之前申请权限
//  private fun getPermissions() {
//    val rxPermissions = RxPermissions(this)
//    rxPermissions.request(
//      Manifest.permission.ACCESS_COARSE_LOCATION,
//      Manifest.permission.ACCESS_FINE_LOCATION,
//      Manifest.permission.READ_EXTERNAL_STORAGE,
//      Manifest.permission.READ_PHONE_STATE,
//    )
//      .subscribe {
//        if (it) {
//          launcher()
//        } else {
//          DialogUtils.showPermissionDialog(this, "需要电话、存储、位置")
//        }
//      }
//  }

}
