#pragma once

#include <memory>

#include "core/music.h"
#include "tools/beatutils.h"

class BPMCalculator
{
public:
    explicit BPMCalculator();

    void start();
    void stop();
    void click(const kku::microsec& offset);

    bool calculating() const;

    const beat_utils::BeatInfo& fetchApproximatedInfo();
    kku::microsec fetchTimeUntilNextBeat(const kku::microsec& offset);

    kku::microsec getStartingOffset() const;
    void setStartingOffset(kku::microsec offset);
    void moveStartingOffsetBy(kku::microsec shift);

private:
    bool _need_recalculate;
    bool _calculating;

    std::vector<kku::microsec> _deltas;
    kku::microsec _previous_click_offset;
    kku::microsec _first_click_offset;

    beat_utils::BeatInfo _approximated_info;

    inline void reset();
};
