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

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemExpeditionSummaryRowBinding
import es.verdnatura.presentation.common.OnAddressRowClickListener
import es.verdnatura.presentation.common.OnItemImageLoadRowClickListener
import es.verdnatura.presentation.view.feature.delivery.model.ExpeditionInfoSummary


class ExpeditionSummaryAdapter(
    private val items: List<ExpeditionInfoSummary>,
    private val onAddressRowClickListener: OnAddressRowClickListener,
    private val onItemImageLoadRowClickListener: OnItemImageLoadRowClickListener
) : RecyclerView.Adapter<ExpeditionSummaryAdapter.ItemHolder>() {


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

    }

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

                if (item.delivery != item.total) {
                    fondo.setBackgroundColor(
                        res.getColor(R.color.verdnatura_pumpkin_light)
                    )
                } else {
                    fondo.setBackgroundColor(res.getColor(R.color.verdnatura_black))
                }
                if (item.delivered == item.total){
                    fondo.setBackgroundColor(res.getColor(R.color.verdnatura_black))
                }

                showExp.setOnClickListener {
                    onItemImageLoadRowClickListener.onItemImageLoadRowClickListener(item)
                }
                this.item = item
            }
        }
    }
}
