using System;
using RebootKit.Engine.Simulation;

namespace RebootKit.Engine.AI {
    public class Sequence : BehaviourNode {
        readonly Func<bool> m_Condition;
        
        public Sequence(string name, Func<bool> condition = null) : base(name) {
            m_Condition = condition;
        }

        public override Status Process(Actor target, float dt) {
            if (m_Condition != null && !m_Condition()) {
                return Status.Failure;
            }

            if (m_CurrentChild < Children.Count) {
                Status status = Children[m_CurrentChild].Process(target, dt);
            
                switch (status) {
                case Status.Success:
                    m_CurrentChild += 1;
                    return m_CurrentChild == Children.Count ? Status.Success : Status.Running;
                
                case Status.Failure:
                    Reset();
                    return Status.Failure;

                case Status.Running:
                    return Status.Running;
                }     
            }

            Reset();
            return Status.Success;
        }
    }
}