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

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.databinding.ItemStopmapTruckRowBinding
import es.verdnatura.presentation.common.OnStopMapClickListener
import es.verdnatura.presentation.view.feature.roadmap.model.RoadStopMapTruck
import es.verdnatura.presentation.view.feature.roadmap.model.TruckAction

class StopMapListAdapter(
    private var items: List<RoadStopMapTruck>,
    private val onStopMapClickListener: OnStopMapClickListener
) : RecyclerView.Adapter<StopMapListAdapter.ItemHolder>() {

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

    override fun getItemCount() = items.size

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.bind(items[position])
    }

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

    inner class ItemHolder(
        val binding: ItemStopmapTruckRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        private val res = binding.root.context.resources
        fun bind(item: RoadStopMapTruck) {
            binding.apply {
                binding.loadPallet.setOnClickListener {

                    onStopMapClickListener.onStopMapClickListener(item, "", TruckAction.LOAD)
                }
                binding.unLoadPallet.setOnClickListener {

                    onStopMapClickListener.onStopMapClickListener(item, "", TruckAction.UNLOAD)
                }
                /*   binding.icCall.setOnClickListener {
                       onStopMapClickListener.onStopMapClickListener(
                           item,
                           "",
                           TruckAction.CALL
                       )
                   }

                   binding.ubication.setOnClickListener {
                       onStopMapClickListener.onStopMapClickListener(
                           item,
                           "",
                           TruckAction.GOTOMAPS
                       )
                   }*/

                binding.moreActions.setOnClickListener {

                    onStopMapClickListener.onStopMapClickListener(
                        item,
                        "",
                        TruckAction.OPTIONS, binding.root
                    )

                }
                this.item = item
            }
        }
    }
}