package com.example.mathseduc

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.mathseduc.controllers.ControllerPlayer
import com.example.mathseduc.models.Player

class ServerDetailsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_server_details)

        val serverName = intent.getStringExtra("serverName")

        // Now you can use the data as needed, for example, display it in a TextView
        val serverNameTextView = findViewById<TextView>(R.id.titleServerDetails)
        serverNameTextView.text = serverName

        val lobbyId = intent.getIntExtra("lobbyId", -1)
        val playerIds = ControllerPlayer.getPlayersIdFromLobbyId(lobbyId)
        if (playerIds != null) {
            val playerList = playerIds.mapNotNull { playerId ->
                ControllerPlayer.getPlayerInfoById(playerId.toString())
            }
            val listViewPlayers = findViewById<ListView>(R.id.listViewPlayers)
            val playerAdapter = PlayerAdapter(this, playerList)
            listViewPlayers.adapter = playerAdapter
        }
    }

    class PlayerAdapter(context: Context, players: List<Player>) :
        ArrayAdapter<Player>(context, 0, players) {

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            var currentPlayerView = convertView
            if (currentPlayerView == null) {
                currentPlayerView =
                    LayoutInflater.from(context).inflate(R.layout.list_view_player, parent, false)
            }

            val playerNameTextView = currentPlayerView!!.findViewById<TextView>(R.id.playerName)
            playerNameTextView.text = getItem(position)?.nickname ?: "Unknown"

            // You can customize the view further if needed

            return currentPlayerView
        }

    }
}