package com.bassbooster.soundeffects.equalizer.view.activity.effects.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bassbooster.soundeffects.equalizer.database.EffectsData
import com.bassbooster.equalizer.databinding.AdapterMyEffectsBinding

class MyEffectsAdapter(
    val context: Context,
    var list: List<EffectsData>,
    val mListener: OnItemClickListener
) : RecyclerView.Adapter<MyEffectsAdapter.ViewHolder>() {
    var showDeleteIcon = false

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

    fun updateData(newList: List<EffectsData>) {
        list = newList
        notifyDataSetChanged()
    }

    fun setDeleteIconVisibility(show: Boolean) {
        showDeleteIcon = show
        notifyDataSetChanged()
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.mTxtName.text = list[position].name
        holder.binding.icDelete.setOnClickListener {
            mListener.onDelete(position)
        }
        holder.binding.rlLayout.setOnClickListener {
            mListener.onItemClick(position)
        }
    }

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

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


}