extends Node

const GCD_TIME := 0.5
const ICON_SIZE := 20
const FONT_SIZE := 14
const BORDER_WIDTH := 2
const MARGIN := 2

var ability_labels: Array[String] = ["1", "2", "3", "4", "P"]
var effect_container: HBoxContainer = null
var tavern_bar: ProgressBar = null
var tavern_label: Label = null
var wave_label: Label = null
var level_label: Label = null
var xp_bar: ProgressBar = null
var xp_label: Label = null

func _ready() -> void:
  EventBus.health_changed.connect(_on_health_changed)
  EventBus.shield_changed.connect(_on_shield_changed)
  EventBus.entity_died.connect(_on_entity_died)
  EventBus.player_respawned.connect(_on_player_respawned)
  EventBus.respawn_tick.connect(_on_respawn_tick)
  EventBus.role_changed.connect(_on_role_changed)
  EventBus.cooldown_tick.connect(_on_cooldown_tick)
  EventBus.effect_applied.connect(_on_effect_applied)
  EventBus.effect_expired.connect(_on_effect_expired)
  EventBus.tavern_damaged.connect(_on_tavern_damaged)
  EventBus.wave_started.connect(_on_wave_started)
  EventBus.wave_timer_tick.connect(_on_wave_timer_tick)
  EventBus.xp_gained.connect(_on_xp_gained)
  EventBus.level_up.connect(_on_level_up)
  _init_hud.call_deferred()

func _init_hud() -> void:
  var hud: CanvasLayer = _get_hud()
  if not hud:
    return
  hud.get_node("RespawnTimer").visible = false
  effect_container = HBoxContainer.new()
  effect_container.name = "EffectContainer"
  effect_container.position = Vector2(10, 60)
  effect_container.add_theme_constant_override("separation", 3)
  hud.add_child(effect_container)
  _init_tavern_bar(hud)
  _init_xp_bar(hud)

func _init_tavern_bar(hud: CanvasLayer) -> void:
  var container := VBoxContainer.new()
  container.name = "TavernContainer"
  container.anchor_left = 0.5
  container.anchor_right = 0.5
  container.offset_left = -150
  container.offset_right = 150
  container.offset_top = 10
  container.add_theme_constant_override("separation", 2)
  wave_label = Label.new()
  wave_label.text = "Welle 1 — 60:00"
  wave_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  wave_label.add_theme_font_size_override("font_size", 18)
  container.add_child(wave_label)
  var title := Label.new()
  title.text = "Taverne"
  title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  title.add_theme_font_size_override("font_size", 14)
  container.add_child(title)
  tavern_bar = ProgressBar.new()
  tavern_bar.custom_minimum_size = Vector2(300, 22)
  tavern_bar.show_percentage = false
  var bg := StyleBoxFlat.new()
  bg.bg_color = Color(0.3, 0.1, 0.1, 1)
  var fill := StyleBoxFlat.new()
  fill.bg_color = Color(0.9, 0.7, 0.2, 1)
  tavern_bar.add_theme_stylebox_override("background", bg)
  tavern_bar.add_theme_stylebox_override("fill", fill)
  tavern_bar.max_value = 5000.0
  tavern_bar.value = 5000.0
  container.add_child(tavern_bar)
  tavern_label = Label.new()
  tavern_label.text = "5000/5000"
  tavern_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  tavern_label.anchor_left = 0.0
  tavern_label.anchor_right = 1.0
  tavern_bar.add_child(tavern_label)
  hud.add_child(container)

func _on_tavern_damaged(current: float, max_val: float) -> void:
  if tavern_bar:
    tavern_bar.max_value = max_val
    tavern_bar.value = current
  if tavern_label:
    tavern_label.text = "%d/%d" % [current, max_val]

