package com.yidian.shenghuoquan.newscontent.ui.auth

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yidian.common.XRouterPathConstants
import com.yidian.common.base.BaseActivity
import com.yidian.common.cardutils.adapter.OnItemClickListener
import com.yidian.shenghuoquan.newscontent.adapter.LifeAccountIdentityAdapter
import com.yidian.shenghuoquan.newscontent.databinding.ActivityLifeAccountIdentityBinding
import com.yidian.shenghuoquan.newscontent.http.httpbean.LifeAccountItemBean
import com.yidian.shenghuoquan.newscontent.ui.center.MerchantCenterActivity
import com.yidian.shenghuoquan.newscontent.utils.StorageUtil
import com.yidian.xpage.XPageManager

/**
 * author: yinjiacheng
 * date: 5/19/21 4:47 PM
 * description: 生活号身份选择
 */
class LifeAccountIdentityActivity : BaseActivity<ActivityLifeAccountIdentityBinding>(),
  OnItemClickListener<LifeAccountItemBean.Response> {

  private var lastSelectedIndex = -1

  /**
   * 当前选择的生活号
   */
  private lateinit var curSelectedItem: LifeAccountItemBean.Response

  companion object {
    // 传递的生活号列表
    const val EXTRA_LIFE_ACCOUNT_LIST = "lifeAccountList"
  }

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

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

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

  private fun initView() {
    viewBind.rvLifeAccountIdentity.layoutManager =
      LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    viewBind.rvLifeAccountIdentity.adapter = LifeAccountIdentityAdapter(this)
    (viewBind.rvLifeAccountIdentity.adapter as LifeAccountIdentityAdapter).setOnItemClickListener(
      this
    )
    setLoginButtonStatus(false)
  }

  private fun initData() {
    val data = intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { params ->
      (params as HashMap<*, *>)[EXTRA_LIFE_ACCOUNT_LIST]?.let {
        it as List<LifeAccountItemBean.Response>
      }
    }
    (viewBind.rvLifeAccountIdentity.adapter as LifeAccountIdentityAdapter).updateData(data)
  }

  private fun onClickListener() {
    viewBind.back.setOnClickListener {
      XPageManager.pop(null)
    }
    viewBind.tvEnterLifeAccount.setOnClickListener {
      StorageUtil.putLifeAccountId(curSelectedItem.life_account_id)
      // 进入商户管理中心页
      XPageManager.push(
        XRouterPathConstants.MERCHANT_CENTER,
        hashMapOf(
          Pair(MerchantCenterActivity.EXTRA_LIFE_ACCOUNT_INFO, curSelectedItem),
          Pair(MerchantCenterActivity.EXTRA_IS_MULTI, true)
        )
      )
      XPageManager.pop(null)
    }
  }

  override fun onClick(view: View?, position: Int, item: LifeAccountItemBean.Response?) {
    if (item == null) return
    if (position != lastSelectedIndex) {
      setLoginButtonStatus(true)
      curSelectedItem = item
      lastSelectedIndex = position
    }
  }

  private fun setLoginButtonStatus(flag: Boolean) {
    if (flag) {
      viewBind.tvEnterLifeAccount.alpha = 1.0f
      viewBind.tvEnterLifeAccount.isEnabled = true
    } else {
      viewBind.tvEnterLifeAccount.alpha = 0.32f
      viewBind.tvEnterLifeAccount.isEnabled = false
    }
  }
}
