extends Node

var ability_sets: Dictionary = {}

func _ready() -> void:
  ability_sets[GameState.ROLE_TANK] = _build_tank_set()
  ability_sets[GameState.ROLE_DAMAGE] = _build_damage_set()
  ability_sets[GameState.ROLE_HEALER] = _build_healer_set()
  EventBus.role_changed.connect(_on_role_changed)

func get_set(role: int) -> AbilitySet:
  return ability_sets.get(role, ability_sets[GameState.ROLE_DAMAGE])

func _on_role_changed(player: Node, role: int) -> void:
  var set: AbilitySet = get_set(role)
  var passive: Ability = null
  for a in set.abilities:
    if a.type == Ability.Type.PASSIVE:
      passive = a
      break
  var effects: Node = get_node_or_null("../EffectSystem")
  if effects and passive and effects.has_method("apply_passive_aura"):
    effects.apply_passive_aura(player, passive)

func _make_ability(name: String, type: int, damage: float, ability_range: float, cooldown: float, opts: Dictionary = {}) -> Ability:
  var a := Ability.new()
  a.ability_name = StringName(name)
  a.type = type
  a.damage = damage
  a.ability_range = ability_range
  a.cooldown = cooldown
  a.uses_gcd = opts.get("uses_gcd", true)
  a.aoe_radius = opts.get("aoe_radius", 0.0)
  a.is_heal = opts.get("is_heal", false)
  a.shield_value = opts.get("shield_value", 0.0)
  a.shield_multiplier = opts.get("shield_multiplier", 0.0)
  a.passive_stat = opts.get("passive_stat", &"")
  a.passive_value = opts.get("passive_value", 0.5)
  a.passive_radius = opts.get("passive_radius", 50.0)
  a.element = opts.get("element", Element.NONE)
  return a

func _build_tank_set() -> AbilitySet:
  var s := AbilitySet.new()
  s.role_name = &"Tank"
  s.color = Color(0.3, 0.5, 0.95)
  s.aa_damage = 5.0
  s.aa_range = 3.0
  s.aa_is_heal = false
  s.abilities = [
    _make_ability("Smash", Ability.Type.SINGLE, 15.0, 3.0, 2.0, {"shield_value": 10.0}),
    _make_ability("Sweep", Ability.Type.AOE, 10.0, 3.0, 3.0, {"aoe_radius": 10.0, "shield_value": 5.0}),
    _make_ability("Bulwark", Ability.Type.UTILITY, 0.0, 0.0, 5.0, {"uses_gcd": false, "shield_multiplier": 1.0}),
    _make_ability("Aegis", Ability.Type.ULT, 0.0, 0.0, 20.0, {"shield_multiplier": 3.0}),
    _make_ability("Fortify", Ability.Type.PASSIVE, 0.0, 0.0, 0.0, {"passive_stat": &"buff_shield", "passive_value": 0.5, "passive_radius": 50.0})
  ]
  return s

func _build_damage_set() -> AbilitySet:
  var s := AbilitySet.new()
  s.role_name = &"Damage"
  s.color = Color(0.95, 0.3, 0.3)
  s.aa_damage = 10.0
  s.aa_range = 10.0
  s.aa_is_heal = false
  s.abilities = [
    _make_ability("Bolt", Ability.Type.SINGLE, 30.0, 20.0, 2.0, {"element": Element.FIRE}),
    _make_ability("Inferno", Ability.Type.AOE, 20.0, 20.0, 3.0, {"aoe_radius": 5.0, "element": Element.FIRE}),
    _make_ability("Barrier", Ability.Type.UTILITY, 0.0, 0.0, 5.0, {"uses_gcd": false, "shield_multiplier": 1.0}),
    _make_ability("Rain", Ability.Type.ULT, 50.0, 20.0, 15.0, {"aoe_radius": 3.0, "element": Element.FIRE}),
    _make_ability("Power", Ability.Type.PASSIVE, 0.0, 0.0, 0.0, {"passive_stat": &"buff_damage", "passive_value": 0.5, "passive_radius": 50.0})
  ]
  return s

func _build_healer_set() -> AbilitySet:
  var s := AbilitySet.new()
  s.role_name = &"Healer"
  s.color = Color(0.4, 0.85, 0.4)
  s.aa_damage = 1.0
  s.aa_range = 20.0
  s.aa_is_heal = true
  s.abilities = [
    _make_ability("Mend", Ability.Type.SINGLE, 15.0, 20.0, 2.0, {"is_heal": true}),
    _make_ability("Bloom", Ability.Type.AOE, 10.0, 20.0, 3.0, {"aoe_radius": 20.0, "is_heal": true}),
    _make_ability("Ward", Ability.Type.UTILITY, 0.0, 0.0, 5.0, {"uses_gcd": false, "shield_multiplier": 1.0}),
    _make_ability("Sanctuary", Ability.Type.ULT, 25.0, 20.0, 15.0, {"aoe_radius": 3.0, "is_heal": true}),
    _make_ability("Devotion", Ability.Type.PASSIVE, 0.0, 0.0, 0.0, {"passive_stat": &"buff_heal", "passive_value": 0.5, "passive_radius": 50.0})
  ]
  return s
