#ifndef BU_SPEEDOMETER_H
#define BU_SPEEDOMETER_H

#include "bu/mutex.h"

#include <stdint.h>

namespace Bu
{
    class Speedometer
    {
    public:
        Speedometer( int64_t iBucketMs, int32_t iBucketCount );
        virtual ~Speedometer();

        void addEvent();
        double getSpeed();
        int32_t getTotalEvents();
        void debug();

    private:
        void _updateBuckets( int64_t iNow );
        /**
         * Gets the current time in milliseconds.
         */
        int64_t getCurrentTime() const;

    private:
        uint32_t *aiBucket;
        uint32_t iTotal;
        int32_t iCapacity;
        int32_t iFill;
        int32_t iStart;
        int32_t iCurrent;
        int64_t iBucketMs;
        int64_t iStartTime;
        int64_t iWindowSizeMs;
        int64_t iLastUpdate;
        mutable Bu::Mutex mLock;
    };
}

#endif
