extends Node

const ICON_SIZE := 10
const FONT_SIZE := 7
const BORDER_WIDTH := 1
const ICON_MARGIN := 0
const BASE_HEIGHT := 29

var styles: Dictionary = {}

func _ready() -> void:
  EventBus.health_changed.connect(_on_health_changed)
  EventBus.shield_changed.connect(_on_shield_changed)
  EventBus.target_changed.connect(_on_target_changed)
  EventBus.entity_died.connect(_on_entity_died)
  EventBus.effect_applied.connect(_on_effect_applied)
  EventBus.effect_expired.connect(_on_effect_expired)
  EventBus.portal_spawn.connect(_on_portal_spawn)
  EventBus.invasion_started.connect(_on_invasion_started)
  _init_nameplates.call_deferred()

func _on_invasion_started(enemies: Array) -> void:
  for enemy in enemies:
    if is_instance_valid(enemy):
      _setup_nameplate.call_deferred(enemy)

func _init_nameplates() -> void:
  for enemy in get_tree().get_nodes_in_group("enemies"):
    _setup_nameplate(enemy)
  for portal in get_tree().get_nodes_in_group("portals"):
    _setup_nameplate(portal)

func _setup_nameplate(entity: Node) -> void:
  var nameplate: Sprite3D = entity.get_node_or_null("Healthbar")
  if not nameplate:
    return
  var viewport: SubViewport = nameplate.get_node("SubViewport")
  nameplate.texture = viewport.get_texture()
  var border: ColorRect = viewport.get_node_or_null("Border")
  if border:
    border.visible = false

func _process(_delta: float) -> void:
  var player: Node = get_tree().get_first_node_in_group("player")
  for enemy in get_tree().get_nodes_in_group("enemies"):
    if not is_instance_valid(enemy):
      continue
    var nameplate: Sprite3D = enemy.get_node_or_null("Healthbar")
    if not nameplate:
      continue
    var health_bar: ProgressBar = nameplate.get_node("SubViewport/HealthBar")
    var data_source: Node = _get_data_source(enemy)
    if not data_source:
      continue
    if enemy not in styles:
      var style_normal: StyleBoxFlat = health_bar.get_theme_stylebox("fill").duplicate()
      var style_aggro: StyleBoxFlat = style_normal.duplicate()
      style_aggro.bg_color = Color(0.2, 0.4, 0.9, 1)
      styles[enemy] = { "normal": style_normal, "aggro": style_aggro }
    var s: Dictionary = styles[enemy]
    var enemy_target: Variant = data_source.get_stat(enemy, "target")
    if player and enemy_target == player:
      health_bar.add_theme_stylebox_override("fill", s["aggro"])
    else:
      health_bar.add_theme_stylebox_override("fill", s["normal"])

func _on_health_changed(entity: Node, current: float, max_val: float) -> void:
  if entity == PlayerData:
    return
  if not is_instance_valid(entity):
    return
  var nameplate: Sprite3D = entity.get_node_or_null("Healthbar")
  if not nameplate:
    return
  if not nameplate.texture:
    _setup_nameplate(entity)
  var bar: ProgressBar = nameplate.get_node("SubViewport/HealthBar")
  bar.max_value = max_val
  bar.value = current

func _on_shield_changed(entity: Node, current: float, max_val: float) -> void:
  if entity == PlayerData:
    return
  if not is_instance_valid(entity):
    return
  var nameplate: Sprite3D = entity.get_node_or_null("Healthbar")
  if not nameplate:
    return
  if not nameplate.texture:
    _setup_nameplate(entity)
  var bar: ProgressBar = nameplate.get_node_or_null("SubViewport/ShieldBar")
  if not bar:
    return
  if max_val <= 0:
    bar.visible = false
    return
  bar.visible = true
  bar.max_value = max_val
  bar.value = current

