package com.example.tolucagraphics.adapters

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.tolucagraphics.R
import com.example.tolucagraphics.datas.DataJT
import com.example.tolucagraphics.datas.JoinTable
import com.example.tolucagraphics.events.EventCreateMesa

class MesaAdapter(val mesasCreadas: ArrayList<EventCreateMesa>, var callback: (String, JoinTable) -> Unit) : RecyclerView.Adapter<MesaAdapter.JugadoresHolder>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): JugadoresHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.modelo_recycler_mesas, parent, false)
        return JugadoresHolder(view)
    }

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

    override fun onBindViewHolder(holder: JugadoresHolder, position: Int) {
        holder.binding(mesasCreadas[position])
        holder.itemView.findViewById<Button>(R.id.campoMesaacc).setOnClickListener {
            val joinTable = JoinTable()
            joinTable.data?.tableID = mesasCreadas[position].data?.table?.id
            Log.d("TAG", "LO QUE TIENE TABLA ID ${mesasCreadas[position].data?.table?.id}")
            callback("Con mucho amor Hasta aqui llegue ♥", joinTable)
        }
    }

    class JugadoresHolder(view: View):RecyclerView.ViewHolder(view){
        var id= view.findViewById<TextView>(R.id.campoMesaId)
        var st= view.findViewById<TextView>(R.id.campoMesaEstado)
        var s1= view.findViewById<TextView>(R.id.campoMesaS1)
        var s2= view.findViewById<TextView>(R.id.campoMesaS2)
        var s3= view.findViewById<TextView>(R.id.campoMesaS3)
        var s4= view.findViewById<TextView>(R.id.campoMesaS4)
        var s5= view.findViewById<TextView>(R.id.campoMesaS5)
        var s6= view.findViewById<TextView>(R.id.campoMesaS6)

        fun binding(userElementJoin: EventCreateMesa) {
            id.text = userElementJoin.data?.table?.id.toString()
            st.text = userElementJoin.data?.table?.status.toString()
            /*
            s1.text = if(userElementJoin.data?.table?.users?.get(0)?.username !=null) userElementJoin.data.table.users?.get(0)?.username else "-"
            s2.text = if(userElementJoin.data?.table?.users?.get(1)?.username !=null) userElementJoin.data.table.users?.get(1)?.username else "-"
            s3.text = if(userElementJoin.data?.table?.users?.get(2)?.username !=null) userElementJoin.data.table.users?.get(2)?.username else "-"
            s4.text = if(userElementJoin.data?.table?.users?.get(3)?.username !=null) userElementJoin.data.table.users?.get(3)?.username else "-"
            s5.text = if(userElementJoin.data?.table?.users?.get(4)?.username !=null) userElementJoin.data.table.users?.get(4)?.username else "-"
            s6.text = if(userElementJoin.data?.table?.users?.get(5)?.username !=null) userElementJoin.data.table.users?.get(5)?.username else "-"*/
            s1.text = "-"
            s2.text = "-"
            s3.text = "-"
            s4.text = "-"
            s5.text = "-"
            s6.text = "-"
        }
    }
}