package es.verdnatura.presentation.view.feature.delivery.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemExpeditionSummaryRowBinding
import es.verdnatura.databinding.ItemRouteRowBinding
import es.verdnatura.domain.toDateString
import es.verdnatura.presentation.common.OnAddressRowClickListener
import es.verdnatura.presentation.common.OnItemCardRowClickListener
import es.verdnatura.presentation.common.OnItemImageLoadRowClickListener
import es.verdnatura.presentation.common.OnItemImageRouteRowClickListener
import es.verdnatura.presentation.view.feature.delivery.model.ExpeditionInfoSummary
import es.verdnatura.presentation.view.feature.delivery.model.RouteInfo
import es.verdnatura.presentation.view.feature.delivery.model.RouteLoaded


class RouteAdapter(
    private var items: MutableList<RouteInfo>,
    private val OnItemImageRouteRowClickListener: OnItemImageRouteRowClickListener,
    private val routeLoaded: List<RouteLoaded> = listOf()
) : RecyclerView.Adapter<RouteAdapter.ItemHolder>() {


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

    fun setHeaders(list: MutableList<RouteInfo>):MutableList<RouteInfo>{

        var prevCreated = ""
        for (route in list) {
            if (route.created.toString() != prevCreated) {
                route.header = true
                prevCreated = route.created.toString()
            }
        }
        return list
    }
    fun updateList(newList: MutableList<RouteInfo>) {
        items = setHeaders(newList)
        notifyDataSetChanged()
    }

    override fun getItemCount() = items.size

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

        holder.binding.root.setOnClickListener {
            //onAddressRowClickListener.onAddressRowClickListener(items[position])
        }

    }

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


                showTickets.setOnClickListener {
                    OnItemImageRouteRowClickListener.onItemImageRouteRowClickListener(item,"showTickets")
                }
                imageLoadVan.setOnClickListener {
                    OnItemImageRouteRowClickListener.onItemImageRouteRowClickListener(item,"loadVan")
                }
                linearLayoutShowTickets.setOnClickListener {
                    OnItemImageRouteRowClickListener.onItemImageRouteRowClickListener(item,"showTickets")
                }
                linearLayoutLoadVan.setOnClickListener {
                     OnItemImageRouteRowClickListener.onItemImageRouteRowClickListener(item,"loadVan")
                }

                if (item.header){
                    headerRouteLayout.visibility = View.VISIBLE
                    headerRoute.text = toDateString(item.created)
                }else{
                    headerRouteLayout.visibility = View.GONE


                }

                if (routeLoaded.filter { it.id == item.id }.isNotEmpty()){
                    imageLoadVan.visibility = View.INVISIBLE
                }else{
                    imageLoadVan.visibility = View.VISIBLE
                }
                this.item = item

            }
        }
    }
}