func _on_target_changed(_player: Node, target: Node) -> void:
  for enemy in get_tree().get_nodes_in_group("enemies"):
    if not is_instance_valid(enemy):
      continue
    var nameplate: Sprite3D = enemy.get_node_or_null("Healthbar")
    if nameplate:
      if not nameplate.texture:
        _setup_nameplate(enemy)
      nameplate.get_node("SubViewport/Border").visible = (target == enemy)
  for portal in get_tree().get_nodes_in_group("portals"):
    if not is_instance_valid(portal):
      continue
    var nameplate: Sprite3D = portal.get_node_or_null("Healthbar")
    if nameplate:
      if not nameplate.texture:
        _setup_nameplate(portal)
      nameplate.get_node("SubViewport/Border").visible = (target == portal)

func _on_entity_died(entity: Node) -> void:
  if entity != PlayerData and is_instance_valid(entity):
    styles.erase(entity)

func _on_effect_applied(target: Node, effect: Effect) -> void:
  if target == PlayerData:
    return
  if not is_instance_valid(target):
    return
  var nameplate: Sprite3D = target.get_node_or_null("Healthbar")
  if not nameplate:
    return
  var container: HBoxContainer = _get_or_create_effect_container(nameplate)
  _add_icon(container, effect)
  _resize_viewport(nameplate)

func _on_effect_expired(target: Node, effect: Effect) -> void:
  if target == PlayerData:
    return
  if not is_instance_valid(target):
    return
  var nameplate: Sprite3D = target.get_node_or_null("Healthbar")
  if not nameplate:
    return
  var container: HBoxContainer = nameplate.get_node_or_null("SubViewport/EffectContainer")
  if container:
    _remove_icon(container, effect)
    _resize_viewport.call_deferred(nameplate)

func _on_portal_spawn(_portal: Node, enemies: Array) -> void:
  for enemy in enemies:
    _setup_nameplate.call_deferred(enemy)

func _get_or_create_effect_container(nameplate: Sprite3D) -> HBoxContainer:
  var viewport: SubViewport = nameplate.get_node("SubViewport")
  var container: HBoxContainer = viewport.get_node_or_null("EffectContainer")
  if container:
    return container
  container = HBoxContainer.new()
  container.name = "EffectContainer"
  var health_bar: ProgressBar = viewport.get_node("HealthBar")
  var shield_bar: ProgressBar = viewport.get_node_or_null("ShieldBar")
  var y_pos: float = 0.0
  if shield_bar and shield_bar.visible:
    y_pos = shield_bar.offset_bottom + 2
  else:
    y_pos = health_bar.offset_bottom + 2
  container.position = Vector2(2, y_pos)
  container.add_theme_constant_override("separation", 1)
  viewport.add_child(container)
  return container

func _resize_viewport(nameplate: Sprite3D) -> void:
  var viewport: SubViewport = nameplate.get_node("SubViewport")
  var border: ColorRect = viewport.get_node("Border")
  var container: HBoxContainer = viewport.get_node_or_null("EffectContainer")
  if not container:
    return
  var icon_count := 0
  for child in container.get_children():
    if not child.is_queued_for_deletion():
      icon_count += 1
  if icon_count > 0:
    var needed: int = int(container.position.y) + ICON_SIZE + 4
    viewport.size.y = max(BASE_HEIGHT, needed)
    border.offset_bottom = viewport.size.y
  else:
    viewport.size.y = BASE_HEIGHT
    border.offset_bottom = BASE_HEIGHT

func _add_icon(container: HBoxContainer, 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(ICON_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 container.get_children():
    if child.has_meta("effect_type") and child.get_meta("effect_type") <= effect.type:
      insert_idx += 1
    else:
      break
  container.add_child(panel)
  container.move_child(panel, insert_idx)

func _remove_icon(container: HBoxContainer, effect: Effect) -> void:
  for child in 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_data_source(entity: Node) -> Node:
  if entity.is_in_group("boss"):
    return BossData
  elif entity.is_in_group("enemies"):
    return EnemyData
  return null
