extends Node2D

# Floating text and item graphic that rises and fades out

@onready var label = $Label
@onready var item_sprite = $ItemSprite  # Sprite2D for item graphic (optional)

var text: String = ""
var color: Color = Color.WHITE
var display_duration: float = 0.5  # How long to show text/graphic before fading
var fade_duration: float = 0.5  # How long fade out takes
var rise_distance: float = 30.0
var is_coin: bool = false  # Track if this is a coin (to animate)

func setup(text_value: String, text_color: Color, show_time: float = 0.5, fade_time: float = 0.5, item_texture: Texture2D = null, sprite_hframes: int = 1, sprite_vframes: int = 1, sprite_frame: int = 0, item = null):
	text = text_value
	color = text_color
	display_duration = show_time
	fade_duration = fade_time
	
	if label:
		label.text = text
		label.modulate = color
		label.modulate.a = 1.0  # Start fully visible
		label.z_index = 10  # Render above other objects
	
	# Setup item sprite if texture provided
	if item_sprite and item_texture:
		item_sprite.visible = true
		item_sprite.texture = item_texture
		item_sprite.hframes = sprite_hframes
		item_sprite.vframes = sprite_vframes
		item_sprite.frame = sprite_frame
		item_sprite.modulate = Color.WHITE
		item_sprite.modulate.a = 1.0
		item_sprite.z_index = 10  # Render above other objects
		# Position sprite above label (if label exists) or centered
		if label:
			item_sprite.position = Vector2(0, -12)  # Just above the text (sprite is ~16px tall)
		else:
			item_sprite.position = Vector2(0, 0)
		# Apply item colorization (cloth shader + colorReplacements) when we have an item
		if item and ItemDatabase:
			ItemDatabase.apply_item_colors_to_sprite(item_sprite, item)
		# Check if this is a coin (6 frames horizontal, 1 frame vertical) - animate it
		if sprite_hframes == 6 and sprite_vframes == 1:
			is_coin = true
	else:
		# Hide sprite if no texture provided
		if item_sprite:
			item_sprite.visible = false
			is_coin = false

func _ready():
	# Reparent to FloatingTextLayer (not affected by CanvasModulate) and use screen position
	var gw = get_tree().get_first_node_in_group("game_world")
	var layer = gw.get_node_or_null("FloatingTextLayer") if gw else null
	if layer and gw and gw.has_method("world_to_screen"):
		var world_pos = global_position
		get_parent().remove_child(self)
		layer.add_child(self)
		global_position = gw.world_to_screen(world_pos)
	# Start coin animation if needed
	if is_coin and item_sprite:
		_animate_coin()
	
	# Show text/graphic for display_duration, then fade out over fade_duration
	# Wait for display duration (text/graphic stays visible)
	await get_tree().create_timer(display_duration).timeout
	
	# Then fade out over fade_duration
	var fade_tween = create_tween()
	fade_tween.set_parallel(true)
	
	# Move upward while fading
	var start_pos = global_position
	var end_pos = start_pos + Vector2(0, -rise_distance)
	fade_tween.tween_property(self, "global_position", end_pos, fade_duration)
	
	# Fade out label
	if label:
		fade_tween.tween_property(label, "modulate:a", 0.0, fade_duration)
	
	# Fade out sprite
	if item_sprite and item_sprite.visible:
		fade_tween.tween_property(item_sprite, "modulate:a", 0.0, fade_duration)
	
	# Remove after fade animation completes
	fade_tween.tween_callback(queue_free).set_delay(fade_duration)

func _animate_coin():
	# Animate coin rotation during display (similar to loot coin animation)
	if not is_coin or not item_sprite or not item_sprite.visible:
		return
	
	# Use _process to animate coin frames continuously
	# We'll animate at 10 frames per second during the display
	var coin_anim_speed = 10.0  # Frames per second
	var coin_frame_time = 1.0 / coin_anim_speed  # Time per frame
	
	# Create a tween that cycles through frames
	var total_time = display_duration + fade_duration  # Total display time
	var frames_to_cycle = int(total_time * coin_anim_speed)
	
	# Animate coin frames
	for i in range(frames_to_cycle):
		var target_frame = i % item_sprite.hframes  # Cycle through 0-5
		await get_tree().create_timer(coin_frame_time).timeout
		if item_sprite and is_instance_valid(item_sprite) and item_sprite.visible:
			item_sprite.frame = target_frame
