#pragma once

#include "MidiDocument.h"
#include "MetaMidiEvent.h"
#include "MidiTrack.h"
#include "MidiChannelEvent.h"
#include "File.h"

#include <filesystem>
#include <string>

using Path = std::filesystem::path;

class MidiReader
{
    static constexpr const char TrackChunkLabel[] = "MTrk";
    static constexpr const char HeaderLabel[] = "MThd";

public:
    MidiReader();

    MidiDocument* getDocument() const;

    void read(const Path& path);

private:
    bool processHeader();
    bool processTrackChunk(bool debug=false);
    int processEvent(MidiTrack* track);

private:
    std::unique_ptr<File> mFile;
    MidiDocumentPtr mDocument;
    int mLastMidiChannel {0};
    MidiChannelEvent::Type mLastChannelEventType;
};
