extends Control

onready var countdown_timer = get_node("countdown_timer")
onready var player_settings = find_node("player_settings")
onready var player_list = find_node("players")


func _ready():
	if get_tree().get_root().has_node("multiplayer_menu"):
		get_tree().get_root().get_node("multiplayer_menu").hide()
	if get_tree().get_root().has_node("main_menu"):
		get_tree().get_root().get_node("main_menu").hide()
	
	get_node("menu/back").connect("pressed",self,"_on_back_pressed")
	
	gamestate.connect("players_changed", self, "_players_changed")
	gamestate.connect("game_ready", self, "_game_ready")
	gamestate.connect("game_started", self, "_game_started")
	client.connect("server_disconnected", self, "_server_disconnected")
	
	player_settings.get_name_node().connect("text_changed",self,"_on_name_text_changed")
	player_settings.get_color_node().connect("color_changed",self,"_on_color_color_changed")
	player_settings.get_color_node().connect("draw",self,"_on_player_changed")
	
	countdown_timer.set_wait_time(server.LOBBY_READY_WAIT_TIME)
	
	if gamestate.game_name:	
		find_node("server_name").set_text(gamestate.game_name)
		find_node("server_name").set_tooltip(gamestate.game_name)


func _draw():
	find_node("ready").grab_focus()


func _physics_process(delta):
	if not countdown_timer.is_stopped():
		get_node("countdown").text = str(int(countdown_timer.get_time_left()) + 1)


func _players_changed(players):
	player_list.clear()
	for p_id in players:
		var player = players[p_id]
		if p_id == get_tree().get_network_unique_id():
			player_list.add_item(player.name + " (You)")
		else:
			player_list.add_item(player.name)
		player_list.set_item_custom_bg_color(player_list.get_item_count()-1, player.color)
		if player.ready:
			player_list.set_item_icon(player_list.get_item_count()-1, player_list.get_node("ready_icon").get_button_icon())
		else:
			player_list.set_item_icon(player_list.get_item_count()-1, player_list.get_node("not_ready_icon").get_button_icon())
	find_node("server_bots").set_pressed(gamestate.bots)


func _game_ready(all_ready):
	if all_ready:
		get_node("countdown").show()
		countdown_timer.start()
	else:
		countdown_timer.stop()
		get_node("countdown").hide()
	
func _game_started():
	queue_free()


func _on_name_text_changed(new_text):
	_on_player_changed()


func _on_color_color_changed(color):
	_on_player_changed()


func _on_ready_toggled(button_pressed):
	_on_player_changed()
	player_settings.get_name_node().set_editable(not button_pressed)
	player_settings.get_color_node().set_disabled(button_pressed)


func _on_player_changed():
	var name = player_settings.get_name_node().text
	var color = player_settings.get_color_node().color
	var ready = find_node("ready").pressed
	gamestate.update_player(name,color,ready)


func _on_back_pressed():
	gamestate.quit_game()
	if get_tree().get_root().has_node("multiplayer_menu"):
		get_tree().get_root().get_node("multiplayer_menu").show()
		get_tree().get_root().get_node("main_menu").hide()
	elif get_tree().get_root().has_node("main_menu"):
		get_tree().get_root().get_node("main_menu").show()
	queue_free()


func _server_disconnected():
	_on_back_pressed()
