package com.yidian.shenghuoquan.bmanagercenter.ui.auth

import android.os.Bundle
import android.view.View
import com.orhanobut.hawk.Hawk
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.bean.LifeAccountItemBean
import com.yidian.bcommon.constant.HawkConfig
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityLifeAccountCreateCompleteBinding
import com.yidian.shenghuoquan.bmanagercenter.http.ApiService
import com.yidian.shenghuoquan.bmanagercenter.http.callback.IGetLifeAccountListCallback
import com.yidian.xpage.XPageManager

/**
 * author: yinjiacheng
 * date: 6/17/21 00:31 AM
 * description: 生活号创建成功
 */
class LifeAccountCreateCompleteActivity : BaseActivity<ActivityLifeAccountCreateCompleteBinding>(), IGetLifeAccountListCallback,
  View.OnClickListener {

  companion object {
    const val EXTRA_LIFE_ACCOUNT_NAME = "live_account_name"
    const val EXTRA_LIFE_ACCOUNT_ID = "life_account_id"
  }

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

  override fun getXPageName(): String {
    return XRouterPathConstants.LIFE_ACCOUNT_CREATE_COMPLETE
  }

  /**
   * 由生活号创建页传递的参数
   */
  private val params by lazy {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let {
      it as HashMap<*, *>
    }
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initView()
    initListener()
    initData()
  }

  private fun initView() {
    // 此页面不可返回
    viewBind.bvTopBar.setBackVisibility(false)
  }

  private fun initData() {
    viewBind.ivMerchantInfo.fillAuthInfo(
      params?.get(EXTRA_LIFE_ACCOUNT_NAME)?.let { it as String },
      Hawk.get(HawkConfig.Nickname, ""),
      Hawk.get(HawkConfig.Mobile, "")
    )
  }

  private fun initListener() {
    viewBind.tvEnterMerchantCenter.setOnClickListener(this)
  }

  override fun getLifeAccountListSuccess(result: ArrayList<LifeAccountItemBean.Response>?) {
    // 生活号列表请求成功
    val curLifeAccountId = params?.get(EXTRA_LIFE_ACCOUNT_ID)?.let { it as String }
    result?.forEach {
      if (it.life_account_id == curLifeAccountId) {
        XPageManager.push(XRouterPathConstants.MERCHANT_CENTER, null)
        XPageManager.pop(null)
      }
    }
  }

  override fun getLifeAccountListFailure(message: String?) {

  }

  override fun onClick(v: View?) {
    if (v?.id == R.id.tv_enter_merchant_center) {
      // 跳转商户管理中心 先请求生活号列表 带参跳转
      ApiService.getLifeAccountList(this)
    }
  }

  override fun onBackPressed() {
    // 禁止back event
  }

}
