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

import android.view.LayoutInflater
import android.view.View.GONE
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemStopmapTruckPalletRowBinding
import es.verdnatura.presentation.common.OnPrintClickListener
import es.verdnatura.presentation.view.feature.roadmap.model.StopMapTruckPallet
import es.verdnatura.presentation.view.feature.roadmap.model.TruckAction

class StopMapPalletListAdapter(
    private var items: List<StopMapTruckPallet>,
    private val onPrintClickListener: OnPrintClickListener,
    private val action: TruckAction
) : RecyclerView.Adapter<StopMapPalletListAdapter.ItemHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        return ItemHolder(
            ItemStopmapTruckPalletRowBinding.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 {
            //   onStopMapClickListener.onStopMapClickListener(items[position], "")

        }
    }

    fun setList(itemsList: List<StopMapTruckPallet>) {
        items = itemsList
        notifyDataSetChanged()
    }

    inner class ItemHolder(
        val binding: ItemStopmapTruckPalletRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        private val res = binding.root.context.resources
        fun bind(item: StopMapTruckPallet) {
            binding.apply {

                if (action == TruckAction.UNLOAD) {
                    binding.printNewRoutePallet.visibility = GONE
                    binding.printNewRoutePallet.setOnClickListener {
                        onPrintClickListener.onPrintClickListener(item, "")
                    }
                } else {
                    binding.printNewRoutePallet.visibility = GONE
                }

                itemRowLayout.setBackgroundColor(
                    ContextCompat.getColor(
                        binding.root.context!!,
                        if (item.hasMatchStateCode) {
                            R.color.verdnatura_black_5
                        } else {
                            R.color.salix_success_light
                        }

                    )
                )


                this.item = item

            }
        }
    }
}