package com.yidian.shenghuoquan.newscontent.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.yidian.common.cardutils.adapter.OnItemClickListener
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.http.httpbean.LifeAccountItemBean
import kotlinx.android.synthetic.main.item_life_account_identity.view.*

/**
 * 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.tv_life_account.text = data.life_account_name
    if (data.role_type == 1) {
      holder.itemView.tv_life_identity.text = "超级管理员"
      holder.itemView.tv_life_identity.setBackgroundResource(R.drawable.bg_item_manager_selected_on)
      holder.itemView.tv_life_identity.setTextColor(ContextCompat.getColor(context, R.color.color_FFFFFFFF))
    } else {
      holder.itemView.tv_life_identity.text = "普通员工"
      holder.itemView.tv_life_identity.setBackgroundResource(R.drawable.bg_item_manager_selected_off)
      holder.itemView.tv_life_identity.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)

}
