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

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemBuyerRowBinding
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.common.OnStarSelectedListener
import es.verdnatura.presentation.common.loadUrl
import es.verdnatura.presentation.view.feature.calidad.model.ItemBuyerVO
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO

class ItemBuyersAdapter(
    private val items: List<ItemBuyerVO>,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener,
    private val onStarSelectedListener: OnStarSelectedListener
) : RecyclerView.Adapter<ItemBuyersAdapter.BuyersItemHolder>() {


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

    override fun getItemCount() = items.size

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

    }


    inner class BuyersItemHolder(
        val binding: ItemBuyerRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: ItemBuyerVO) {
            binding.apply {
                this.itemBuyer = item
                if (item.image != "")
                    imgItem.loadUrl("https://verdnatura.es/vn-image-data/catalog/200x200/" + item.image)
                else
                    imgItem.loadUrl("https://verdnatura.es/vn-image-data/catalog/200x200/" + item.itemFk)

                imgItem.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(
                        PasillerosItemVO(
                            title = binding.root.context.getString(
                                R.string.titleItemConsult
                            )
                        ), item.itemFk
                    )
                }

                if (item.showStars) layoutStars.visibility = View.VISIBLE
                else layoutStars.visibility = View.GONE


                s1.setOnClickListener {
                    onStarSelectedListener.onStarSelected(item.id, "1")
                }

                s2.setOnClickListener {
                    onStarSelectedListener.onStarSelected(item.id, "2")
                }

                s3.setOnClickListener {
                    onStarSelectedListener.onStarSelected(item.id, "3")
                }

                s4.setOnClickListener {
                    onStarSelectedListener.onStarSelected(item.id, "4")
                }

                s5.setOnClickListener {
                    onStarSelectedListener.onStarSelected(item.id, "5")
                }

                if (item.stars != "") {
                    try {
                        val punt = item.stars.toInt()
                        if (punt == 1) {
                            s1.setImageResource(item.iconResourceOk)
                            s2.setImageResource(item.iconResourceKo)
                            s3.setImageResource(item.iconResourceKo)
                            s4.setImageResource(item.iconResourceKo)
                            s5.setImageResource(item.iconResourceKo)
                        } else if (punt == 2) {
                            s1.setImageResource(item.iconResourceOk)
                            s2.setImageResource(item.iconResourceOk)
                            s3.setImageResource(item.iconResourceKo)
                            s4.setImageResource(item.iconResourceKo)
                            s5.setImageResource(item.iconResourceKo)
                        } else if (punt == 3) {
                            s1.setImageResource(item.iconResourceOk)
                            s2.setImageResource(item.iconResourceOk)
                            s3.setImageResource(item.iconResourceOk)
                            s4.setImageResource(item.iconResourceKo)
                            s5.setImageResource(item.iconResourceKo)
                        } else if (punt == 4) {
                            s1.setImageResource(item.iconResourceOk)
                            s2.setImageResource(item.iconResourceOk)
                            s3.setImageResource(item.iconResourceOk)
                            s4.setImageResource(item.iconResourceOk)
                            s5.setImageResource(item.iconResourceKo)
                        } else if (punt == 5) {
                            s1.setImageResource(item.iconResourceOk)
                            s2.setImageResource(item.iconResourceOk)
                            s3.setImageResource(item.iconResourceOk)
                            s4.setImageResource(item.iconResourceOk)
                            s5.setImageResource(item.iconResourceOk)
                        } else {
                            s1.setImageResource(item.iconResourceOk)
                            s2.setImageResource(item.iconResourceOk)
                            s3.setImageResource(item.iconResourceOk)
                            s4.setImageResource(item.iconResourceOk)
                            s5.setImageResource(item.iconResourceOk)
                        }
                    } catch (e: Exception) {
                    }
                }
            }
        }


    }
}