package com.yidian.shenghuoquan.bmanagercenter.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.yidian.bcommon.bean.LifeAccountItemBean
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ItemMerchantSwitchBinding
import com.yidian.shenghuoquan.bmanagercenter.manager.LifeAccountManager

/**
 * author: yinjiacheng
 * date: 6/16/21 12:02 AM
 * description: 商户切换主体
 */
class MerchantSwitchAdapter(
  private val curLifeAccountId: String,
  data: List<LifeAccountItemBean.Response>? = null,
  val listener: OnItemClickListener? = null
) :
  RecyclerView.Adapter<MerchantSwitchAdapter.MerchantSwitchViewHolder>() {

  private val data by lazy {
    data?.let {
      ArrayList<LifeAccountItemBean.Response>(it)
    } ?: ArrayList()
  }

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MerchantSwitchViewHolder {
    val viewBinding = ItemMerchantSwitchBinding.bind(
      LayoutInflater.from(parent.context)
        .inflate(R.layout.item_merchant_switch, parent, false)
    )
    return MerchantSwitchViewHolder(viewBinding.root, viewBinding)
  }

  override fun onBindViewHolder(holder: MerchantSwitchViewHolder, position: Int) {
    val data = data[position] as LifeAccountItemBean.Response
    holder.viewBinding.tvLifeAccountName.text = data.life_account_name
    holder.viewBinding.tvLifeAccountStatus.apply {
      text = data.tag.tag_text
      when (LifeAccountManager.getLifeAccountTag(data.tag.tag_key)) {
        LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_COMMON_ENTERPRISE_AUTH, LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_INDIVIDUAL_BUSINESS_AUTH, LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_PERSONAL_AUTH -> {
          // 个体工商户、普通企业、个人
          setBackgroundResource(R.drawable.shape_life_account_status_auth)
          setTextColor(ContextCompat.getColor(context, R.color.color_FF1852F1))
        }
        LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_NOT_AUTH -> {
          // 未认证
          setBackgroundResource(R.drawable.shape_life_account_status_unauth)
          setTextColor(ContextCompat.getColor(context, R.color.color_333333))
        }
        LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_AUDIT_UNDER -> {
          // 审核中
          setBackgroundResource(R.drawable.shape_life_account_status_audit)
          setTextColor(ContextCompat.getColor(context, R.color.color_FFF26E3D))
        }
        LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_AUTH_REJECT -> {
          // 审核驳回
          setBackgroundResource(R.drawable.shape_life_account_status_reject)
          setTextColor(ContextCompat.getColor(context, R.color.color_FFFF3A3A))
        }
        else -> {
          setBackgroundResource(R.drawable.shape_life_account_status_auth)
          setTextColor(ContextCompat.getColor(context, R.color.color_FF1852F1))
        }
      }
    }
    // 当前选中的生活号
    holder.viewBinding.ivLifeAccountSelect.isVisible = data.life_account_id == curLifeAccountId
    holder.viewBinding.root.setOnClickListener {
      listener?.onItemClick(it, position, data)
    }
  }

  override fun getItemCount(): Int {
    return data.size
  }

  interface OnItemClickListener {
    fun onItemClick(view: View, position: Int, data: LifeAccountItemBean.Response)
  }

  class MerchantSwitchViewHolder(itemView: View, val viewBinding: ItemMerchantSwitchBinding) :
    RecyclerView.ViewHolder(itemView)

}
