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

    import android.content.Context
    import android.graphics.BlendMode
    import android.graphics.BlendModeColorFilter
    import android.graphics.Color
    import android.graphics.PorterDuff
    import android.os.Build
    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.databinding.ItemArticleRowReubicationFragmentBinding
    import es.verdnatura.presentation.common.*
    import es.verdnatura.presentation.view.feature.collection.adapter.PlacementAdapter
    import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
    import es.verdnatura.presentation.view.feature.reubication.model.PlacementReubication
    import es.verdnatura.presentation.view.feature.reubication.model.Reubication
    import es.verdnatura.presentation.view.feature.sacador.model.SaleVO


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

                ItemArticleRowReubicationFragmentBinding.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: ItemArticleRowReubicationFragmentBinding
        ) : RecyclerView.ViewHolder(binding.root) {
            fun bind(reubication: Reubication) {
                binding.apply {

                    itemArticleItemFk.setOnClickListener {
                        onPasillerosItemClickListener.onPasillerosItemClickListener(
                            PasillerosItemVO(
                                title = binding.root.context.getString(R.string.titleItemConsult)
                            ), reubication.itemFk
                        )}
                    contentLayout.setOnClickListener {
                        onReubicationClickListener.onReubicationClick(reubication)
                    }
                  /*  if (sale.pickedQuantity.isNullOrEmpty())
                        sale.pickedQuantity = "0"
                    */
                    val childLayoutManager =
                        LinearLayoutManager(context!!, RecyclerView.HORIZONTAL, false)

                    itemArticlePlacements.apply {
                        layoutManager = childLayoutManager
                        adapter = reubication.carros.let { ReubicatorPlacementAdapter(it, onPasillerosItemClickListener) }
                    }

                    //CLICK EVENTS
                  /*

                    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(
                            ContextCompat.getColor(
                                context!!,
                                R.color.verdnatura_dark_sky_blue
                            )
                        )
                    } else {
                        itemArticleRowSemaforoPre.setBackgroundColor(
                            ContextCompat.getColor(
                                context!!,
                                R.color.verdnatura_warm_grey
                            )
                        )
                    }

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



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


                    if (sale.isControlled == "1") {
                        contentLayout.setBackgroundColor(
                            ContextCompat.getColor(
                                context!!,
                                R.color.verdnatura_dark_mint
                            )
                        )
                    } else if (sale.isPrepared == "1") {
                        contentLayout.setBackgroundColor(
                            ContextCompat.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(
                            ContextCompat.getColor(
                                context!!,
                                R.color.verdnatura_dark_mint_light_precontrolled
                            )
                        )
                        itemArticleRowSemaforoCon.setBackgroundColor(
                            ContextCompat.getColor(
                                context!!,
                                R.color.verdnatura_dark_mint_light_precontrolled
                            )
                        )
                    } else 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
                            )
                        )
                    }


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


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

                }
            }
        }


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


            when (color.uppercase().trim()) {
                "ROJO" -> 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)
                }
                // itemView.background.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP)

                "AMARILLO" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(Color.YELLOW, BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP)
                }
                "VERDE" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(Color.GREEN, BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP)
                }
                "AZUL" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(Color.BLUE, BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP)
                }
                "BLANCO" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(Color.WHITE, BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
                }
                // "NEGRO"-> itemView.setBackgroundResource((R.drawable.rectangle))

                "NEGRO" -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        itemView.background.colorFilter =
                            BlendModeColorFilter(Color.BLACK, BlendMode.SRC_ATOP)
                    } else {
                        itemView.background.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP)
                    }

                    itemView.setBackgroundResource((R.drawable.border))
                }
                "VIOLETA" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFF4c2882.toInt(), BlendMode.SRC_ATOP)
                    // BlendModeColorFilter(Color.YELLOW, BlendMode.SRC_ATOP)
                    //Log.d("VERDNATURA::", "EL COLOR ES " + color)
                } else {
                    itemView.background.setColorFilter(0xFF4c2882.toInt(), PorterDuff.Mode.SRC_ATOP)
                }

                "GRIS" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(Color.GRAY, BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_ATOP)
                }
                "ORANGE" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(
                            ContextCompat.getColor(
                                context!!,
                                android.R.color.holo_orange_light
                            ), BlendMode.SRC_ATOP
                        )
                } else {
                    itemView.background.setColorFilter(
                        ContextCompat.getColor(
                            context!!,
                            android.R.color.holo_orange_light
                        ), PorterDuff.Mode.SRC_ATOP
                    )
                }

                "MORADO" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(
                            ContextCompat.getColor(
                                context!!,
                                android.R.color.holo_purple
                            ), BlendMode.SRC_ATOP
                        )
                } else {
                    itemView.background.setColorFilter(
                        ContextCompat.getColor(
                            context!!,
                            android.R.color.holo_purple
                        ), PorterDuff.Mode.SRC_ATOP
                    )
                }

                "CELESTE" ->if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFF5b85F5.toInt(), BlendMode.SRC_ATOP)
                }


                "ROSA" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFFfc0fc0.toInt(), BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(
                        0xFFfc0fc0.toInt(),
                        PorterDuff.Mode.SRC_ATOP
                    )
                }

                "DORADO" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFFb8860b.toInt(), BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(
                        0xFFb8860b.toInt(),
                        PorterDuff.Mode.SRC_ATOP
                    )
                }
                "MARRON" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFF663300.toInt(), BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(
                        0xFF663300.toInt(),
                        PorterDuff.Mode.SRC_ATOP
                    )
                }
                "PERLA" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFFcdcecf.toInt(), BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(
                        0xFFcdcecf.toInt(),
                        PorterDuff.Mode.SRC_ATOP
                    )
                }
                "MARFIL" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFFf7eada.toInt(), BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(
                        0xFFf7eada.toInt(),
                        PorterDuff.Mode.SRC_ATOP
                    )
                }
                "CIAN" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFF00ffff.toInt(), BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(
                        0xFF00ffff.toInt(),
                        PorterDuff.Mode.SRC_ATOP
                    )
                }
                "BEIGE" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    itemView.background.colorFilter =
                        BlendModeColorFilter(0xFFf2e7bf.toInt(), BlendMode.SRC_ATOP)
                } else {
                    itemView.background.setColorFilter(
                        0xFFf2e7bf.toInt(),
                        PorterDuff.Mode.SRC_ATOP
                    )
                }

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


        }*/
    }