package es.verdnatura.presentation.view.feature.ubicador.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.ItemUbicationRowNewBinding
import es.verdnatura.presentation.common.OnMoreClickListenerNew
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.common.OnVisibleClickListenerNew
import es.verdnatura.presentation.common.loadUrl
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import es.verdnatura.presentation.view.feature.ubicador.model.ItemUbicador

class UbicadorAdapterNew(
    private val items: List<ItemUbicador>,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener,
    private val onVisibleClickListener: OnVisibleClickListenerNew,
    private val onMoreClickListener: OnMoreClickListenerNew
) : RecyclerView.Adapter<UbicadorAdapterNew.ItemHolder>() {

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

    override fun getItemCount() = items.size

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

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

        @SuppressLint("SetTextI18n")
        fun bind(item: ItemUbicador) {
            binding.apply {
                this.item = item
                multiText.text = "${item.stickers}x${item.packing}"
                imgItem.loadUrl("http:" + item.url + "/catalog/200x200/" + item.item)
                imgItem.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(
                        PasillerosItemVO(
                            title =
                                R.string.itemSearch

                        ), item.item.toString()
                    )
                }

                layerVisible.setOnClickListener {
                    onVisibleClickListener.onVisibleClickListener(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)
                }
                namelayout.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(
                        PasillerosItemVO(
                            title = R.string.itemSearch
                        ), item.item.toString()
                    )
                }

                if (item.isChecked != null) {

                    when (item.isChecked!!.toInt()) {
                        0 -> itemRowLayout.setBackgroundColor(
                            ContextCompat.getColor(
                                visibleText.context,
                                R.color.verdnatura_red_salix
                            )
                        )

                        in 1..Int.MAX_VALUE ->
                            if (item.visible == 0) {
                                itemRowLayout.setBackgroundColor(
                                    ContextCompat.getColor(
                                        visibleText.context,
                                        R.color.verdnatura_dark_sky_blue
                                    )
                                )
                            } else {
                                itemRowLayout.setBackgroundColor(
                                    ContextCompat.getColor(
                                        visibleText.context,
                                        R.color.verdnatura_dark_green_verdnatura
                                    )
                                )
                            }

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

            }
        }
    }
}