package com.yidian.shenghuoquan.bmanagercenter.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.bean.BottomSelectBean
import com.yidian.shenghuoquan.bmanagercenter.databinding.ItemBottomSelectBinding

/**
 * author: yinjiacheng
 * date: 5/29/21 11:59 AM
 * description: 底部选择弹窗 adapter
 */
class BottomSelectAdapter<T : BottomSelectBean> @JvmOverloads constructor(
    data: ArrayList<T>? = null,
    listener: OnItemClickListener<T>? = null
) :
    RecyclerView.Adapter<BottomSelectAdapter.BottomSelectViewHolder>() {

    private val mData: ArrayList<T> by lazy { ArrayList<T>() }
    private val mListener = listener

    init {
        data?.let { mData.addAll(it) }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BottomSelectViewHolder {
        return BottomSelectViewHolder(
            ItemBottomSelectBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            ).root
        )
    }

    override fun onBindViewHolder(holder: BottomSelectViewHolder, position: Int) {
        holder.itemView.findViewById<TextView>(R.id.tv_content).text = mData[position].item
        holder.itemView.setOnClickListener {
            mListener?.onItemClick(it, position, mData[position])
        }
    }

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

    /**
     * 更新数据
     */
    fun updateData(data: ArrayList<T>) {
        mData.clear()
        mData.addAll(data)
        notifyDataSetChanged()
    }

    /**
     * ViewHolder
     */
    class BottomSelectViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    interface OnItemClickListener<T> {
        fun onItemClick(view: View, position: Int, data: T)
    }
}
