extends Spatial

var _current_player_index

var _color_codes = []

var _turn_number

var _round_counter = 0

var _other_players = []

var _map

var _game_started = false

var _ability_left = false

var _previous_hex = null

func _ready():
	$GUI/Player_name_panel/HBoxContainer/Player_name.text = Player.get_player_name()
	_other_players = NetworkingSync._player_in_same_game_room_list.duplicate()
	_other_players.erase([Player.get_player_id(), Player.get_player_name()])
	init_game()
	fill_player_list()
	
	if Player.is_host():
		init_game_state()
	
	NetworkingSync.send_ready_signal()

func init_game():
	create_map()
	_turn_number = 1
	$GUI/Bottom_left_panel/VBoxContainer/HBoxContainer/Turn_indicator.text = str(_turn_number)

func fill_player_list():
	for player in _other_players:
		add_other_players_to_list(player[1])

func update_player_list():
	for child in $GUI/Player_info/VBoxContainer/Player_list.get_children():
		child.queue_free()
	
	fill_player_list()

func add_other_players_to_list(player_name):
	var player = load("res://Scenes/Matchmaking/Game_room/Player_entry.tscn").instance()
	player.set_player_name(player_name)
	$GUI/Player_info/VBoxContainer/Player_list.add_child(player)

func create_map():
	_map = load("res://Scenes/Matchmaking/Game/Map/Map.tscn").instance()
	add_child(_map)

func init_game_state():
	yield(NetworkingSync, "all_ready")
	var starting_player_index = calculate_stating_player_index()
	
	set_starting_player(starting_player_index)
	for player in _other_players:
		rpc_id(player[0], "set_starting_player", starting_player_index)

func is_my_turn():
	return NetworkingSync._player_in_same_game_room_list[_current_player_index][0] == Player.get_player_id()

func calculate_stating_player_index():
	return int(rand_range(0, len(NetworkingSync._player_in_same_game_room_list)))

remote func set_starting_player(starting_player_index):
	_current_player_index = starting_player_index
	$GUI/Top_panel/Top_panel_container/Player_indicator.text = str(NetworkingSync._player_in_same_game_room_list[_current_player_index][1])
	update_current_player()
	

func update_current_player():
	if is_my_turn():
		_ability_left = true
		_round_counter = 0
		_previous_hex = null
		$GUI/Bottom_left_panel/VBoxContainer/Next_turn_button.disabled = false
	else:
		$GUI/Bottom_left_panel/VBoxContainer/Next_turn_button.disabled = true
	
func _on_Next_turn_button_pressed():
	next_turn()

func next_turn():
	next_player()
	for player in _other_players:
		rpc_id(player[0], "next_player")
	_turn_number += 1
	$GUI/Bottom_left_panel/VBoxContainer/HBoxContainer/Turn_indicator.text = str(_turn_number)

remote func next_player():
	_current_player_index = (_current_player_index + 1) % len(NetworkingSync._player_in_same_game_room_list)
	$GUI/Top_panel/Top_panel_container/Player_indicator.text = str(NetworkingSync._player_in_same_game_room_list[_current_player_index][1])
	update_current_player()

func game_started():
	return _game_started

func color_the_hex(hex_node):
	for player in _other_players:
		rpc_id(player[0], "color_the_hex_to_player_color", hex_node._x, hex_node._y)
	color_the_hex_to_player_color(hex_node._x, hex_node._y)

remote func color_the_hex_to_player_color(hex_x, hex_y):
	_map._hexes[hex_x][hex_y].set_color(_current_player_index)

func ability_left():
	return _ability_left

func get_previous_hex():
	return _previous_hex

func try_to_occupy_hex(hex_node):
	color_the_hex(hex_node)
	occupy_hex(hex_node._x, hex_node._y)
	for player in _other_players:
		rpc_id(player[0], "occupy_hex", hex_node._x, hex_node._y)
	
	_previous_hex = hex_node
	_round_counter += 1
	
	if _round_counter >= _turn_number:
		next_turn()
	else:
		if !hex_node.is_any_neighbour_free():
			left_game()

remote func occupy_hex(hex_x, hex_y):
	_map._hexes[hex_x][hex_y].occupy_hex()

func left_game():
	for player in _other_players:
		rpc_id(player[0], "player_left", Player.get_player_id(), Player.get_player_name())
	end_game(false)

func end_game(is_winning):
	if is_winning:
		$End_game_panel/Label.text = "You won."
	$GUI.visible = false
	$End_game_panel.visible = true

remote func player_left(player_id, player_name):
	_other_players.erase([player_id, player_name])
	NetworkingSync._player_in_same_game_room_list.erase([player_id, player_name])
	update_player_list()
	check_for_win()
	next_player()

func check_for_win():
	if len(_other_players) <= 0:
		end_game(true)

func back_to_lobby():
	get_tree().change_scene("res://Scenes/Matchmaking/Lobby/Lobby.tscn")

func _on_Back_to_lobby_button_pressed():
	back_to_lobby()

func _on_Surrender_button_pressed():
	$Menu_panel.visible = false
	left_game()

func _input(event):
	if Input.is_key_pressed(KEY_ESCAPE) and $Menu_panel.visible:
		$Menu_panel.visible = false
	elif Input.is_key_pressed(KEY_ESCAPE) and !$Menu_panel.visible:
		$Menu_panel.visible = true

func _on_Close_pressed():
	$Menu_panel.visible = false

func _on_Blocker_power_up_pressed():
	#TODO:
	# select 3 hexes, which will be blocked (make invisible?)
	pass # Replace with function body.

func _on_Reuse_Power_up_pressed():
	# TODO: you can click on hexes, which are already filled
	pass # Replace with function body.
