/***************************************************************************
*                                                                          *
*  Copyright (C) Golaem S.A.  All Rights Reserved.                         *
*                                                                          *
***************************************************************************/

#pragma once

#include "glmAbstractParentBehavior.h"
#include "glmAbstractTrigger.h"

namespace glm
{
    class GLM_SDK_API LoopBehaviorOperator : public AbstractParentBehavior
    {
    public:
        bool _checkConditionFirst;

    private:
        AbstractTrigger::SP _condition;

    public:
        GLM_CLASS_STAT_ID( glm::MemoryStatIds::Simulation)

        LoopBehaviorOperator(bool checkConditionFirst = false, const AbstractTrigger::SP& condition = NULL);
        virtual ~LoopBehaviorOperator();

        void setCondition(const AbstractTrigger::SP& condition);
        inline const AbstractTrigger::SP& getCondition() const;

        inline virtual bool hasChildLooping(const AbstractBehavior* /*child*/) const override;

        virtual void allocate(BehaviorGraph& graph) override;
        virtual void resize(BehaviorGraph& graph) override;
#ifndef NDEBUG
        virtual void init(BehaviorGraph& graph) override;
#endif
        virtual void update(int64_t index, BehaviorGraph& graph, Time dt) override;
        virtual void requestStart(int64_t index, BehaviorGraph& graph) override;
        virtual void requestStop(int64_t index, BehaviorGraph& graph, unsigned int next = _nextInvalidId) override;

        // type checking - value derived from this type
        virtual bool isThisType(const AbstractBehavior* value) const override;

    protected:
        // cloning
        friend class CloneFactory;
        virtual void cloneImpl(AbstractBehavior* clone, CloneFactory* factory) const override;
        inline virtual LoopBehaviorOperator* create() const override;
    };

    //-------------------------------------------------------------------------
    inline const AbstractTrigger::SP& LoopBehaviorOperator::getCondition() const
    {
        return _condition;
    }

    //-------------------------------------------------------------------------
    inline bool LoopBehaviorOperator::hasChildLooping(const AbstractBehavior* /*child*/) const
    {
        return true;
    }

    //-------------------------------------------------------------------------
    inline LoopBehaviorOperator* LoopBehaviorOperator::create() const
    {
        LoopBehaviorOperator* myNewLoopBehaviorOperator = GLM_NEW(LoopBehaviorOperator, myNewLoopBehaviorOperator, getClassMemoryStatId());
        return myNewLoopBehaviorOperator;
    }
} // namespace glm
