#pragma once

#include <vector>

#include <iostream>

template<typename T>
class CircleBuffer
{
public:
    CircleBuffer(std::size_t size)
        : mData(size)
    {

    }

    void addItem(const T& item)
    {
        if (mEndPointer < mData.size())
        {
            mData[mEndPointer] = item;
            mEndPointer++;
        }
        else
        {
            mData[mStartPointer] = item;
            if (mStartPointer < mData.size() - 1)
            {
                mStartPointer++;
            }
            else
            {
                mStartPointer = 0;
            }
        }
    }

    std::size_t getNumItems() const
    {
        return mEndPointer;
    }

    const T& getItem(std::size_t index) const
    {
        if (mEndPointer < mData.size())
        {
            return mData[index];
        }
        else
        {
            auto offset = mStartPointer + index;
            if (offset >= mData.size())
            {
                offset -= mData.size();
            }
            return mData[offset];
        }
    }

private:
    std::size_t mStartPointer{0};
    std::size_t mEndPointer{0};
    std::vector<T> mData;
};
