using Godot;
using Godot.Collections;

namespace Cirno.Scripts.Resources.Events;

[GlobalClass]
public partial class ActivateEvent : EventResource
{
    [Export] public ActivationType ActivationType = ActivationType.Toggle;
    
    [Export] public Array<NodePath> Targets;

    private Node2D _parent;
    
    private bool _isComplete = false;
    
    protected void ActivateTargets()
    {
        foreach (var activationTarget in Targets)
        {
            ActivateTarget(_parent.GetNode<Node2D>(activationTarget));
        }
    }
    
    private bool ActivateTarget(Node2D activationTarget)
    {
        if (activationTarget is not IActivable target)
        {
            GD.PrintErr($"Target {activationTarget.Name} is not activable");
            return false;
        }
        
        target?.Activate(ActivationType);
        
        GD.Print($"{activationTarget.Name} activated");

        return true;
    }
    
    public override void Init(Node2D parent)
    {
        _parent = parent;
    }

    public override void Start(Node2D parent)
    {
        
        ActivateTargets();
        _isComplete = true;
    }

    public override void UpdateEvent(double delta)
    {
        
    }

    public override bool IsComplete()
    {
        return _isComplete;
    }
}