extends Control

@onready var press_any_key_screen = $PressAnyKeyScreen
var current_menu = null

const MAIN_MENU_SCENE = preload("res://ui/MainMenu.tscn")
const SETTINGS_MENU_SCENE = preload("res://ui/SettingsMenu.tscn")

func _ready():
	print("Main scene ready")
	press_any_key_screen.any_key_pressed.connect(_on_any_key_pressed)

func _on_any_key_pressed():
	print("Transitioning to main menu")
	press_any_key_screen.queue_free()
	show_main_menu()

func show_main_menu():
	clear_current_menu()
	var main_menu = MAIN_MENU_SCENE.instantiate()
	main_menu.open_settings.connect(_on_open_settings)
	add_child(main_menu)
	current_menu = main_menu

func show_settings_menu():
	clear_current_menu()
	var settings_menu = SETTINGS_MENU_SCENE.instantiate()
	settings_menu.back_to_main_menu.connect(_on_back_to_main_menu)
	add_child(settings_menu)
	current_menu = settings_menu

func clear_current_menu():
	if current_menu:
		current_menu.queue_free()
		current_menu = null

func _on_open_settings():
	print("Opening settings menu")
	show_settings_menu()

func _on_back_to_main_menu():
	print("Back to main menu")
	show_main_menu()
