extends Control

var selected_level: int = 1

var layer: CanvasLayer
var root_panel: Panel
var title: Label
var info: Label
var btn_l1: Button
var btn_l2: Button
var btn_l3: Button
var btn_play: Button
var btn_back: Button


func _ready() -> void:
	_build_ui()
	_update_ui()


func _build_ui() -> void:
	layer = CanvasLayer.new()
	add_child(layer)

	root_panel = Panel.new()
	layer.add_child(root_panel)

	# Panel aizņem visu ekrānu
	var vp := get_viewport_rect().size
	root_panel.position = Vector2.ZERO
	root_panel.size = vp

	# Centrālais bloks
	var center := CenterContainer.new()
	center.position = Vector2.ZERO
	center.size = vp
	root_panel.add_child(center)

	var box := VBoxContainer.new()
	box.custom_minimum_size = Vector2(420, 320)
	box.size_flags_horizontal = Control.SIZE_EXPAND_FILL
	box.size_flags_vertical = Control.SIZE_EXPAND_FILL
	box.alignment = BoxContainer.ALIGNMENT_CENTER
	center.add_child(box)

	title = Label.new()
	title.text = "Choose Level"
	title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	title.add_theme_font_size_override("font_size", 34)
	box.add_child(title)

	info = Label.new()
	info.text = ""
	info.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	info.add_theme_font_size_override("font_size", 18)
	box.add_child(info)

	box.add_child(_spacer(12))

	btn_l1 = Button.new()
	btn_l1.text = "Level 1 (Easy)"
	btn_l1.pressed.connect(func(): _select_level(1))
	box.add_child(btn_l1)

	btn_l2 = Button.new()
	btn_l2.text = "Level 2 (Normal)"
	btn_l2.pressed.connect(func(): _select_level(2))
	box.add_child(btn_l2)

	btn_l3 = Button.new()
	btn_l3.text = "Level 3 (Hard)"
	btn_l3.pressed.connect(func(): _select_level(3))
	box.add_child(btn_l3)

	box.add_child(_spacer(18))

	btn_play = Button.new()
	btn_play.text = "Play"
	btn_play.pressed.connect(_on_play_pressed)
	box.add_child(btn_play)

	btn_back = Button.new()
	btn_back.text = "Back"
	btn_back.pressed.connect(_on_back_pressed)
	box.add_child(btn_back)


func _spacer(h: float) -> Control:
	var c := Control.new()
	c.custom_minimum_size = Vector2(1, h)
	return c


func _select_level(lvl: int) -> void:
	selected_level = clamp(lvl, 1, 3)
	_update_ui()


func _update_ui() -> void:
	# saglabā izvēli GameManager (ja ir)
	var gm: Node = get_node_or_null("/root/GameManager")
	if gm == null:
		gm = get_node_or_null("GameManager")

	if gm != null:
		gm.set("selected_level", selected_level)

	var level_name := "Level %d" % selected_level
	if gm != null and gm.has_method("get_level_name"):
		level_name = str(gm.call("get_level_name", selected_level))

	info.text = "Selected: " + level_name

	btn_l1.disabled = (selected_level == 1)
	btn_l2.disabled = (selected_level == 2)
	btn_l3.disabled = (selected_level == 3)


func _on_play_pressed() -> void:
	var fight_path := "res://scenes/FightScene.tscn"
	if ResourceLoader.exists(fight_path):
		get_tree().change_scene_to_file(fight_path)
	else:
		push_error("FightScene not found at: " + fight_path)


func _on_back_pressed() -> void:
	var menu_path := "res://scenes/MainMenu.tscn"
	if ResourceLoader.exists(menu_path):
		get_tree().change_scene_to_file(menu_path)
	else:
		push_error("MainMenu not found at: " + menu_path)
