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

import android.view.LayoutInflater
import android.view.ViewGroup

import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemHistoricoRowBinding
import es.verdnatura.presentation.view.feature.historicoarticulo.model.ItemHistoricoVO
import androidx.core.content.ContextCompat
import java.text.SimpleDateFormat
import java.util.*

class HistoricoAdapter  (
    private val items: List<ItemHistoricoVO>
): RecyclerView.Adapter<HistoricoAdapter.ItemHolder> () {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        return ItemHolder(
            ItemHistoricoRowBinding.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: ItemHistoricoRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        private val res = binding.root.context.resources
        fun bind(item: ItemHistoricoVO) {
            binding.apply {
                this.item = item

                val sdf = SimpleDateFormat(res.getString(R.string.dateFormat))
                val currentDate = sdf.format(Date())
                if (currentDate == item.shipped){
                    //  historicoDate.setBackgroundColor(res.getColor(R.color.verdnatura_pumpkin_orange))
                    historicoDate.setBackgroundColor(ContextCompat.getColor(historicoDate.getContext(),R.color.verdnatura_pumpkin_orange))
                }else{
                    historicoDate.setBackgroundColor(ContextCompat.getColor(historicoDate.getContext(),R.color.verdnatura_black_5))
                }
            }
        }
    }
}