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

import android.os.Build
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemShelvingdayofsaleRowBinding
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.view.feature.diadeventa.model.ItemShelvingSaleDate
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import java.text.SimpleDateFormat
import java.util.Date

class DayOfSaleAdapter(
    private val items: List<ItemShelvingSaleDate>,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener?
) : RecyclerView.Adapter<DayOfSaleAdapter.ItemHolder>() {


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

    override fun getItemCount() = items.size

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.bind(items[position])
    }

    inner class ItemHolder(
        val binding: ItemShelvingdayofsaleRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        private val res = binding.root.context.resources
        @RequiresApi(Build.VERSION_CODES.O)
        fun bind(item: ItemShelvingSaleDate) {
            binding.apply {
                this.item = item
                val sdf = SimpleDateFormat(res.getString(R.string.dateFormat))
                val currentDate = sdf.format(Date())
                if (currentDate == item.dated) {
                    saledate.setBackgroundColor(
                        ContextCompat.getColor(
                            historicoDate.context,
                            R.color.verdnatura_pumpkin_orange
                        )
                    )
                } else {
                    saledate.setBackgroundColor(
                        ContextCompat.getColor(
                            historicoDate.context,
                            R.color.verdnatura_black_5
                        )
                    )
                }
                historicoDate.setOnClickListener {
                    onPasillerosItemClickListener!!.onPasillerosItemClickListener(
                        PasillerosItemVO(
                            title = res.getString(
                                R.string.titleItemConsult
                            )
                        ), item.itemFk.toString()
                    )
                }

            }
        }
    }
}
