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

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat.getColor
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemArticleRowFragmentBinding
import es.verdnatura.presentation.common.*
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import es.verdnatura.presentation.view.feature.sacador.model.SaleVO


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

            ItemArticleRowFragmentBinding.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: ItemArticleRowFragmentBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(sale: SaleVO) {
            binding.apply {
                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.getString(R.string.titleItemConsult)
                        ), sale.itemFk
                    )
                }

                itemPackingItemFk.setOnClickListener {
                    onPackingClick.onPackingClick(sale)
                }

                itemArticleQuantity.setOnClickListener {
                    onQuantityClick.onQuantityClick(sale)
                }

                itemArticleQuantityPicked.setOnClickListener {
                    onQuantityClick.onQuantityClick(sale)
                }

                txtde.setOnClickListener {
                    onQuantityClick.onQuantityClick(sale)
                }

                contentLayout.setOnLongClickListener {
                    //"Nueva funcionalidad en el icono de la derecha".toast(context!!)
                    //onMistakeClickListener.onMistakeClickListener(sale)
                    false
                }

                /*itemRowLayout.setOnLongClickListener{
                    onMistakeClickListener.onMistakeClickListener(sale)
                    false
                }*/
                imageErrorMessage.setOnClickListener {
                    onMistakeClickListener.onMistakeClickListener(sale)
                    false
                }


                //ERROR
                if (sale.originalQuantity != sale.quantity) {
                    layoutError.visibility = View.VISIBLE
                    txtError.text =
                        binding.root.context.getString(R.string.originalQuantity) + sale.originalQuantity
                    if (sale.isPrepared == "1" || sale.isControlled == "1")
                        sale.pickedQuantity = sale.quantity
                }

                if (sale.isNew) {
                    layoutError.visibility = View.VISIBLE
                    txtError.text = binding.root.context.getString(R.string.newItem)

                }

                paintTicketcolor(sale.level, itemTicketColor)
                //paintTicketcolor(sale.color, itemColor)


                if (!sale.isNew && sale.originalQuantity == sale.quantity) {
                    layoutError.visibility = View.GONE
                }


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

                if (sale.isPrepared == "1") {
                    itemArticleRowSemaforoSac.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_pumpkin_orange
                        )
                    )
                } else {
                    itemArticleRowSemaforoSac.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_warm_grey
                        )
                    )
                }



                if (sale.isControlled == "1") {
                    itemArticleRowSemaforoCon.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_dark_mint
                        )
                    )
                } else {
                    itemArticleRowSemaforoCon.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_warm_grey
                        )
                    )
                }


                if (sale.isControlled == "1") {
                    contentLayout.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_dark_mint
                        )
                    )
                } else if (sale.isPrepared == "1") {
                    contentLayout.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_pumpkin_orange
                        )
                    )
                } /*else if (sale.isPreviousPrepared == "1") {
                    contentLayout.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_dark_sky_blue
                        )
                    )
                }*/ else if (sale.isPreControlled == "1") {

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


                //sergio: para de momento que pinten las lineas sin actualizar ni nada
                if (sale.isControlled == "2") {
                    itemArticleRowSemaforoCon.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_white
                        )
                    )
                    contentLayout.setBackgroundColor(
                        getColor(
                            context!!,
                            R.color.verdnatura_dark_mint_light
                        )
                    )
                }


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

            }
        }
    }

    /* private fun paintTicketcolorOld(color: String, itemView: View) {
         when (color.uppercase()) {
             "ROJO" -> itemView.setBackgroundColor(Color.RED)
             "AMARILLO" -> itemView.setBackgroundColor(Color.YELLOW)
             "VERDE" -> itemView.setBackgroundColor(Color.GREEN)
             "AZUL" -> itemView.setBackgroundColor(Color.BLUE)
             "BLANCO" -> {
                 itemView.setBackgroundColor(Color.WHITE)
                 itemView.setBackgroundResource((R.drawable.border))
             }
             "NEGRO" -> {
                 itemView.setBackgroundColor(Color.BLACK)
             }
             "VIOLETA" -> itemView.setBackgroundColor(0xFF4c2882.toInt())
             "GRIS" -> itemView.setBackgroundColor(Color.GRAY)
             "ORANGE" -> itemView.setBackgroundColor(
                 getColor(
                     context!!,
                     android.R.color.holo_orange_light
                 )
             )
             "MORADO" -> itemView.setBackgroundColor(
                 getColor(
                     context!!,
                     android.R.color.holo_purple
                 )
             )
             "CELESTE" -> itemView.setBackgroundColor(0xFF5b85F5.toInt())
             "ROSA" -> itemView.setBackgroundColor(0xFFfc0fc0.toInt())
             "DORADO" -> itemView.setBackgroundColor(0xFFb8860b.toInt())
             "MARRON" -> itemView.setBackgroundColor(0xFF663300.toInt())
             "PERLA" -> itemView.setBackgroundColor(0xFFcdcecf.toInt())
             "MARFIL" -> itemView.setBackgroundColor(0xFFf7eada.toInt())
             "CIAN" -> itemView.setBackgroundColor(0xFF00ffff.toInt())
             "BEIGE" -> itemView.setBackgroundColor(0xFFf2e7bf.toInt())
             "transparente" -> itemView.setBackgroundColor(0xFFf2e7bf.toInt())

             else -> {
                 itemView.setBackgroundColor(Color.TRANSPARENT)
                 itemView.setBackgroundResource(R.drawable.background_item_color)

             }
         }
     }*/

    private fun paintTicketcolor(color: String, itemView: View) {


        when (color.uppercase().trim()) {
            "ROJO" ->
                itemView.setBackgroundColor(Color.RED)
            /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            itemView.background.colorFilter =
                BlendModeColorFilter(Color.RED, BlendMode.SRC_ATOP)
        } else {
            itemView.background.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP)
        }*/

            "AMARILLO" ->
                itemView.setBackgroundColor(Color.YELLOW)

            "VERDE" -> itemView.setBackgroundColor(Color.GREEN)

            "AZUL" -> itemView.setBackgroundColor(Color.BLUE)

            "BLANCO" -> itemView.setBackgroundColor(Color.WHITE)

            "NEGRO" -> {

                itemView.setBackgroundColor(Color.BLACK)
                itemView.setBackgroundResource((R.drawable.border))
            }
            "VIOLETA" -> itemView.setBackgroundColor(0xFF4c2882.toInt())

            "GRIS" -> itemView.setBackgroundColor(Color.GRAY)

            "ORANGE" -> itemView.setBackgroundColor(0xFFFFBB33.toInt())

            "MORADO" -> itemView.setBackgroundColor(0xFFAA66CC.toInt())

            "CELESTE" -> itemView.setBackgroundColor(0xFF5b85F5.toInt())

            "ROSA" -> itemView.setBackgroundColor(0xFFfc0fc0.toInt())

            "DORADO" -> itemView.setBackgroundColor(0xFFb8860b.toInt())

            "MARRON" -> itemView.setBackgroundColor(0xFF663300.toInt())

            "PERLA" -> itemView.setBackgroundColor(0xFFcdcecf.toInt())

            "MARFIL" -> itemView.setBackgroundColor(0xFFf7eada.toInt())

            "CIAN" -> itemView.setBackgroundColor(0xFF00ffff.toInt())


            "BEIGE" -> itemView.setBackgroundColor(0xFFf2e7bf.toInt())

            else -> {
                itemView.setBackgroundColor(Color.TRANSPARENT)
                //itemView.setBackgroundResource(R.drawable.background_item_color)
                //  itemView.visibility=View.INVISIBLE
            }
        }
        // itemTicketColor.setBackgroundResource((R.drawable.rectangle))


    }
}