extends Node2D
class_name SceneManager

const SCENES : Dictionary = {
	"MAINMENU" : "res://scenes/main_menu.tscn",
	"HIGHSCORES" : "res://scenes/high_scores.tscn",
	"LEVEL1" : "res://levels/level_1.tscn",
	"LEVEL2" : "res://levels/level_2.tscn",
	"LEVEL3" : "res://levels/level_3.tscn",
	"LEVEL4" : "res://levels/level_4.tscn",
	"LEVEL5" : "res://levels/level_5.tscn",
	"LEVEL6" : "res://levels/level_6.tscn",
}

var loading_scene_res : Resource = null

func _ready() -> void:
	## LOAD GAME DATA
	HighScoreMgr.load()
	HighScoreMgr.debug_output()
	# HighScoreMgr.debug_save_high_score()


	Log.pr("SceneManager is ready.")
	SceneMgr.connect("change_scene", Callable(self, "_on_change_scene"))
	$TransitionScene.connect("transitioned", Callable(self, "_on_transition_scene_transitioned"))

func _on_change_scene(scene_name : String) -> void:
	Log.pr("Going to load a scene.", scene_name)
	if SCENES.has(scene_name):
		GameState.reset()
		loading_scene_res = load(SCENES[scene_name])
		Log.pr("Loading scene: ", loading_scene_res)
		$TransitionScene.transition()
	else:
		loading_scene_res = null

func _on_transition_scene_transitioned() -> void:
	$CurrentScene.get_child(0).queue_free()
	$CurrentScene.add_child(loading_scene_res.instantiate())
