package com.yidian.shenghuoquan.bmanagercenter.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.yidian.bcommon.cardutils.adapter.OnItemClickListener
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.bcommon.bean.LifeAccountItemBean

/**
 * author: yinjiacheng
 * date: 5/19/21 5:32 PM
 * description: 生活号身份adapter
 */
class LifeAccountIdentityAdapter(val context: Context) :
  RecyclerView.Adapter<LifeAccountIdentityAdapter.LifeAccountIdentityViewHolder>() {

  private val mData: ArrayList<LifeAccountItemBean.Response> by lazy { ArrayList() }
  private lateinit var mListener: OnItemClickListener<LifeAccountItemBean.Response>
  private var mLastSelectItem: View? = null

  override fun onCreateViewHolder(
    parent: ViewGroup,
    viewType: Int
  ): LifeAccountIdentityViewHolder {
    return LifeAccountIdentityViewHolder(
      LayoutInflater.from(parent.context)
        .inflate(R.layout.item_life_account_identity, parent, false)
    )
  }

  override fun onBindViewHolder(holder: LifeAccountIdentityViewHolder, position: Int) {
    val data = mData[position]
    holder.itemView.findViewById<TextView>(R.id.tv_life_account).text = data.life_account_name
    val tvLifeIdentity = holder.itemView.findViewById<TextView>(R.id.tv_life_identity)
    if (data.role_type == 1) {
      tvLifeIdentity.text = "超级管理员"
      tvLifeIdentity.setBackgroundResource(R.drawable.bg_item_manager_selected_on)
      tvLifeIdentity.setTextColor(ContextCompat.getColor(context, R.color.color_FFFFFFFF))
    } else {
      tvLifeIdentity.text = "普通员工"
      tvLifeIdentity.setBackgroundResource(R.drawable.bg_item_manager_selected_off)
      tvLifeIdentity.setTextColor(ContextCompat.getColor(context, R.color.color_FFEAAF35))
    }

    holder.itemView.setOnClickListener {
      mLastSelectItem?.isSelected = false
      mLastSelectItem = holder.itemView
      holder.itemView.isSelected = true
      mListener.onClick(it, position, data)
    }
  }

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

  /**
   * 更新数据
   */
  fun updateData(data: List<LifeAccountItemBean.Response>?) {
    data?.let {
      mData.clear()
      mData.addAll(it)
      notifyDataSetChanged()
    }
  }

  fun setOnItemClickListener(listener: OnItemClickListener<LifeAccountItemBean.Response>) {
    mListener = listener
  }

  /**
   * 自定义ViewHolder
   */
  class LifeAccountIdentityViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

}
