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

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.ItemShelvingdayofsaleRowBinding
import es.verdnatura.presentation.view.feature.diadeventa.model.ItemShelvingSaleDate
import java.text.SimpleDateFormat
import java.util.*

class DayOfSaleAdapter  (
        private val items: List<ItemShelvingSaleDate>
                                    ): 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

        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
            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))
                    }
                }
            }
        }
    }
