class_name PauseMenu
extends Node

const ACTION_PAUSE: StringName = &"pause"

var can_pause: bool = true

@onready var pause_menu: MenuBase = $PauseMenu
@onready var options_menu: MenuBase = $OptionsMenu


func _input(event: InputEvent) -> void:
	if LevelThresholdArea.is_level_loading:
		return

	if event.is_action_pressed(ACTION_PAUSE):
		if options_menu.is_visible_in_tree():
			options_menu.close_menu()
		else:
			toggle_pause()


func set_paused(value: bool) -> void:
	if SceneFader.is_fading:
		return

	get_tree().paused = value
	(pause_menu.open_menu if value else pause_menu.close_menu).call()

	InputManager.set_mouse_captured(not value)


func toggle_pause() -> void:
	if can_pause and not options_menu.is_visible():
		set_paused(not get_tree().paused)


func _on_continue_button_pressed() -> void:
	set_paused(false)


func _on_main_menu_button_pressed() -> void:
	InputManager.release_mouse()
	await SceneFader.load_to_path(GameGlobals.MAIN_MENU_PATH)


func _on_settings_button_pressed() -> void:
	options_menu.open_menu(pause_menu)
