//
// Created by bastian on 01.04.22.
//

#include "InterruptableSequence.h"

BT::InterruptableSequence::InterruptableSequence(const std::string &name, const NodeConfiguration &config)
        : ControlNode::ControlNode(name, config) {
}

void BT::InterruptableSequence::halt() {
    ControlNode::halt();
}

BT::NodeStatus BT::InterruptableSequence::tick() {
    auto result = children_nodes_[position]->executeTick();

    if(result==NodeStatus::FAILURE){
        resetChildren();
        position=0;
        setStatus(NodeStatus::FAILURE);
        return NodeStatus::FAILURE;
    }

    if(result==NodeStatus::SUCCESS){
        position++;
        if(position>=children_nodes_.size()){
            resetChildren();
            position=0;
            setStatus(NodeStatus::SUCCESS);
            return NodeStatus::SUCCESS;
        }
    }

    setStatus(NodeStatus::RUNNING);
    return NodeStatus::RUNNING;
}

BT::PortsList BT::InterruptableSequence::providedPorts() {
    return {};
}