extends CanvasLayer

@onready var health_bar: ProgressBar = $Panel/VBox/Health
@onready var shadow_bar: ProgressBar = $Panel/VBox/Shadow
@onready var status_label: Label = $Panel/VBox/Status
@onready var wave_label: Label = $Wave
@onready var xp_label: Label = $XP
@onready var score_label: Label = $Score
@onready var abilities_label: Label = $Abilities
@onready var boss_panel: Panel = $BossPanel
@onready var boss_name_label: Label = $BossPanel/VBox/BossName
@onready var boss_health_bar: ProgressBar = $BossPanel/VBox/BossHealth
@onready var victory_panel: Panel = $VictoryPanel
@onready var victory_title: Label = $VictoryPanel/VBox/VictoryTitle
@onready var victory_label: Label = $VictoryPanel/VBox/VictoryText
@onready var victory_stats: Label = $VictoryPanel/VBox/VictoryStats
@onready var victory_prompt: Label = $VictoryPanel/VBox/VictoryPrompt
@onready var title_panel: Panel = $TitlePanel
@onready var boss_banner: Panel = $BossBanner
@onready var boss_banner_title: Label = $BossBanner/VBox/BannerTitle
@onready var boss_banner_text: Label = $BossBanner/VBox/BannerText

func _ready() -> void:
	_apply_readable_fonts()

func set_stats(health: float, max_health: float, shadow: float, max_shadow: float) -> void:
	health_bar.max_value = max_health
	health_bar.value = health
	shadow_bar.max_value = max_shadow
	shadow_bar.value = shadow

func set_status(text: String) -> void:
	status_label.text = text

func set_wave(wave: int) -> void:
	wave_label.text = "Wave %d" % wave

func set_progress(xp: int, xp_to_next: int, level: int, score: int) -> void:
	xp_label.text = "Level %d  XP %d / %d" % [level, xp, xp_to_next]
	score_label.text = "Score %d" % score

func set_cooldowns(cooldowns: Dictionary) -> void:
	var lines: Array[String] = []
	for ability_name in cooldowns.keys():
		var data: Dictionary = cooldowns[ability_name]
		var cooldown: float = data.get("cooldown", 0.0)
		var cost: float = data.get("cost", 0.0)
		var ready_text: String = "Ready" if cooldown <= 0.05 else "%.1fs" % cooldown
		var cost_text: String = "" if cost <= 0.0 else " | %.0f Shadow" % cost
		lines.append("%s: %s%s" % [ability_name, ready_text, cost_text])
	abilities_label.text = "\n".join(lines)

func show_boss(boss_name: String, health: float, max_health: float) -> void:
	boss_panel.visible = true
	boss_name_label.text = boss_name
	set_boss_health(health, max_health)

func set_boss_health(health: float, max_health: float) -> void:
	boss_health_bar.max_value = max_health
	boss_health_bar.value = maxf(0.0, health)

func hide_boss() -> void:
	boss_panel.visible = false

func show_victory(title: String, text: String, level: int, score: int) -> void:
	victory_panel.visible = true
	victory_title.text = title
	victory_label.text = text
	victory_stats.text = "Level %d  |  Score %d" % [level, score]
	victory_prompt.text = "Press R to replay the demo"

func hide_victory() -> void:
	victory_panel.visible = false

func show_title_screen() -> void:
	title_panel.visible = true
	set_gameplay_hud_visible(false)
	hide_boss()
	hide_victory()
	hide_boss_banner()
	set_wave(1)
	set_progress(0, 100, 1, 0)
	set_status("Before Olympus, there was Night.")

func hide_title_screen() -> void:
	title_panel.visible = false
	set_gameplay_hud_visible(true)

func show_boss_banner(title: String, text: String) -> void:
	boss_banner.visible = true
	boss_banner_title.text = title
	boss_banner_text.text = text

func hide_boss_banner() -> void:
	boss_banner.visible = false

func set_gameplay_hud_visible(visible_state: bool) -> void:
	$Panel.visible = visible_state
	abilities_label.visible = visible_state
	wave_label.visible = visible_state
	xp_label.visible = visible_state
	score_label.visible = visible_state

func _apply_readable_fonts() -> void:
	var gameplay_title: Label = $Panel/VBox/Title
	gameplay_title.add_theme_font_size_override("font_size", 20)
	status_label.add_theme_font_size_override("font_size", 19)
	abilities_label.add_theme_font_size_override("font_size", 19)
	wave_label.add_theme_font_size_override("font_size", 22)
	xp_label.add_theme_font_size_override("font_size", 19)
	score_label.add_theme_font_size_override("font_size", 19)
	boss_name_label.add_theme_font_size_override("font_size", 22)
	victory_title.add_theme_font_size_override("font_size", 27)
	victory_label.add_theme_font_size_override("font_size", 21)
	victory_stats.add_theme_font_size_override("font_size", 22)
	victory_prompt.add_theme_font_size_override("font_size", 20)
	boss_banner_title.add_theme_font_size_override("font_size", 30)
	boss_banner_text.add_theme_font_size_override("font_size", 21)
	var title_label: Label = $TitlePanel/VBox/Title
	var subtitle_label: Label = $TitlePanel/VBox/Subtitle
	var objective_label: Label = $TitlePanel/VBox/Objective
	var controls_label: Label = $TitlePanel/VBox/Controls
	var start_label: Label = $TitlePanel/VBox/Start
	title_label.add_theme_font_size_override("font_size", 30)
	subtitle_label.add_theme_font_size_override("font_size", 22)
	objective_label.add_theme_font_size_override("font_size", 20)
	controls_label.add_theme_font_size_override("font_size", 18)
	start_label.add_theme_font_size_override("font_size", 20)
