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

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
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.AdapterCallback
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,
    private val adapterCallBack: AdapterCallback
) : 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 {

                val color = getColorBackground(item)
                fondo.setBackgroundColor(color)

                showExp.setOnClickListener {
                    onItemImageLoadRowClickListener.onItemImageLoadRowClickListener(item)
                }

                if (adapterCallBack.getPosition() == absoluteAdapterPosition) {
                    val border = GradientDrawable()
                    border.shape = GradientDrawable.RECTANGLE
                    border.setStroke(2, Color.WHITE)
                    border.setColor(color)
                    fondo.background = border
                }


                this.item = item
            }
        }

        private fun getColorBackground(item: ExpeditionInfoSummary): Int {
            var color: Int
            if (item.delivery != item.total) {
                color = binding.root.context.getColor(R.color.verdnatura_pumpkin_light)
            } else {
                color = binding.root.context.getColor(R.color.verdnatura_black)
            }

            if (item.delivered == item.total) {
                color = binding.root.context.getColor(R.color.verdnatura_black)
            }
            return color

        }
    }
}
