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.ItemRoadmapTruckRowBinding
import es.verdnatura.presentation.common.OnRoadMapClickListener
import es.verdnatura.presentation.view.feature.roadmap.model.RoadMapTruck

class RoadMapListAdapter(
    private var items: List<RoadMapTruck>,
    private val onRoadMapClickListener: OnRoadMapClickListener
) : RecyclerView.Adapter<RoadMapListAdapter.ItemHolder>() {

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

        }
    }

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

    inner class ItemHolder(
        val binding: ItemRoadmapTruckRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        private val res = binding.root.context.resources
        fun bind(item: RoadMapTruck) {
            binding.apply {
                this.item = item
            }
        }
    }
}