class_name PauseMenu
extends CanvasLayer

## PauseMenu - In-game pause/escape menu

signal resume_game
signal restart_game
signal return_to_menu

# UI Components
var panel: PanelContainer
var resume_button: Button
var restart_button: Button
var menu_button: Button

var menu_is_visible: bool = false

func _ready() -> void:
	_create_menu()
	hide_menu()

func _create_menu() -> void:
	# Semi-transparent background
	var bg = ColorRect.new()
	add_child(bg)
	bg.set_anchors_preset(Control.PRESET_FULL_RECT)
	bg.color = Color(0, 0, 0, 0.7)
	bg.mouse_filter = Control.MOUSE_FILTER_STOP

	# Center container
	var center = CenterContainer.new()
	add_child(center)
	center.set_anchors_preset(Control.PRESET_FULL_RECT)

	# Panel
	panel = PanelContainer.new()
	center.add_child(panel)

	var style = StyleBoxFlat.new()
	style.bg_color = Color(0.15, 0.15, 0.2)
	style.border_color = Color(0.3, 0.3, 0.4)
	style.set_border_width_all(2)
	style.set_corner_radius_all(10)
	style.set_content_margin_all(30)
	panel.add_theme_stylebox_override("panel", style)

	var vbox = VBoxContainer.new()
	panel.add_child(vbox)
	vbox.add_theme_constant_override("separation", 15)

	# Title
	var title = Label.new()
	title.text = "Game Paused"
	title.add_theme_font_size_override("font_size", 28)
	title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	vbox.add_child(title)

	# Spacer
	var spacer = Control.new()
	spacer.custom_minimum_size = Vector2(0, 10)
	vbox.add_child(spacer)

	# Resume button
	resume_button = _create_menu_button("Resume")
	vbox.add_child(resume_button)
	resume_button.pressed.connect(_on_resume_pressed)

	# Restart button
	restart_button = _create_menu_button("Restart Game")
	vbox.add_child(restart_button)
	restart_button.pressed.connect(_on_restart_pressed)

	# Return to menu button
	menu_button = _create_menu_button("Main Menu")
	vbox.add_child(menu_button)
	menu_button.pressed.connect(_on_menu_pressed)

func _create_menu_button(text: String) -> Button:
	var button = Button.new()
	button.text = text
	button.custom_minimum_size = Vector2(180, 45)
	button.add_theme_font_size_override("font_size", 18)
	return button

func show_menu() -> void:
	visible = true
	menu_is_visible = true
	get_tree().paused = true

func hide_menu() -> void:
	visible = false
	menu_is_visible = false
	get_tree().paused = false

func toggle_menu() -> void:
	if menu_is_visible:
		hide_menu()
	else:
		show_menu()

func _on_resume_pressed() -> void:
	hide_menu()
	resume_game.emit()

func _on_restart_pressed() -> void:
	hide_menu()
	restart_game.emit()

func _on_menu_pressed() -> void:
	hide_menu()
	return_to_menu.emit()
