extends CanvasLayer

const START_SCREEN_SCENE := "res://scenes/UI/start_screen.tscn"

@onready var pause_menu = $Control

func _ready() -> void:
	_register_focus_sounds()

func _input(event):
	if event.is_action_pressed("ui_cancel"):
		if get_tree().paused:
			resume_game()
		else:
			pause_game()

func pause_game():
	get_tree().paused = true
	visible = true

func resume_game():
	get_tree().paused = false
	visible = false

func _on_quit_button_pressed():
	get_tree().quit()

func _on_continue_button_pressed():
	resume_game()

func _on_main_menu_button_pressed():
	resume_game()
	get_tree().change_scene_to_file(START_SCREEN_SCENE)

func _register_focus_sounds() -> void:
	if pause_menu == null:
		return
	var vbox := pause_menu.get_node_or_null("VBoxContainer")
	if vbox == null:
		return
	for child in vbox.get_children():
		if child is BaseButton:
			var button: BaseButton = child
			if not button.is_connected("focus_entered", Callable(self, "_on_menu_item_focus")):
				button.focus_entered.connect(_on_menu_item_focus)
			if not button.is_connected("mouse_entered", Callable(self, "_on_menu_item_focus")):
				button.mouse_entered.connect(_on_menu_item_focus)

func _on_menu_item_focus() -> void:
	if MenuSfx:
		MenuSfx.play_hover()
