# LootWindow.gd
# Zeigt Loot eines besiegten Gegners an und lässt Spieler Items aufheben
extends CanvasLayer

var player = null
var current_loot: Dictionary = {}  # {"gold": int, "items": [Equipment]}
var loot_world_position: Vector3 = Vector3.ZERO
var panel_visible = false

const LOOT_PICKUP_RANGE = 5.0

@onready var panel = $Panel
@onready var gold_label = $Panel/VBoxContainer/GoldLabel
@onready var item_list = $Panel/VBoxContainer/ScrollContainer/ItemList
@onready var loot_all_button = $Panel/VBoxContainer/LootAllButton

func _ready():
	panel.visible = false
	loot_all_button.pressed.connect(_on_loot_all)

func setup(p):
	player = p

# Loot anzeigen
func show_loot(loot: Dictionary, world_pos: Vector3):
	current_loot = loot
	loot_world_position = world_pos
	_refresh_display()
	panel_visible = true
	panel.visible = true

func hide_loot():
	panel_visible = false
	panel.visible = false
	current_loot = {}

func _refresh_display():
	# Gold anzeigen
	if current_loot.get("gold", 0) > 0:
		gold_label.text = str(current_loot["gold"]) + " Gold"
		gold_label.visible = true
	else:
		gold_label.visible = false

	# Items anzeigen
	for child in item_list.get_children():
		child.queue_free()

	var items = current_loot.get("items", [])
	for i in range(items.size()):
		var item = items[i]
		var hbox = HBoxContainer.new()
		hbox.add_theme_constant_override("separation", 4)

		# Icon wenn vorhanden
		if item.icon:
			var icon = TextureRect.new()
			icon.texture = item.icon
			icon.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL
			icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
			icon.custom_minimum_size = Vector2(24, 24)
			hbox.add_child(icon)

		var button = Button.new()
		if item is Equipment:
			button.text = item.item_name + " (" + Equipment.get_slot_name(item.slot) + ")"
			button.modulate = Equipment.get_rarity_color(item.rarity)
		elif item is Consumable:
			var count_text = " x" + str(item.stack_size) if item.stack_size > 1 else ""
			button.text = item.item_name + count_text
			button.modulate = Color(0.3, 0.9, 0.3)
		button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
		button.pressed.connect(_on_loot_item.bind(i))
		button.tooltip_text = _get_item_tooltip(item)
		hbox.add_child(button)

		item_list.add_child(hbox)

	# Wenn kein Loot mehr da, Fenster schließen
	if current_loot.get("gold", 0) <= 0 and items.size() == 0:
		hide_loot()

# Einzelnes Item aufheben
func _on_loot_item(index: int):
	if player == null:
		return

	var items = current_loot.get("items", [])
	if index >= items.size():
		return

	var item = items[index]
	if player.inventory.add_item(item):
		items.remove_at(index)
		_refresh_display()

# Alles aufheben
func _on_loot_all():
	if player == null:
		return

	# Gold aufheben
	var gold = current_loot.get("gold", 0)
	if gold > 0:
		player.inventory.add_gold(gold)
		current_loot["gold"] = 0

	# Items aufheben
	var items = current_loot.get("items", [])
	var remaining = []
	for item in items:
		if not player.inventory.add_item(item):
			remaining.append(item)  # Inventar voll
	current_loot["items"] = remaining

	_refresh_display()

func _get_item_tooltip(item) -> String:
	if item is Consumable:
		return item.item_name + "\n" + item.get_effect_text()

	var tooltip = item.item_name + "\n"
	tooltip += Equipment.get_slot_name(item.slot) + "\n"

	if item.slot == Equipment.Slot.WEAPON:
		tooltip += "Schaden: " + str(item.min_damage) + "-" + str(item.max_damage) + "\n"
		tooltip += "Tempo: " + str(item.attack_speed) + "s\n"
	if item.armor > 0:
		tooltip += "Rüstung: +" + str(item.armor) + "\n"
	if item.strength > 0:
		tooltip += "Stärke: +" + str(item.strength) + "\n"
	if item.agility > 0:
		tooltip += "Beweglichkeit: +" + str(item.agility) + "\n"
	if item.intelligence > 0:
		tooltip += "Intelligenz: +" + str(item.intelligence) + "\n"
	if item.stamina > 0:
		tooltip += "Ausdauer: +" + str(item.stamina) + "\n"
	if item.haste > 0:
		tooltip += "Tempo: +" + str(int(item.haste * 100)) + "%\n"

	return tooltip
