extends Node


# just to keep these resources loaded
# also makes them visible in the inspector while debugging
var _config: Config
var _game_state: GameState

var _current_scene: Node
var _kept_scenes := {}


func _ready() -> void:
	MainEvents.scene_change_requested.connect(_change_scene_to)
	_current_scene = get_child(0)


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("toggle_fullscreen"):
		match DisplayServer.window_get_mode():
			DisplayServer.WINDOW_MODE_WINDOWED, DisplayServer.WINDOW_MODE_MAXIMIZED:
				DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
			DisplayServer.WINDOW_MODE_FULLSCREEN:
				DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED) 


func _change_scene_to(path: String, keep_state := false) -> void:
	if keep_state:
		_kept_scenes[_current_scene.scene_file_path] = _current_scene
		remove_child(_current_scene)
		_current_scene.process_mode = Node.PROCESS_MODE_DISABLED
	else:
		_current_scene.queue_free()
	
	if _kept_scenes.has(path):
		_current_scene = _kept_scenes[path]
		_current_scene.process_mode = Node.PROCESS_MODE_INHERIT
		_kept_scenes.erase(path)
	else:
		_current_scene = load(path).instantiate()
	
	add_child(_current_scene)
