package com.example.pantallacompra

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener

class ChairList : AppCompatActivity() {
    private lateinit var dbreference : DatabaseReference
    private lateinit var recycleView: RecyclerView
    private lateinit var eventList : ArrayList<Asiento>
    // private lateinit var chairs : ArrayList<String>
    //var arraySillas = arrayListOf<String>()
    var total : Int =0

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chair_list)

        //SE GUARDA EL NOMBRE DEL EVENTO
        val bundle= intent.extras
        val nombreEvento=bundle?.getString("nombreEvento")
        val noSala=bundle?.getString("sala")
        val btnComprar : Button = findViewById(R.id.btnConfirmar)
        val btnSalir : Button = findViewById(R.id.btncancelar)

        val nEvento:TextView =findViewById(R.id.eventoText)
        nEvento.text=nombreEvento

        val nSala:TextView=findViewById(R.id.noSalaText)
        nSala.text=noSala

        recycleView = findViewById(R.id.chairRecycleView)
        recycleView.layoutManager = GridLayoutManager(this,2)
        recycleView.setHasFixedSize(true)

        eventList = arrayListOf<Asiento>()
        //  chairs= arrayListOf<String>()

        getEvents()
        btnComprar.setOnClickListener{
            val adapter = recycleView.adapter as ChairAdapter
            val asientosOcupados = adapter.getOccupiedSeats()
            val intent = Intent(this, compra::class.java)
            intent.putStringArrayListExtra("asientosSeleccionados", ArrayList(asientosOcupados))
            startActivity(intent)

            val mensajeToast = if (asientosOcupados.isNotEmpty()) {
                "Asientos ocupados: ${asientosOcupados.joinToString(", ")}"
            } else {
                "No hay asientos ocupados."
            }
            Toast.makeText(this@ChairList, mensajeToast, Toast.LENGTH_LONG).show()
        }

        btnSalir.setOnClickListener{
            val intent = Intent(this, EventList::class.java)
            startActivity(intent)
        }

    }
    private fun getEvents(){
        dbreference= FirebaseDatabase.getInstance().getReference("Asientos")
        dbreference.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                if(snapshot.exists()){

                    for(event in snapshot.children){
                        val item=event.getValue(Asiento::class.java)
                        eventList.add(item!!)
                    }
                    var adapter=ChairAdapter(eventList)
                    recycleView.adapter=adapter

                    adapter.setOnItemClickListener(object : ChairAdapter.onItemClickListener{
                        override fun onItemClick(position: Int) {
                            adapter.toggleSelection(position)
                        }

                    })
                    // total= adapter.getTotal()

                }
            }

            override fun onCancelled(error: DatabaseError) {
                TODO("Not yet implemented")
            }

        })
    }


}