package es.verdnatura.presentation.view.feature.historicovehiculo.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.ItemHistoricovehiculoRowBinding
import es.verdnatura.presentation.view.feature.historicovehiculo.model.ItemHistoricoVehiculo
import java.text.SimpleDateFormat
import java.util.*



class HistoricoVehiculoAdapter  (
    private val items: List<ItemHistoricoVehiculo>
): RecyclerView.Adapter<HistoricoVehiculoAdapter.ItemHolder> () {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        return ItemHolder(
            ItemHistoricovehiculoRowBinding.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: ItemHistoricovehiculoRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        private val res = binding.root.context.resources
        fun bind(item: ItemHistoricoVehiculo) {
            binding.apply {
                this.item = item
                val sdf = SimpleDateFormat(binding.root.context.getString(R.string.dateFormat))
                val currentDate = sdf.format(Date())
                if (currentDate == item.inTimed){
                    historicoDate.setBackgroundColor(ContextCompat.getColor(historicoDate.context,R.color.verdnatura_pumpkin_orange))
                }else{
                    historicoDate.setBackgroundColor(ContextCompat.getColor(historicoDate.context,R.color.verdnatura_black_5))
                }
            }
        }
    }
}
