extends Node2D

const GAME_OVER_SCENE := preload("res://scenes/game_over.tscn")

@onready var game_root: Node2D = $GameRoot
@onready var board: Node2D = $GameRoot/Board
@onready var hud_left: Control = $GameRoot/HUDLeft
@onready var hud_right: Control = $GameRoot/HUDRight
@onready var ui_root: Control = $GameRoot/UILayer/UIRoot
@onready var blur_overlay: ColorRect = $GameRoot/UILayer/UIRoot/BlurOverlay # New node

@onready var label_score_value: Label = $GameRoot/HUDLeft/VBoxContainer/LabelScoreValue
@onready var label_highscore_value: Label = $GameRoot/HUDLeft/VBoxContainer/LabelHighScoreValue
@onready var label_lines_value: Label = $GameRoot/HUDLeft/VBoxContainer/LabelLinesValue
@onready var next_preview: Node2D = $GameRoot/HUDRight/VBoxContainer/NextPreview

func _ready() -> void:
	board.connect("lines_cleared", Callable(self, "_on_lines_cleared"))
	board.connect("next_piece_changed", Callable(self, "_on_next_piece_changed"))
	board.connect("game_over", Callable(self, "_on_game_over"))

	# Center once and whenever the window size changes
	get_viewport().size_changed.connect(_on_viewport_size_changed)
	_on_viewport_size_changed()
	
func _on_viewport_size_changed() -> void:
	var window_size: Vector2 = get_viewport_rect().size
	var layout_bounds := _get_layout_bounds()

	var layout_center := layout_bounds.position + layout_bounds.size * 0.5
	var target_center := window_size * 0.5

	# Move the whole game so the *layout* center matches the window center
	game_root.position = target_center - layout_center
	
func _on_lines_cleared(total_lines: int, score: int, highscore: int) -> void:
	label_score_value.text = str(score)
	label_highscore_value.text = str(highscore)
	label_lines_value.text = str(total_lines)

func _get_layout_bounds() -> Rect2:
	# Board rectangle (we know its size from constants)
	var board_size := Vector2(
		board.BOARD_WIDTH * board.CELL_SIZE,
		board.BOARD_HEIGHT * board.CELL_SIZE
	)
	var bounds := Rect2(board.position, board_size)

	# Merge HUDLeft and HUDRight rectangles into the bounds
	if hud_left:
		bounds = bounds.merge(hud_left.get_rect())
	if hud_right:
		bounds = bounds.merge(hud_right.get_rect())

	return bounds
	
func _on_next_piece_changed(shape_key: String, shapes: Dictionary, colors: Dictionary) -> void:
	next_preview.update_preview(shape_key, shapes, colors)

func _on_game_over(score: int, highscore: int, total_lines: int) -> void:
	# 1) Turn on blur behind the overlay
	if blur_overlay:
		blur_overlay.visible = true

	# 2) Show the GameOver UI on top
	var game_over_layer := GAME_OVER_SCENE.instantiate()
	game_over_layer.set_anchors_preset(Control.PRESET_CENTER)	# New
	game_over_layer.scale = Vector2(2, 2)	# New

	game_over_layer.final_score = score
	game_over_layer.final_highscore = highscore
	game_over_layer.total_lines = total_lines

	ui_root.add_child(game_over_layer)
	game_over_layer.set_anchors_preset(Control.PRESET_FULL_RECT)

func reset_game() -> void:
	# Hide blur again
	if blur_overlay:
		blur_overlay.visible = false

	if board.has_method("reset_game"):
		board.reset_game()