func _init_xp_bar(hud: CanvasLayer) -> void:
  var container := VBoxContainer.new()
  container.name = "LevelContainer"
  container.anchor_left = 1.0
  container.anchor_top = 1.0
  container.anchor_right = 1.0
  container.anchor_bottom = 1.0
  container.offset_left = -260
  container.offset_top = -80
  container.offset_right = -10
  container.offset_bottom = -10
  container.add_theme_constant_override("separation", 2)
  level_label = Label.new()
  level_label.text = "Level 1"
  level_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
  level_label.add_theme_font_size_override("font_size", 20)
  container.add_child(level_label)
  xp_bar = ProgressBar.new()
  xp_bar.custom_minimum_size = Vector2(250, 22)
  xp_bar.max_value = 1
  xp_bar.value = 0
  xp_bar.show_percentage = false
  var bg := StyleBoxFlat.new()
  bg.bg_color = Color(0.1, 0.2, 0.1, 1)
  var fill := StyleBoxFlat.new()
  fill.bg_color = Color(0.3, 0.9, 0.3, 1)
  xp_bar.add_theme_stylebox_override("background", bg)
  xp_bar.add_theme_stylebox_override("fill", fill)
  xp_label = Label.new()
  xp_label.text = "0/1"
  xp_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  xp_label.anchor_left = 0.0
  xp_label.anchor_right = 1.0
  xp_bar.add_child(xp_label)
  container.add_child(xp_bar)
  hud.add_child(container)
  _update_xp_ui()

func _on_xp_gained(_player: Node, _amount: int) -> void:
  _update_xp_ui()

func _on_level_up(_player: Node, _new_level: int) -> void:
  _update_xp_ui()

func _update_xp_ui() -> void:
  if level_label:
    level_label.text = "Level %d" % PlayerData.level
  if xp_bar:
    xp_bar.max_value = PlayerData.xp_to_next
    xp_bar.value = PlayerData.xp
  if xp_label:
    xp_label.text = "%d/%d" % [PlayerData.xp, PlayerData.xp_to_next]

func _on_wave_started(_wave_number: int) -> void:
  _update_wave_label()

func _on_wave_timer_tick(_seconds_remaining: float) -> void:
  _update_wave_label()

func _update_wave_label() -> void:
  if not wave_label:
    return
  var secs: int = int(max(0.0, GameState.wave_timer_remaining))
  var mm: int = secs / 60
  var ss: int = secs % 60
  wave_label.text = "Welle %d — %02d:%02d" % [GameState.current_wave, mm, ss]

func _on_health_changed(entity: Node, current: float, max_val: float) -> void:
  if entity != PlayerData:
    return
  var hud: CanvasLayer = _get_hud()
  if not hud:
    return
  var bar: ProgressBar = hud.get_node("HealthBar")
  bar.max_value = max_val
  bar.value = current
  hud.get_node("HealthBar/HealthLabel").text = "%d/%d" % [current, max_val]

func _on_shield_changed(entity: Node, current: float, max_val: float) -> void:
  if entity != PlayerData:
    return
  var hud: CanvasLayer = _get_hud()
  if not hud:
    return
  var bar: ProgressBar = hud.get_node("ShieldBar")
  bar.max_value = max_val
  bar.value = current
  hud.get_node("ShieldBar/ShieldLabel").text = "%d/%d" % [current, max_val]

func _on_entity_died(entity: Node) -> void:
  if entity != PlayerData:
    return
  var hud: CanvasLayer = _get_hud()
  if hud:
    hud.get_node("RespawnTimer").visible = true

func _on_player_respawned(_player: Node) -> void:
  var hud: CanvasLayer = _get_hud()
  if hud:
    hud.get_node("RespawnTimer").visible = false

func _on_respawn_tick(timer: float) -> void:
  var hud: CanvasLayer = _get_hud()
  if hud:
    hud.get_node("RespawnTimer").text = str(ceil(timer))

