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

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

	# 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)
