extends Node3D

var quit_dialog: ConfirmationDialog

@export var start_scene_path: String = "res://scenes/start.tscn"


func _ready() -> void:
	quit_dialog = ConfirmationDialog.new()
	quit_dialog.title = "Quit Game?"
	quit_dialog.dialog_text = "Return to the start menu?"
	add_child(quit_dialog)

	quit_dialog.confirmed.connect(_on_return_confirmed)
	quit_dialog.canceled.connect(_on_return_canceled)


func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventKey and event.pressed and not event.echo:
		if event.ctrl_pressed and event.keycode == KEY_Q:
			# Enable mouse so player can click the dialog
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
			quit_dialog.popup_centered()


func _on_return_confirmed() -> void:
	# Change back to the start scene
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	get_tree().change_scene_to_file(start_scene_path)


func _on_return_canceled() -> void:
	# Hide dialog and recapture mouse
	quit_dialog.hide()
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
