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

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.databinding.ItemCollectionRowBinding
import es.verdnatura.presentation.common.OnCollectionSelectedListener
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO

class CollectionAdapter(
    private val items: List<CollectionVO>,
    private val onCollectionSelected: OnCollectionSelectedListener,
    private val type: String
) : RecyclerView.Adapter<CollectionAdapter.AjustesItemHolder>() {


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

    override fun getItemCount() = items.size

    override fun onBindViewHolder(holder: AjustesItemHolder, position: Int) {
        holder.bind(items[position])
        holder.binding.root.setOnClickListener {
            onCollectionSelected.onCollectionSelected(items[position], type)
        }
    }


    class AjustesItemHolder(
        val binding: ItemCollectionRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: CollectionVO) {
            binding.apply {
                if (item.ticketTotalCount == null) {
                    binding.totalTicketsId.visibility = View.GONE
                }
                this.item = item
            }
        }
    }
}