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.SearchSuggestionsAdapterItemBinding

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

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

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

    override fun getItemCount(): Int = list.size

    inner class ViewHolder(val binding: SearchSuggestionsAdapterItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    private var itemClickListener: OnItemClickListener? = null

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

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }
}