using Cirno.Scripts.Actors;
using Cirno.Scripts.AttackPatterns;
using Godot;
using Godot.Collections;

namespace Cirno.Scripts.Resources;

[GlobalClass]
[Tool]
public partial class BossPhase : Resource
{
    [Export] public string PhaseName = string.Empty;
    [Export] public int Threshold;
    [Export] public bool PlayAnimation;
    [Export] public Array<AttackPattern> Patterns;

    private int currentPatternIndex = 0;
    private double patternTimer;

    private IPatternMachine _patternMachine;
    
    public void Start(Node boss)
    {
        currentPatternIndex = 0;
        _patternMachine = Patterns[currentPatternIndex].MakeMachine(boss);
        _patternMachine.Start();
        //Patterns[currentPatternIndex].Start(boss);
    }

    public void UpdatePhase(double delta)
    {
        patternTimer += delta;
        var currentPattern = Patterns[currentPatternIndex];

        _patternMachine.UpdatePattern(delta);

        if (!currentPattern.WaitForCompletion || _patternMachine.IsComplete())
        {
            currentPatternIndex = (currentPatternIndex + 1) % Patterns.Count;
            var oldParent = _patternMachine.Parent;
            _patternMachine = Patterns[currentPatternIndex].MakeMachine(oldParent);
            _patternMachine.Start();
            //_patternMachine.Start(currentPattern.Parent);
        }
    }
}