package es.verdnatura.presentation.view.feature.historicoarticulo.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.ItemHistoricoRowBinding
import es.verdnatura.presentation.common.OnClientHistoricItemClickListener
import es.verdnatura.presentation.view.feature.historicoarticulo.model.ItemHistoricoVO
import java.text.SimpleDateFormat
import java.util.Date

class HistoricoAdapter(
    private val items: List<ItemHistoricoVO>,
    private val onClientHistoricItemClickListener: OnClientHistoricItemClickListener
) : 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())

                historicoDate.setBackgroundColor(
                    ContextCompat.getColor(
                        historicoDate.context, if (currentDate == item.shipped) {
                            R.color.verdnatura_pumpkin_orange
                        } else {
                            R.color.verdnatura_black_5
                        }
                    )
                )

                val colorIn = if ((item.invalue ?: 0) > 0) {
                    R.color.verdnatura_dark_green_verdnatura
                } else {
                    R.color.verdnatura_black_5
                }
                inQuantity.setBackgroundColor(
                    ContextCompat.getColor(
                        historicoDate.context, colorIn
                    )
                )

                val colorOut = if ((item.out ?: 0) < 0) {
                    R.color.verdnatura_red_salix
                } else {
                    R.color.verdnatura_black_5
                }
                outQuantity.setBackgroundColor(
                    ContextCompat.getColor(
                        historicoDate.context, colorOut
                    )
                )

                paintClientName(item.stateName, binding)

                clientName.setOnClickListener {
                    onClientHistoricItemClickListener.onClientHistoricItemClickListener(item)
                }

            }
        }
    }

    private fun paintClientName(
        stateName: String?,
        binding: ItemHistoricoRowBinding,
    ) {


        when (stateName?.lowercase()) {
            "preparado", "en preparación", "autoimpreso", "impreso" -> {
                binding.clientName.setBackgroundColor(
                    ContextCompat.getColor(
                        binding.root.context, R.color.verdnatura_pumpkin_orange
                    )
                )
            }

            "encajando", "encajado", "reparto", "en reparto", "entregado" -> {
                binding.clientName.setBackgroundColor(
                    ContextCompat.getColor(
                        binding.root.context, R.color.verdnatura_red_salix
                    )
                )
            }

            "revisado", "en revisión" -> {
                binding.clientName.setBackgroundColor(
                    ContextCompat.getColor(
                        binding.root.context, R.color.verdnatura_dark_green_verdnatura
                    )
                )
            }

            "previa", "previa revisado", "previa OK", "previa revisando", "previa impreso" -> {
                binding.clientName.setBackgroundColor(
                    ContextCompat.getColor(
                        binding.root.context, R.color.verdnatura_pink_salix
                    )
                )
            }

            else -> {
                binding.clientName.setBackgroundColor(
                    ContextCompat.getColor(
                        binding.root.context, R.color.verdnatura_black_5
                    )
                )
            }
        }
    }
}