package com.yidian.shenghuoquan.newscontent.ui

import android.Manifest
import android.os.Bundle
import com.orhanobut.hawk.Hawk
import com.tbruyelle.rxpermissions3.RxPermissions
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.R
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.utils.ToastUtil
import com.yidian.xpage.XPageManager

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

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

  override fun getCurrentTheme(isNight: Boolean): Int {
    return R.style.AppTheme
  }

  override fun getXPageName(): String {
    return FLASH
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    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 {
        // 未登录 跳转登录
        XPageManager.push(XRouterPathConstants.LOGIN_LIFE_CIRCLE, null)
        XPageManager.pop(null)
      }
    } else {
      // TODO: 6/12/21 协议dialog
    }
  }

  private fun getPermissions() {
    RxPermissions(this).request(
      Manifest.permission.WRITE_EXTERNAL_STORAGE,
      Manifest.permission.CAMERA
    )
      .subscribe {
        if (it) {
          launcher()
        } else {
          ToastUtil.showToast(this, "请在设置里同意相关权限")
        }
      }
  }

  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?) {

  }
}
