using Godot;

namespace Mr.BrickAdventures.scripts.components;

[GlobalClass]
public partial class InvulnerabilityComponent : Node
{
    [Export] public float Duration { get; set; } = 1f;
    [Export] public FlashingComponent FlashingComponent { get; set; }
    
    private bool _isInvulnerable = false;

    public void Activate()
    {
        if (_isInvulnerable)
            return;
        
        _isInvulnerable = true;
        FlashingComponent?.StartFlashing();

        var timer = GetTree().CreateTimer(Duration);
        timer.Timeout += Deactivate;
    }

    private void Deactivate()
    {
        _isInvulnerable = false;
        FlashingComponent?.StopFlashing();
    }
    
    public bool IsInvulnerable() => _isInvulnerable;
}