class_name Ability extends Node2D

@export var cooldown: float = 5.0
@export var effect_duration: float = 3.0

var modifiers: Array = []
var can_activate: bool = true
var cooldown_timer: Timer
var effect_timer: Timer
var is_active: bool = false

func _ready():
    cooldown_timer = Timer.new()
    add_child(cooldown_timer)
    cooldown_timer.one_shot = true
    cooldown_timer.connect("timeout", _on_cooldown_timeout)
    
    effect_timer = Timer.new()
    add_child(effect_timer)
    effect_timer.one_shot = true
    effect_timer.connect("timeout", _on_effect_timeout)

func activate():
    if !can_activate:
        return
        
    # Apply ability effects
    _start_ability()
    
    # Apply modifiers to the ability
    for modifier in modifiers:
        modifier.modify_ability(self)
    
    is_active = true
    can_activate = false
    effect_timer.start(effect_duration)
    
func add_modifier(modifier: Modifier):
    modifiers.append(modifier)
    modifier.on_equip(self)
    
func remove_modifier(modifier_id: String):
    for i in range(modifiers.size()):
        if modifiers[i].id == modifier_id:
            modifiers[i].on_unequip(self)
            modifiers.remove_at(i)
            break
    
# Override in child classes
func _start_ability():
    pass
    
# Override in child classes
func _end_ability():
    pass
    
func _on_effect_timeout():
    is_active = false
    _end_ability()
    cooldown_timer.start(cooldown)
    
func _on_cooldown_timeout():
    can_activate = true