extends CanvasLayer

var _root: Control


func _ready() -> void:
	layer = 10
	_build_ui()
	_root.visible = false


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("toggle_pause"):
		_toggle()
		get_viewport().set_input_as_handled()


func _toggle() -> void:
	_root.visible = not _root.visible


func _build_ui() -> void:
	_root = Control.new()
	_root.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	add_child(_root)

	# Dim overlay — blocks mouse clicks reaching the game
	var overlay := ColorRect.new()
	overlay.color = Color(0.0, 0.0, 0.0, 0.58)
	overlay.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	overlay.mouse_filter = Control.MOUSE_FILTER_STOP
	_root.add_child(overlay)

	# Center panel (360 × 490)
	var panel := Panel.new()
	panel.set_anchors_preset(Control.PRESET_CENTER)
	panel.offset_left   = -180
	panel.offset_top    = -245
	panel.offset_right  =  180
	panel.offset_bottom =  245

	var ps := StyleBoxFlat.new()
	ps.bg_color = Color(0.04, 0.07, 0.16, 0.97)
	ps.set_corner_radius_all(10)
	ps.set_border_width_all(1)
	ps.border_color = Color(0.18, 0.38, 0.65, 0.9)
	panel.add_theme_stylebox_override("panel", ps)
	_root.add_child(panel)

	var vb := VBoxContainer.new()
	vb.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	vb.offset_left   = 28
	vb.offset_top    = 28
	vb.offset_right  = -28
	vb.offset_bottom = -28
	vb.add_theme_constant_override("separation", 10)
	panel.add_child(vb)

	var title := Label.new()
	title.text = "MENU"
	title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	title.add_theme_font_size_override("font_size", 26)
	title.add_theme_color_override("font_color", Color(0.4, 0.85, 1.0))
	vb.add_child(title)

	vb.add_child(_hsep())

	var gap := Control.new()
	gap.custom_minimum_size = Vector2(0, 4)
	vb.add_child(gap)

	_add_btn(vb, "RESUME", true).pressed.connect(_toggle)
	_add_btn(vb, "SETTINGS", false)
	_add_btn(vb, "SELECT TEAM", false)

	vb.add_child(_hsep())

	_add_btn(vb, "QUIT TO MENU", true).pressed.connect(_on_quit_to_menu)
	_add_btn(vb, "QUIT TO DESKTOP", true).pressed.connect(_on_quit_to_desktop)


func _add_btn(parent: VBoxContainer, label_text: String, active: bool) -> Button:
	var btn := Button.new()
	btn.text = label_text
	btn.custom_minimum_size = Vector2(0, 54)
	btn.add_theme_font_size_override("font_size", 16)

	if active:
		btn.add_theme_color_override("font_color", Color(0.92, 0.95, 1.0))
		var sn := _flat(Color(0.07, 0.13, 0.25, 1.0), 6)
		var sh := _flat(Color(0.13, 0.24, 0.42, 1.0), 6)
		sh.set_border_width_all(1)
		sh.border_color = Color(0.3, 0.65, 1.0)
		btn.add_theme_stylebox_override("normal", sn)
		btn.add_theme_stylebox_override("hover", sh)
	else:
		btn.add_theme_color_override("font_color", Color(0.35, 0.40, 0.50))
		var sn := _flat(Color(0.04, 0.06, 0.10, 1.0), 6)
		btn.add_theme_stylebox_override("normal", sn)
		btn.add_theme_stylebox_override("hover", sn)

	parent.add_child(btn)
	return btn


func _hsep() -> HSeparator:
	var sep := HSeparator.new()
	sep.add_theme_color_override("color", Color(0.18, 0.28, 0.45, 0.7))
	return sep


func _flat(col: Color, radius: int = 0) -> StyleBoxFlat:
	var s := StyleBoxFlat.new()
	s.bg_color = col
	s.set_corner_radius_all(radius)
	return s


func _on_quit_to_menu() -> void:
	get_tree().change_scene_to_file("res://main_menu.tscn")


func _on_quit_to_desktop() -> void:
	get_tree().quit()
