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.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.constant.Constant
import com.yidian.shenghuoquan.bmanagercenter.databinding.ItemMerchantSwitchBinding
import com.yidian.bcommon.bean.LifeAccountItemBean

/**
 * 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 {
      when (data.life_account_auth_status) {
        Constant.STATUS_AUTH_NOT -> {
          // 未认证
          setBackgroundResource(R.drawable.shape_life_account_status_unauth)
          setTextColor(ContextCompat.getColor(context, R.color.color_333333))
          text = resources.getString(R.string.not_auth)
        }
        Constant.STATUS_AUTH_FINISH -> {
          // 已认证
          setBackgroundResource(R.drawable.shape_life_account_status_auth)
          setTextColor(ContextCompat.getColor(context, R.color.color_FF1852F1))
          text = when (data.tag.tag_key) {
            Constant.LIFE_ACCOUNT_TAG_COMMON_ENTERPRISE_AUTH -> resources.getString(R.string.common_enterprise)
            Constant.LIFE_ACCOUNT_TAG_INDIVIDUAL_BUSINESS_AUTH -> resources.getString(R.string.individual_business)
            Constant.LIFE_ACCOUNT_TAG_PERSONAL_AUTH -> resources.getString(R.string.personal_auth)
            else -> ""
          }
        }
      }
      when (data.enterprise_audit_status) {
        Constant.STATUS_AUDIT_UNDER -> {
          // 审核中
          setBackgroundResource(R.drawable.shape_life_account_status_audit)
          setTextColor(ContextCompat.getColor(context, R.color.color_FFF26E3D))
          text = resources.getString(R.string.under_audit)
        }
        Constant.STATUS_AUDIT_REJECT -> {
          // 审核驳回
          setBackgroundResource(R.drawable.shape_life_account_status_reject)
          setTextColor(ContextCompat.getColor(context, R.color.color_FFFF3A3A))
          text = resources.getString(R.string.auth_reject)
        }
      }
    }
    // 当前选中的生活号
    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)

}
