extends CanvasLayer

# Level Complete UI - Shows stats when level is completed

var title_label: Label = null
var stats_header_label: Label = null
var enemies_label: Label = null
var time_label: Label = null
var downed_label: Label = null
var exp_label: Label = null
var coins_label: Label = null

func _ready():
	visible = false
	_find_labels()

func _find_labels():
	# Find labels (works for both scene-based and programmatically created UI)
	var vbox = get_child(0) if get_child_count() > 0 else null
	if vbox:
		for child in vbox.get_children():
			if child is Label:
				# Check by name first (more reliable)
				if child.name == "TitleLabel" or (title_label == null and (child.text == "LEVEL COMPLETE!" or child.text.begins_with("LEVEL"))):
					title_label = child
				elif child.name == "StatsHeaderLabel" or (stats_header_label == null and child.text == "stats"):
					stats_header_label = child
				# Check by name for other labels
				elif child.name == "EnemiesLabel":
					enemies_label = child
				elif child.name == "TimeLabel":
					time_label = child
				elif child.name == "DownedLabel":
					downed_label = child
				elif child.name == "ExpLabel":
					exp_label = child
				elif child.name == "CoinsLabel":
					coins_label = child
			elif child is VBoxContainer:
				# Stats container
				for stat_child in child.get_children():
					if stat_child.name == "EnemiesLabel":
						enemies_label = stat_child
					elif stat_child.name == "TimeLabel":
						time_label = stat_child
					elif stat_child.name == "DownedLabel":
						downed_label = stat_child
					elif stat_child.name == "ExpLabel":
						exp_label = stat_child
					elif stat_child.name == "CoinsLabel":
						coins_label = stat_child

func show_stats(enemies_defeated: int, times_downed: int, exp_collected: float, coins_collected: int, level_time: float, level_number: int = 1):
	# Ensure labels are found (in case _ready() hasn't run yet)
	_find_labels()
	
	# Update labels
	if title_label:
		title_label.text = "LEVEL " + str(level_number) + " COMPLETE!"
	else:
		print("LevelCompleteUI: WARNING - title_label not found!")
	
	if stats_header_label:
		stats_header_label.text = "stats"
	
	if enemies_label:
		enemies_label.text = "Enemies defeated: " + str(enemies_defeated)
		enemies_label.visible = true
	else:
		print("LevelCompleteUI: WARNING - enemies_label not found!")
	
	# Format level time as MM:SS
	if time_label:
		var minutes = int(level_time / 60.0)
		var seconds = int(fmod(level_time, 60.0))
		time_label.text = "Finish time: %02d:%02d" % [minutes, seconds]
		time_label.visible = true
	else:
		print("LevelCompleteUI: WARNING - time_label not found!")
	
	# Hide labels that are not needed (per user request: only show enemies, coins, and time)
	if downed_label:
		downed_label.visible = false
		downed_label.text = "Times Downed: " + str(times_downed)
	
	if exp_label:
		exp_label.visible = false
		exp_label.text = "EXP Collected: " + str(int(exp_collected))
	
	if coins_label:
		coins_label.text = "Coins collected: " + str(coins_collected)
		coins_label.visible = true
	else:
		print("LevelCompleteUI: WARNING - coins_label not found!")
	
	# Show UI
	visible = true
	
	# Fade in - fade the VBoxContainer and all labels
	var vbox = get_child(0) if get_child_count() > 0 else null
	if vbox:
		vbox.modulate.a = 0.0
		var fade_in = create_tween()
		fade_in.set_parallel(true)
		fade_in.tween_property(vbox, "modulate:a", 1.0, 0.5)
		
		# Also fade individual labels if they exist
		if title_label:
			title_label.modulate.a = 0.0
			fade_in.tween_property(title_label, "modulate:a", 1.0, 0.5)
		if stats_header_label:
			stats_header_label.modulate.a = 0.0
			fade_in.tween_property(stats_header_label, "modulate:a", 1.0, 0.5)
		if enemies_label:
			enemies_label.modulate.a = 0.0
			fade_in.tween_property(enemies_label, "modulate:a", 1.0, 0.5)
		if time_label:
			time_label.modulate.a = 0.0
			fade_in.tween_property(time_label, "modulate:a", 1.0, 0.5)
		if downed_label:
			downed_label.modulate.a = 0.0
			fade_in.tween_property(downed_label, "modulate:a", 1.0, 0.5)
		if exp_label:
			exp_label.modulate.a = 0.0
			fade_in.tween_property(exp_label, "modulate:a", 1.0, 0.5)
		if coins_label:
			coins_label.modulate.a = 0.0
			fade_in.tween_property(coins_label, "modulate:a", 1.0, 0.5)
