extends Panel


var dragging := false
var drag_offset := Vector2.ZERO

func _gui_input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			if event.pressed:
				# Kliknięto na panel → zaczynamy drag
				dragging = true
				drag_offset = get_local_mouse_position()
			else:
				# Zwolniono przycisk → kończymy drag
				dragging = false
	elif event is InputEventMouseMotion and dragging:
		# Przesuwamy panel względem pozycji myszy
		position += event.relative


var current_station = null
@onready var ships_container = $ShipContainer

func _ready():
	generate_ship_buttons()
	var tech_panel = get_tree().root.get_node("Galaxy/UI/TechPanel")
	tech_panel.connect("ships_buttons_update", Callable(self, "_on_ships_buttons_update"))

func _on_ships_buttons_update():
	generate_ship_buttons()
	
func generate_ship_buttons():
	# Usuwamy stare guziki
	for c in ships_container.get_children():
		c.queue_free()

	# Pobieramy statki z autoloadu
	var all_ships = ShipsType.SHIP_TYPES

	# Pobieramy odblokowane technologie gracza
	var unlocked = PlayerData.unlocked_techs

	for ship_id in all_ships.keys():
		var data = all_ships[ship_id]

		# 1. Sprawdzenie technologii
		if data.required_tech != null:
			var req = data.required_tech

			if req is String and not unlocked.has(req):
				continue

			if req is Array:
				if req.any(func(t): return not unlocked.has(t)):
					continue


		# 2. Tworzymy guzik
		var btn = Button.new()
		btn.text = data.name

		# 3. Podpinamy akcję
		btn.pressed.connect(func():
			build_ship(ship_id)
		)

		# 4. Dodajemy do UI
		ships_container.add_child(btn)

func show_for_station(station):
	self.visible = true
	current_station = station


func _on_button_2_pressed() -> void:
	self.visible = false


#func _on_button_pressed() -> void:
	#build_ship("frigate")
	#
#func _on_button_3_pressed() -> void:
	#build_ship("transport")
	


func build_ship(type: String):
	if current_station == null:
		print("Brak stacji – nie można budować statku")
		return

	var cost = ShipsType.SHIP_TYPES[type].cost

	# 1. Sprawdź zasoby gracza
	for res in cost.keys():
		if PlayerData.resources[res] < cost[res]:
			print("Brakuje:", res)
			return

	# 2. Odejmij zasoby gracza
	for res in cost.keys():
		PlayerData.resources[res] -= cost[res]
		PlayerData.emit_signal("stats_changed")
	# 3. Stwórz statek
	var ship = preload("res://scenes/ship.tscn").instantiate()
	ship.setup(type)

	# POZYCJA SPAWNU — np. obok stacji
	ship.position = current_station.position + Vector2(15, 15)
	# Ustawiamy prędkość z listy statków
	ship.speed = ShipsType.SHIP_TYPES[type].speed
	ship.get_node("Sprite2D").scale = Vector2(ShipsType.SHIP_TYPES[type].scale,ShipsType.SHIP_TYPES[type].scale)

	# Nadajemy kierunek (np. losowy)
	ship.direction = Vector2(randf() * 2 - 1, randf() * 2 - 1).normalized()

	# Obrót zgodnie z kierunkiem
	ship.rotation = ship.direction.angle()

		# Dodaj do sceny
	get_tree().root.get_node("Galaxy").add_child(ship)

	print("Zbudowano statek:", type)
