package com.yidian.shenghuoquan.newscontent.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.yidian.shenghuoquan.newscontent.bean.BottomSelectBean
import com.yidian.shenghuoquan.newscontent.databinding.ItemBottomSelectBinding
import kotlinx.android.synthetic.main.item_bottom_select.view.*

/**
 * 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.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)
    }
}