class_name LobbyMenu
extends Node


#region Onready Variables

@onready var lobby_container       : Container    = $LobbyContainer
@onready var username              : LineEdit     = $VBoxContainer/Name
@onready var lobby_label           : Label        = $VBoxContainer/HBoxContainer/LobbyLabel
@onready var steam_lobby_id_button : Button       = $VBoxContainer/HBoxContainer/SteamLobbyIdButton
@onready var disconnect_button     : Button       = $VBoxContainer/HBoxContainer/Disconnect
@onready var tab_container         : TabContainer = $VBoxContainer/TabContainer

@onready var steam_tab             : Control      = $VBoxContainer/TabContainer/Steam
@onready var steam_address_entry   : LineEdit     = $VBoxContainer/TabContainer/Steam/PanelContainer/MarginContainer/VBoxContainer/Address
@onready var steam_host_button     : Button       = $VBoxContainer/TabContainer/Steam/PanelContainer/MarginContainer/VBoxContainer/Host
@onready var steam_join_button     : Button       = $VBoxContainer/TabContainer/Steam/PanelContainer/MarginContainer/VBoxContainer/Join

@onready var enet_address_entry    : LineEdit     = $VBoxContainer/TabContainer/ENet/PanelContainer/MarginContainer/VBoxContainer/Address
@onready var enet_host_toggle      : CheckButton  = $VBoxContainer/TabContainer/ENet/PanelContainer/MarginContainer/VBoxContainer/HostToggleContainer/Host
@onready var enet_join_button      : Button       = $VBoxContainer/TabContainer/ENet/PanelContainer/MarginContainer/VBoxContainer/Join

#endregion


#region Variables

var steam_lobby_id : String = ""

#endregion


#region Signals

signal disconnect_button_pressed()

signal steam_host_pressed(new_username : String)
signal steam_join_pressed(new_username : String)

signal enet_host_toggled_on(new_username : String)
signal enet_host_toggled_off()
signal enet_join_pressed(new_username : String)

#endregion


#region Godot Functions

func _ready() -> void:
	#set username to steam username
	username.text = "username"

	disconnect_button.pressed.connect(_on_disconnet_button_pressed)

	#steam
	if Global.steam_connected == true:
		username.text = Steam.getPersonaName()

		steam_lobby_id_button.pressed.connect(_on_steam_lobby_id_button_pressed)
		steam_host_button.pressed.connect(_on_steam_host_pressed)
		steam_join_button.pressed.connect(_on_steam_join_pressed)

	#enet
	enet_host_toggle.toggled.connect(_on_enet_host_toggled)
	enet_join_button.pressed.connect(_on_enet_join_pressed)

	if Global.steam_connected == false:
		tab_container.remove_child(steam_tab)

#endregion


#region Public Functions

func reset_visible() -> void:
	steam_lobby_id_button.hide()
	disconnect_button.hide()
	clear_lobby_text()
	username.show()

	#steam
	steam_join_button.show()
	steam_address_entry.show()
	steam_host_button.show()

	#enet
	tab_container.tabs_visible = true
	tab_container.show()
	enet_address_entry.show()
	enet_join_button.show()

func request_lobby_list() -> void:
	for child in lobby_container.get_children():
		child.queue_free()

	Steam.requestLobbyList()

func set_lobby_text(new_names : Array[String]) -> void:
	clear_lobby_text()

	for new_name in new_names:
		var name_label : Label = Label.new()
		name_label.text = new_name
		lobby_container.add_child(name_label)

func clear_lobby_text() -> void:
	var lobby_container_children = lobby_container.get_children()
	for child in lobby_container_children:
		child.free()

func update_steam_lobby_id_button():
	steam_lobby_id_button.show()
	steam_lobby_id = Global.steam_lobby_id

#endregion


#region Signal Functions
func _on_disconnet_button_pressed() -> void:
	disconnect_button_pressed.emit()

func _on_steam_host_pressed() -> void:
	steam_host_pressed.emit(username.text)
	steam_join_button.hide()
	tab_container.tabs_visible = false
	steam_address_entry.hide()
	steam_host_button.hide()
	username.hide()

func _on_steam_join_pressed() -> void:
	Global.steam_lobby_id = steam_address_entry.text
	steam_join_pressed.emit(username.text)
	steam_join_button.hide()
	tab_container.tabs_visible = false
	steam_address_entry.hide()
	steam_host_button.hide()
	username.hide()
	disconnect_button.show()

func _on_enet_host_toggled(toggled_on : bool) -> void:
	if toggled_on:
		enet_address_entry.hide()
		enet_join_button.hide()
		username.hide()
		enet_host_toggled_on.emit(username.text)
		tab_container.tabs_visible = false
		return

	tab_container.tabs_visible = true
	enet_host_toggled_off.emit()
	enet_address_entry.show()
	enet_join_button.show()
	username.show()

func _on_enet_join_pressed() -> void:
	if enet_address_entry.text.is_empty() == false:
		Global.enet_address = enet_address_entry.text

	tab_container.tabs_visible = false
	enet_join_pressed.emit(username.text)
	enet_join_button.hide()
	tab_container.hide()
	username.hide()
	disconnect_button.show()

func _on_steam_lobby_id_button_pressed():
	DisplayServer.clipboard_set(steam_lobby_id)

#endregion
