package es.verdnatura.presentation.view.feature.smarttag.model

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.databinding.ItemSmarttagRowBinding
import es.verdnatura.presentation.common.OnSmartTagRowClickListener


class SmartTagAdapter(
private val items: List<SmartTag>,
private val OnSmartTagRowClickListener: OnSmartTagRowClickListener,
private var showDelete: Boolean = true
): RecyclerView.Adapter<SmartTagAdapter.ItemHolder> () {


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

    override fun getItemCount() =items.size

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.bind(items[position])

        holder.binding.root.setOnClickListener {
            OnSmartTagRowClickListener.OnSmartTagRowClickListener(items[position])
        }

    }


    inner class ItemHolder(
        val binding: ItemSmarttagRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        private val res = binding.root.context.resources
        fun bind(item: SmartTag) {
            binding.apply {
                this.item = item
                if (showDelete) itemImage.visibility = View.VISIBLE
                else itemImage.visibility = View.GONE
            }
        }
    }
}

