package es.verdnatura.presentation.view.feature.articulo.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.databinding.ItemBarcodeRowBinding
import es.verdnatura.presentation.common.OnBarcodeRowClickListener
import es.verdnatura.presentation.view.feature.articulo.model.BarcodeVO

class BarcodeAdapter(
    private val items: List<BarcodeVO>,
    private val onBarcodeRowClickListener: OnBarcodeRowClickListener,
    private var showDelete: Boolean = true
): RecyclerView.Adapter<BarcodeAdapter.ItemHolder> () {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        return ItemHolder(
            ItemBarcodeRowBinding.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 {
            onBarcodeRowClickListener.onBarcodeRowClickListener(items[position])
        }

    }


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