class_name MainMenu extends Panel

var has_stage: bool = false:
	set(value):
		has_stage = value
		if is_node_ready():
			if has_stage: new_game_button.grab_focus()
			new_game_button.disabled = not has_stage
			continue_button.disabled = not has_stage
			load_game_button.disabled = not has_stage
			settings_button.disabled = not has_stage
			credits_button.disabled = not has_stage
			quit_button.disabled = not has_stage

signal start_game(savegame: SaveGame)
signal open_settings(new_game: bool)
signal roll_credits

@onready var new_game_button: Button = $PanelContainer/NewGameButton
@onready var continue_button: Button = $PanelContainer/ContinueGameButton
@onready var load_game_button: Button = $PanelContainer/LoadGameButton
@onready var settings_button: MenuButton = $PanelContainer/SettingsButton
@onready var settings_popup: SettingsPopup = %SettingsPopup
@onready var credits_button: Button = $PanelContainer/CreditsButton
@onready var quit_button: Button = $PanelContainer/QuitButton
@onready var save_game_handle: SaveGameHandle = %SaveGameHandle

@export var save_game_exists: bool = false:
	set(value):
		save_game_exists = value

var await_new_game: bool = false


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	save_game_handle.picked.connect(_on_save_picked)
	continue_button.visible = save_game_handle.get_most_recent_save().current_room != 0
	continue_button.pressed.connect(func(): start_game.emit(save_game_handle.get_most_recent_save()))
	new_game_button.pressed.connect(func(): save_game_handle.pick_slot(true))
	load_game_button.pressed.connect(func(): save_game_handle.pick_slot(false))
	settings_button.get_popup().index_pressed.connect(settings_popup.show_settings)
	quit_button.pressed.connect(get_tree().quit)
	
	State.take_stage(self)

func _on_save_picked(save: SaveGame):
	start_game.emit(save)
