class_name PauseMenu extends Control

signal opened
signal closed

@export var MAIN:PauseMain
@export var settingsPanel:PauseSettings

func _ready() -> void:
  close()
  MAIN.resumeRequested.connect(close)
  MAIN.settingsRequested.connect(_openSettings)
  UI.MAIN_MENU_DIALOG.confirmed.connect(_goToMainMenu)

func isOpen() -> bool:
  return visible

func open() -> void:
  visible = true
  get_tree().paused = true
  MAIN.open()
  opened.emit()

func close() -> void:
  get_tree().paused = false
  visible = false
  MAIN.close()
  settingsPanel.close()
  closed.emit()

func _openSettings() -> void:
  MAIN.close()
  settingsPanel.open()

func _goToMainMenu() -> void:
  close()
  SCENE.setScene(SceneSingleton.SceneType.INITIAL)

func _unhandled_input(event:InputEvent) -> void:
  if !visible:
    return
  if !event.is_action_pressed("ui_cancel"):
    return
  if (UI.QUIT_DIALOG != null and UI.QUIT_DIALOG.isOpen) or (UI.MAIN_MENU_DIALOG != null and UI.MAIN_MENU_DIALOG.isOpen):
    return
  if settingsPanel.isOpen():
    settingsPanel.close()
    MAIN.open()
  else:
    close()
  get_viewport().set_input_as_handled()
