extends CanvasLayer

const gui_components = [
	"res://scenes/gui/menus/settings_menu.tscn",
	"res://scenes/gui/menus/pause_menu.tscn",
	"res://scenes/gui/in_game_gui/in_game_gui.tscn",
	"res://scenes/gui/cutscenes/lever_dialogues.tscn",
	"res://scenes/gui/menus/credits.tscn",
]

var settings_menu : Control
var pause_menu : Control
var in_game_gui : Control
var lever_dialogues : LeverDialogues

func _ready() -> void:
	SignalBus.paused.connect(pause)
	SignalBus.unpaused.connect(unpause)
	SignalBus.changed_scene_to.connect(change_scene_to)
	
	for gui in gui_components:
		var new_scene = load(gui).instantiate()
		add_child(new_scene)
		new_scene.hide()
		
	settings_menu = get_node(Utils.SETTINGS_MENU)
	pause_menu = get_node(Utils.PAUSE_MENU)
	in_game_gui = get_node(Utils.IN_GAME_GUI)
	lever_dialogues = get_node(Utils.LEVER_DIALOGUES)

func _exit_tree() -> void:
	SignalBus.paused.disconnect(pause)
	SignalBus.unpaused.disconnect(unpause)
	SignalBus.changed_scene_to.disconnect(change_scene_to)

func pause():
	in_game_gui.hide()
	pause_menu.show()

func unpause():
	in_game_gui.show()
	hide_menus()

func change_scene_to(_scene : Utils.GAME_SCENES):
	hide_menus()
	if _scene == Utils.GAME_SCENES.GAME:
		in_game_gui.show()
	else:
		in_game_gui.hide()

func hide_menus():
	pause_menu.hide()
	settings_menu.hide()

func show_submenu(original_node : Control, menu : String):
	original_node.hide()
	var submenu = get_node(menu)
	submenu.original_node = original_node
	submenu.update()
	submenu.show()

func return_from_submenu(submenu : Control):
	submenu.hide()
	submenu.original_node.show()
