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

#pragma once

#include "glmSDKAPI.h"
#include <glmAbstractTrigger.h>

namespace glm
{
    // Trigger counting the number of call and enabled when a threshold count is reached/exceeded
    class GLM_SDK_API CountTrigger : public AbstractTrigger
    {

    public:
        unsigned int _condition;

    private:
        glm::PODArray<uint32_t> _callsCount;
        glm::PODArray<unsigned int> _thresholdCounts;
        unsigned int _defaultThresholdCount;

    public:
        typedef SmartPointer<CountTrigger> SP;

        GLM_CLASS_STAT_ID( glm::MemoryStatIds::Simulation);

        CountTrigger(ComparisonOperator::Value condition = ComparisonOperator::EQUAL);
        virtual ~CountTrigger();

        inline unsigned int getTriggerCount(int64_t index) const;
        inline unsigned int getCurrentCount(int64_t index) const;
        inline void setTriggerCount(int64_t index, unsigned int count);
        inline void setDefaultTriggerCount(unsigned int count);

        virtual void allocate(BehaviorGraph& behaviorGraph) override;
        virtual void resize(BehaviorGraph& behaviorGraph) override;
		virtual bool isShareable() override;

    protected:
        virtual bool doCall(int64_t index, AbstractBehavior* behavior, BehaviorGraph& behaviorGraph, bool isAttached) override;
        virtual void doReset(int64_t index, AbstractBehavior* behavior, BehaviorGraph& behaviorGraph) override;

        // cloning
        friend class CloneFactory;
        virtual void cloneImpl(AbstractTrigger* clone, CloneFactory* factory) const override;
        inline virtual CountTrigger* create() const override;
    };

    //-------------------------------------------------------------------------
    inline unsigned int CountTrigger ::getTriggerCount(int64_t index) const
    {
        return _thresholdCounts[index];
    }

    //-------------------------------------------------------------------------
    inline unsigned int CountTrigger::getCurrentCount(int64_t index) const
    {
        return _callsCount[index];
    }

    //-------------------------------------------------------------------------
    inline void CountTrigger::setTriggerCount(int64_t index, unsigned int count)
    {
        _thresholdCounts[index] = count;
    }

    //-------------------------------------------------------------------------
    inline void CountTrigger::setDefaultTriggerCount(unsigned int count)
    {
        _defaultThresholdCount = count;
    }

    //-------------------------------------------------------------------------
    inline CountTrigger* CountTrigger::create() const
    {
        CountTrigger* myNewCountTrigger = GLM_NEW(CountTrigger, myNewCountTrigger, getClassMemoryStatId());
        return myNewCountTrigger;
    }

} // namespace glm
