extends Control

@export var lobby_count_label: Label
@export var PlayerNameContainer: VBoxContainer
@export var playername_array: Array[String] = []


func _ready() -> void:
	$HBoxContainer/VBoxContainer/Host.pressed.connect(host_game)
	$HBoxContainer/VBoxContainer3/Button.pressed.connect(join_game)


func join_game():
	var lobby_id = $HBoxContainer/VBoxContainer3/TextEdit.text
	
	if int(lobby_id) > 0:
		SteamManager.join_lobby(int(lobby_id))


func host_game():
	SteamManager.create_lobby()


func add_new_player(playername: String, total_player_count: int):
	var new_playername_label = Label.new()
	new_playername_label.text = playername
	new_playername_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	new_playername_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
	new_playername_label.name = playername
	playername_array.append(playername)
	PlayerNameContainer.add_child(new_playername_label)
	lobby_count_label.text = "Players in lobby " + str(total_player_count) + "/4"


func delete_player(playername: String, total_player_count: int):
	for playername_index in playername_array:
		if playername_index == playername:
			playername_array.erase(playername_index)
			var playername_node_for_deletion = PlayerNameContainer.find_child(playername)
			playername_node_for_deletion.queue_free()
	lobby_count_label.text = "Players in lobby " + str(total_player_count) + "/4"
