package es.verdnatura.presentation.view.feature.delivery.adapters

import android.graphics.Color
import android.graphics.PorterDuff
import android.view.LayoutInflater
import android.view.View.INVISIBLE
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemTicketRowBinding
import es.verdnatura.presentation.common.OnItemButtonCMRRowClickListener
import es.verdnatura.presentation.common.OnItemButtonTicketRowClickListener
import es.verdnatura.presentation.view.feature.delivery.model.ClientTicketSalix

class TicketAdapter(
    private var items: MutableList<ClientTicketSalix>,
    private val onItemButtonTicketRowClickListener: OnItemButtonTicketRowClickListener,
    private val onItemButtonCMRRowClickListener: OnItemButtonCMRRowClickListener

) : RecyclerView.Adapter<TicketAdapter.ItemHolder>() {

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

    fun setHeaders(list: MutableList<ClientTicketSalix>): MutableList<ClientTicketSalix> {
        return list
    }

    fun updateList(newList: MutableList<ClientTicketSalix>) {
        items = newList
        notifyDataSetChanged()
    }

    override fun getItemCount() = items.size

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {

        holder.bind(items[position])
        val viewTickets = holder.itemView.findViewById<LinearLayout>(R.id.layoutTickets)
        viewTickets.removeAllViews()

        // Inflate and add views for each ticket
        val inflater = LayoutInflater.from(holder.itemView.context)
        items[position].tickets.forEach { ticket ->
            val v = inflater.inflate(R.layout.item_ticket_sub_row, null)
            val txtTicket = v.findViewById<TextView>(R.id.txtTicket)
            val txtPackage = v.findViewById<TextView>(R.id.packages)
            val txtPackingType = v.findViewById<TextView>(R.id.packingType)
            val txtCmr = v.findViewById<TextView>(R.id.btnCmr)
            if (ticket.cmrFk == null) {
                txtCmr.visibility = INVISIBLE
            }
            txtCmr.setOnClickListener {
                onItemButtonCMRRowClickListener.onItemButtonCMRRowClickListener(ticket)
            }
            txtTicket.text = ticket.Id.toString()
            txtPackage.text = ticket.Packages.toString()
            txtPackingType.text = ticket.PackingType

            viewTickets.addView(v)
        }
    }

    inner class ItemHolder(
        val binding: ItemTicketRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        private val res = binding.root.context.resources
        fun bind(item: ClientTicketSalix) {
            binding.apply {

                if (!item.observationDelivery.isNullOrEmpty() && !item.observationDelivery!!.toString().contains("null")){
                    note.setColorFilter(
                        ContextCompat.getColor(
                            note.context, R.color.verdnatura_pumpkin_orange
                        )
                    )
                    note.alpha = 1.0f
                    note.setOnClickListener {
                        onItemButtonTicketRowClickListener.onItemButtonTicketRowClickListener(
                            item, "note"
                        )
                    }
                } else {
                    note.alpha = 0.5f
                    note.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
                    note.isClickable = false

                }

                addNote.setOnClickListener {
                    onItemButtonTicketRowClickListener.onItemButtonTicketRowClickListener(
                        item, "addNote"
                    )
                }
                phone.setOnClickListener {
                    onItemButtonTicketRowClickListener.onItemButtonTicketRowClickListener(
                        item, "call"
                    )
                }
                map.setOnClickListener {
                    onItemButtonTicketRowClickListener.onItemButtonTicketRowClickListener(
                        item, "map"
                    )
                }
                unload.setOnClickListener {
                    onItemButtonTicketRowClickListener.onItemButtonTicketRowClickListener(
                        item, "unload"
                    )
                }

                this.item = item

            }
        }
    }
}
