extends Node
# {game_id:[_game_id, _game_name, _max_players, _player_list]} # player_list is a dict
# {game_id:[_game_id, _game_name, _max_players, {player_id:[_player_name, _player_id, _game_id, _is_host]}]}
var _open_games = {}

var _players_online = []
var _number_of_online_players

func get_number_of_online_players():
	return len(get_tree().get_network_connected_peers())

remote func get_online_players(id):
	rpc_id(id, "update_online_players", _number_of_online_players)

remote func get_open_games_from_server(id):
	rpc_id(id, "update_open_games", _open_games)

remote func add_game_to_game_list(game_id, game_information, host_player):
	_open_games[game_id] = game_information
	add_player_to_open_game(game_id, host_player)
	get_open_games_from_server(game_id)

remote func join_open_game(game_id, player_information):
	add_player_to_open_game(game_id, player_information)

func add_player_to_open_game(game_id, player_information):
	_open_games[game_id][3][player_information[1]] = player_information

remote func remove_player_from_open_game(game_id, player_id):
	_open_games[game_id][3].erase(player_id)

remote func remove_game_from_game_list(game_id):
	_open_games.erase(game_id)


var _queue = []
var _hidden_games = {}

#func _process(delta):
#	if _queue.empty():
#		print("Nothing to do.")
#	else:
#		var player_id = dequeue()
#		
#		if _hidden_games.empty():
#			create_game(player_id)
#		else:
#			join_next_hidden_game(player_id)
#		

func create_game(player_id):
	pass

func join_next_hidden_game(player_id):
	pass

remote func search_for_game(player_id):
	enqueue(player_id)

func enqueue(player):
	_queue.append(player)

func dequeue():
	return _queue.pop_front()


# 1. player clicks matchmaking
# 2. is there a hidden game?
# 3. a) yes -> join the hidden game. -> the it should be full, and the game should start
# 3. b) no
# 4. b) player creates the game that will go to the server-hidden-games
# 5. b) waiting for player to join. if full, game will start

# Player side needs:
	# Searching-Button -> Server will create the games

# Server side needs:
	# queue for the searching players
	# 
