extends Node
class_name GameRoot

signal load_map(name: String)

# -- config from args
var config_file: String = ""

const MAIN_MENU_SCENE := "res://scenes/menus/main/main_menu.tscn"

@onready var level_root: Node3D = $LevelLoader/Level
@onready var level_spawner: MultiplayerSpawner = $LevelLoader/MultiplayerSpawner

func _parse_args() -> void:
	var args := Array(OS.get_cmdline_args())
	
	var config_arg: String = "--config"
	if args.has(config_arg):
		var index := args.find(config_arg)
		config_file = args[index + 1]
	
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	_parse_args()

	if OS.has_feature("dedicated_server") or DisplayServer.get_name() == "headless":
		var err := _start_dedicated_server()
		if err != OK:
			logger.error("couldn't start the server, err is " + str(err))
	else:
		var err := _start_game()
		if err != OK:
			logger.error("couldn't start the game, err is " + str(err))

func _start_dedicated_server() -> Error:
	if config_file != "":
		logger.info("reading config from the file: " + config_file)
	logger.info("starting in the dedicated server mode")
	return OK


func _start_game() -> Error:
	logger.info("starting in the game mode")

	if not ResourceLoader.exists(MAIN_MENU_SCENE):
		return ERR_DOES_NOT_EXIST
	var scene: PackedScene = ResourceLoader.load(MAIN_MENU_SCENE)
	if not scene.can_instantiate():
		return ERR_CANT_OPEN
	
	var node: MainMenu = scene.instantiate()
	add_child(node)	
	return OK

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _on_load_map(map_name: String) -> void:
	for c in level_root.get_children():
		level_root.remove_child(c)
		c.queue_free()
	
	var path_tmpl := "res://scenes/maps/maps/%s"
	var path := path_tmpl % map_name
	
	if not ResourceLoader.exists(path):
		logger.error("map " + map_name + " doesn't exist")
	
	var scene: PackedScene = ResourceLoader.load(path)
	if scene.can_instantiate():
		var node: Node3D = scene.instantiate()
		logger.info("loading map: " + map_name)
		level_root.add_child(node)
	else:
		logger.error("Can't initialize")
	
