extends Control

@export var SectorScenePath: String = "res://scenes/Sector.tscn"
@onready var container = $VBoxContainer

func _ready():
	_init_background()
	_create_sector_buttons()

# --- TWORZENIE GWIAZD SEKTORÓW ---
func _create_sector_buttons():
	for child in container.get_children():
		child.queue_free()

	for i in range(Manager.SECTOR_COUNT):
		var sector_info = Manager.get_sector(i)

		var button = TextureButton.new()
		button.texture_normal = load(sector_info["star"]["texture_path"])
		button.stretch_mode = TextureButton.STRETCH_KEEP_ASPECT_CENTERED

		# Skala zapisanej gwiazdy
		var star_scale = sector_info["star"]["scale"]
		button.scale = Vector2(star_scale, star_scale)

		# Pozycja z Managera
		button.position = sector_info["position"]

		# Podpis sektora
		var sector_label = Label.new()
		sector_label.text = sector_info["name"]
		sector_label.modulate = Color(0.386, 0.577, 0.778, 1.0) # czerwony
		#sector_label.scale = Vector2(0.5, 0.5) # ignoruje skalę gwiazdy

		sector_label.add_theme_font_size_override("font_size", 300) # malutki podpis
		sector_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
		sector_label.position = Vector2(0, -450)
		button.add_child(sector_label)

		var index = i
		button.pressed.connect(func() -> void:
			_on_sector_selected(index)
		)

		container.add_child(button)

# --- WYBRANIE SEKTORA ---
func _on_sector_selected(index: int):
	Manager.current_sector = index
	var sector_scene = load(SectorScenePath)
	get_tree().change_scene_to_packed(sector_scene)

# --- TŁO ---
func _init_background():
	var background = Sprite2D.new()
	background.texture = load("res://assets/background/galaxy_background.jpg")
	background.centered = true
	background.position = get_viewport_rect().size / 2
	var screen_size = get_viewport_rect().size
	var tex_size = background.texture.get_size()
	var scale_factor = max(screen_size.x / tex_size.x, screen_size.y / tex_size.y)
	background.scale = Vector2(scale_factor, scale_factor)
	add_child(background)
	background.z_index = -10
