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

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemArticleRowBinding
import es.verdnatura.databinding.ItemArticleRowPresacadorBinding
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.common.OnQuantityClickListener
import es.verdnatura.presentation.common.OnSaleClickListener
import es.verdnatura.presentation.view.feature.collection.adapter.PlacementAdapter
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import es.verdnatura.presentation.view.feature.presacador.mapper.toSale
import es.verdnatura.presentation.view.feature.presacador.model.PreSacadorItemVO
import es.verdnatura.presentation.view.feature.sacador.model.SaleVO

class PreSacadorAdapter (
    private val items: List<PreSacadorItemVO>,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener,
    private val onQuantityClick: OnQuantityClickListener,
    private val onSaleClickListener: OnSaleClickListener
): RecyclerView.Adapter<PreSacadorAdapter.AjustesItemHolder> () {
    var context: Context? = null
    var position:Int = 0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AjustesItemHolder {
        this.context = parent.context

        return AjustesItemHolder(
            ItemArticleRowPresacadorBinding.inflate(LayoutInflater.from(parent.context),parent,false)


        )
    }

    override fun getItemCount() =items.size

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

    inner class AjustesItemHolder(
        val binding: ItemArticleRowPresacadorBinding
    ) : RecyclerView.ViewHolder(binding.root){
        fun bind(preSale: PreSacadorItemVO) {
            binding.apply {

                val sale : SaleVO = preSale.toSale()

                if (sale.pickedQuantity.isNullOrEmpty())
                    sale.pickedQuantity = "0"

                val childLayoutManager = LinearLayoutManager(context!!, RecyclerView.HORIZONTAL, false)

                itemArticlePlacements.apply {
                    layoutManager = childLayoutManager
                    adapter = PlacementAdapter(sale.placements,onPasillerosItemClickListener)
                }

                //CLICK EVENTS
                contentLayout.setOnClickListener {
                    onSaleClickListener.onSaleClick(sale)
                }

                itemArticleItemFk.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(PasillerosItemVO(title = binding.root.context.resources.getString(R.string.titleItemConsult)),sale.itemFk)
                }

                itemArticleQuantity.setOnClickListener {
                    onQuantityClick.onQuantityClick(sale)
                }

                itemArticleQuantityPicked.setOnClickListener {
                    onQuantityClick.onQuantityClick(sale)
                }

                if (sale.quantity == sale.pickedQuantity){
                    sale.isPreviousPrepared = "1"
                }else{
                    sale.isPreviousPrepared = "0"
                }

                //SEMAFORO
                if (sale.isPreviousPrepared == "1"){
                    itemArticleRowSemaforoPre.setBackgroundColor(ContextCompat.getColor(context!!, R.color.verdnatura_dark_sky_blue))
                }else{
                    itemArticleRowSemaforoPre.setBackgroundColor(ContextCompat.getColor(context!!, R.color.verdnatura_warm_grey))
                }



                if (sale.isPreviousPrepared == "1"){
                    contentLayout.setBackgroundColor(ContextCompat.getColor(context!!, R.color.verdnatura_dark_sky_blue))
                }else{
                    contentLayout.setBackgroundColor(ContextCompat.getColor(context!!, R.color.verdnatura_black))
                }


                //ASIGNAMOS VALOR A LA VSITA
                this.sale = sale

            }
        }
    }
}