package com.player.musicoo.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.player.musicoo.databinding.SearchHistoryAdapterItemBinding

class SearchHistoryAdapter(
    private val context: Context,
    private val list: List<String>,
) :
    RecyclerView.Adapter<SearchHistoryAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding =
            SearchHistoryAdapterItemBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bean = list[position]
        holder.bind(bean)
        holder.itemView.setOnClickListener {
            if (itemClickListener != null) {
                itemClickListener?.onHistoryItemClick(position)
            }
        }
    }

    override fun getItemCount(): Int = list.size

    inner class ViewHolder(private val binding: SearchHistoryAdapterItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(bean: String) {
            binding.apply {
                historyName.text = bean
            }
        }
    }

    private var itemClickListener: HistoryOnItemClickListener? = null

    fun setOnItemClickListener(listener: HistoryOnItemClickListener) {
        itemClickListener = listener
    }

    interface HistoryOnItemClickListener {
        fun onHistoryItemClick(position: Int)
    }
}