func _on_role_changed(_player: Node, role_type: int) -> void:
  var hud: CanvasLayer = _get_hud()
  if not hud:
    return
  var icon: Label = hud.get_node("AbilityBar/ClassIcon/Label")
  match role_type:
    0: icon.text = "T"
    1: icon.text = "D"
    2: icon.text = "H"

func _on_cooldown_tick(cooldowns: Array, max_cooldowns: Array, gcd_timer: float) -> void:
  var hud: CanvasLayer = _get_hud()
  if not hud:
    return
  var panels: Array = [
    hud.get_node("AbilityBar/Ability1"),
    hud.get_node("AbilityBar/Ability2"),
    hud.get_node("AbilityBar/Ability3"),
    hud.get_node("AbilityBar/Ability4"),
    hud.get_node("AbilityBar/Ability5"),
  ]
  for i in range(min(panels.size(), cooldowns.size())):
    var panel: Panel = panels[i]
    var label: Label = panel.get_node("Label")
    var overlay: ColorRect = panel.get_node("CooldownOverlay")
    var cd: float = cooldowns[i]
    var gcd: float = gcd_timer if i != 2 and i != 4 else 0.0
    var active_cd: float = max(cd, gcd)
    var max_cd: float = max_cooldowns[i] if max_cooldowns[i] > 0 else GCD_TIME
    if active_cd > 0:
      var ratio: float = clamp(active_cd / max_cd, 0.0, 1.0)
      overlay.visible = true
      overlay.anchor_bottom = ratio
      label.text = str(ceil(active_cd))
    else:
      overlay.visible = false
      label.text = ability_labels[i]

func _on_effect_applied(target: Node, effect: Effect) -> void:
  if target != PlayerData:
    return
  if effect_container:
    _add_icon(effect)

func _on_effect_expired(target: Node, effect: Effect) -> void:
  if target != PlayerData:
    return
  if effect_container:
    _remove_icon(effect)

func _add_icon(effect: Effect) -> void:
  var panel := PanelContainer.new()
  var style := StyleBoxFlat.new()
  match effect.type:
    Effect.Type.AURA:
      style.bg_color = Color(0.15, 0.15, 0.3, 1)
      style.border_color = Color(0.3, 0.5, 1.0, 1)
    Effect.Type.BUFF:
      style.bg_color = Color(0.15, 0.3, 0.15, 1)
      style.border_color = Color(0.3, 1.0, 0.3, 1)
    Effect.Type.DEBUFF:
      style.bg_color = Color(0.3, 0.15, 0.15, 1)
      style.border_color = Color(1.0, 0.3, 0.3, 1)
  style.set_border_width_all(BORDER_WIDTH)
  style.set_content_margin_all(MARGIN)
  panel.add_theme_stylebox_override("panel", style)
  var label := Label.new()
  label.text = effect.effect_name.left(1)
  label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
  label.add_theme_font_size_override("font_size", FONT_SIZE)
  label.add_theme_color_override("font_color", Color.WHITE)
  label.custom_minimum_size = Vector2(ICON_SIZE, ICON_SIZE)
  panel.add_child(label)
  panel.custom_minimum_size = Vector2(ICON_SIZE + BORDER_WIDTH * 2, ICON_SIZE + BORDER_WIDTH * 2)
  panel.set_meta("effect_type", effect.type)
  panel.set_meta("effect_name", effect.effect_name)
  var insert_idx := 0
  for child in effect_container.get_children():
    if child.has_meta("effect_type") and child.get_meta("effect_type") <= effect.type:
      insert_idx += 1
    else:
      break
  effect_container.add_child(panel)
  effect_container.move_child(panel, insert_idx)

func _remove_icon(effect: Effect) -> void:
  for child in effect_container.get_children():
    if child.has_meta("effect_type") and child.has_meta("effect_name"):
      if child.get_meta("effect_type") == effect.type and child.get_meta("effect_name") == effect.effect_name:
        child.queue_free()
        return

func _get_hud() -> CanvasLayer:
  return get_tree().get_first_node_in_group("hud") as CanvasLayer
