package com.example.pantallacompra

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase

class ChairAdapter (private val item : ArrayList<Asiento>): RecyclerView.Adapter<ChairAdapter.ViewHolder>()  {

    private lateinit var mListener : onItemClickListener
    private lateinit var database: FirebaseDatabase
    //private lateinit var myRef: DatabaseReference
    private val selectedItems = HashSet<Int>()
    private val occupiedSeats = ArrayList<String>()

    fun toggleSelection(position: Int) {

        var asiento=item[position].noAsiento.toString()
        database = FirebaseDatabase.getInstance()
        var myRef = database.getReference("Asientos/$asiento/status")

        val asientoprueba = item[position]
        val asientoNo = asientoprueba.noAsiento.toString()

        if (selectedItems.contains(position)) {
            actulizarValor("Libre", myRef)
            selectedItems.remove(position)
            occupiedSeats.remove(asientoNo)
        } else {
            actulizarValor("Ocupado", myRef)
            selectedItems.add(position)
            occupiedSeats.add(asientoNo)
        }
        notifyItemChanged(position)
    }

    interface onItemClickListener{
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: onItemClickListener){
        mListener=listener
    }

    inner class  ViewHolder(itemView: View, listener: onItemClickListener): RecyclerView.ViewHolder(itemView){
        val chair : TextView = itemView.findViewById(R.id.chair)
        init {
            itemView.setOnClickListener{
                val position=adapterPosition
                if(position!=RecyclerView.NO_POSITION){
                    toggleSelection(position)
                    notifyDataSetChanged()
                }
            }
        }
        fun bind(position: Int){
            val item = item[position]
            chair.text= item.noAsiento.toString()

            if (selectedItems.contains(position)) {
                itemView.setBackgroundColor(Color.RED)
            } else {
                itemView.setBackgroundColor(Color.parseColor("#4a8a9e"))
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.chair_item,parent,false),mListener)
    }

    override fun getItemCount(): Int {
        return item.size
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.chair.text = item[position].noAsiento.toString()
        holder.bind(position)
    }
    fun actulizarValor(status : String, reference: DatabaseReference){
        reference.setValue(status)
    }

    fun getOccupiedSeats(): List<String> {
        return occupiedSeats
    }
}