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

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemInventoryParkingRowBinding
import es.verdnatura.presentation.common.OnMoreClickListener
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.common.OnVisibleInventoryClickListener
import es.verdnatura.presentation.common.loadUrl
import es.verdnatura.presentation.view.feature.inventario.model.ItemInventoryParking
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO

class InventoryParkingAdapter(
    private var items: List<ItemInventoryParking>,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener,
    private val onVisibleInventoryClickListener: OnVisibleInventoryClickListener,
    private val onMoreClickListener: OnMoreClickListener
) : RecyclerView.Adapter<InventoryParkingAdapter.ItemHolder>() {

    fun getList(): List<ItemInventoryParking> {
        return items
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        return ItemHolder(
            ItemInventoryParkingRowBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun getItemCount() = items.size

    fun getListItems(): List<ItemInventoryParking> {
        return items
    }
    fun setItems(newItems: List<ItemInventoryParking>) {
        items = newItems
        notifyDataSetChanged() // Notificar al adaptador que los datos han cambiado
    }
    fun sortBy(fieldSort: String) {
        when (fieldSort) {
            "sector" -> {
                items = items.sortedBy { it.sectorFk }
            }

            "buyer" -> {
                items = items.sortedBy { it.buyer }
            }

            "pickingOrder" -> {
                items = items.sortedBy { it.pickingOrder }
            }
        }

        notifyDataSetChanged()
    }

    fun setData(newData: List<ItemInventoryParking>) {
        items = newData
        notifyDataSetChanged()
    }

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

    inner class ItemHolder(
        val binding: ItemInventoryParkingRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        private val res = binding.root.context.resources

        @SuppressLint("SetTextI18n")
        fun bind(item: ItemInventoryParking) {
            binding.apply {
                this.item = item
                imgItem.loadUrl(item.urlImage)
                imgItem.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(
                        PasillerosItemVO(
                            title = res.getString(
                                R.string.itemSearch
                            )
                        ), item.itemFk.toString()
                    )
                }

                layerVisible.setOnClickListener {
                    //  onVisibleClickListener.onVisibleClickListener(item)
                }

                visibleText.setOnClickListener {
                    onVisibleInventoryClickListener.onVisibleInventoryClickListener(item)
                }

                if (item.visible == 0) {
                    visibleText.setTextColor(
                        ContextCompat.getColor(
                            visibleText.context,
                            R.color.verdnatura_brown_grey
                        )
                    )

                } else {
                    visibleText.setTextColor(
                        ContextCompat.getColor(
                            visibleText.context,
                            R.color.verdnatura_pumpkin_orange
                        )
                    )
                }

                moreImg.setOnClickListener {
                    //  onMoreClickListener.onMoreClickListener(item)
                }

                itemShelving.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(
                        PasillerosItemVO(
                            title = res.getString(R.string.titleUbicator)
                        ), item.shelvingFk
                    )
                }
                itemfkText.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(
                        PasillerosItemVO(
                            title = res.getString(R.string.itemSearch)
                        ), item.itemFk.toString()
                    )
                }

                if (item.isChecked != null) {

                    when (item.isChecked!!.toInt()) {

                        in 1..Int.MAX_VALUE -> itemRowLayout.setBackgroundColor(
                            ContextCompat.getColor(
                                visibleText.context,
                                R.color.verdnatura_dark_green_verdnatura
                            )
                        )


                    }
                } else {
                    itemRowLayout.setBackgroundColor(
                        ContextCompat.getColor(
                            visibleText.context,
                            R.color.verdnatura_black_5
                        )
                    )
                }

            }
        }
    }
}