extends Control
## Menu shown in-game when the user presses pause.

const SETTINGS_SCENE := preload("res://src/ui/menus/settings_menu/settings_menu.tscn")

@onready var menu_list: VBoxContainer = %MenuList
@onready var quit_confirm: CenterContainer = %QuitConfirm
@onready var settings_container: MarginContainer = %SettingsContainer


func _ready() -> void:
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)


func _exit_tree() -> void:
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)


func _unhandled_key_input(event: InputEvent) -> void:
	if event.is_action_pressed("pause"):
		get_viewport().set_input_as_handled()
		resume()


func _unhide() -> void:
	menu_list.show()


func _hide() -> void:
	menu_list.hide()


func resume() -> void:
	queue_free()


func settings() -> void:
	var instance: Control = SETTINGS_SCENE.instantiate()
	settings_container.add_child(instance)
	instance.tree_exited.connect(_unhide)
	_hide()


func quit() -> void:
	quit_confirm.show()


func cancel_quit() -> void:
	quit_confirm.hide()


func confirm_quit() -> void:
	get_tree().root.propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)
