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

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemCollectionRowNewBinding
import es.verdnatura.presentation.common.OnCollectionTicketSelectedListener
import es.verdnatura.presentation.view.feature.sacador.model.CollectionTicket

class CollectionAdapterNew(
    private val items: List<CollectionTicket>,
    private val onCollectionTicketSelected: OnCollectionTicketSelectedListener,
    private val type: String,
) : RecyclerView.Adapter<CollectionAdapterNew.AjustesItemHolder>() {

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

    override fun getItemCount() = items.size

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

    class AjustesItemHolder(
        val binding: ItemCollectionRowNewBinding,
        val type: String
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: CollectionTicket) {

            binding.apply {
                if (item.ticketTotalCount == null || type == "PREITEMPICKERTEST") {
                    binding.totalTicketsId.visibility = View.GONE
                    binding.itemImage.setImageDrawable(ContextCompat.getDrawable(root.context, R.drawable.ic_add_black_24dp))
                }
                this.item = item
            }
        }
    }
}