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

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.databinding.ItemEscanerRowBinding
import es.verdnatura.presentation.common.OnAutomaticItemClickListener
import es.verdnatura.presentation.view.feature.ubicador.model.ItemEscanerVO

class AutomaticAdapter (
    private val items: List<ItemEscanerVO>,
    private val onAutomaticItemClickListener: OnAutomaticItemClickListener
): RecyclerView.Adapter<AutomaticAdapter.ItemHolder> () {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        return ItemHolder(
            ItemEscanerRowBinding.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 {
            onAutomaticItemClickListener.onAutomaticItemClickListener(position)
        }
    }

    inner class ItemHolder(
        val binding: ItemEscanerRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        private val res = binding.root.context.resources
        fun bind(item: ItemEscanerVO) {
            binding.apply {
                this.item = item
            }
        }
    }
}