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

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemExpeditionpalletRowBinding
import es.verdnatura.presentation.common.OnComprobarPalletViewClickListener
import es.verdnatura.presentation.common.OnPalletClickListener
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO
import timber.log.Timber.d

class ExpeditionPalletAdapter (
    private val items: List<ItemPalletVO>,
    private val onPalletClickListener: OnPalletClickListener,
    private val onComprobarPalletViewClickListener: OnComprobarPalletViewClickListener,
    private val itemExpeditionTruckVO: ItemExpeditionTruckVO
): RecyclerView.Adapter<ExpeditionPalletAdapter.ItemHolder> () {

    private var context:Context? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        this.context = parent.context
        return ItemHolder(
            ItemExpeditionpalletRowBinding.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 {
            //Tarea #4125
             Log.d("VERDNATURA::","Entrando en el pallet")
            onPalletClickListener.onPalletClickListener(itemExpeditionTruckVO,items[position])
        }
        holder.binding.root.setOnLongClickListener {
            onComprobarPalletViewClickListener.onComprobarPalletViewClickListener(itemExpeditionTruckVO,items[position])
            true
        }
    }

    inner class ItemHolder(
        val binding: ItemExpeditionpalletRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        private val res = binding.root.context.resources
        fun bind(item: ItemPalletVO) {
            binding.apply {
                this.item = item
                if (item.Rutas != "1"){
                    expeditionPalletRutas.setTextColor(ContextCompat.getColor(context!!, R.color.verdnatura_red))
                }else {
                    expeditionPalletRutas.setTextColor(ContextCompat.getColor(context!!, R.color.verdnatura_white))
                }
            }
        }
    }
}