#include "MidiMetaEventAdapter.h"

#include "BinaryFile.h"
#include "ByteUtils.h"

#include <iostream>
#include <bitset>

int MidiMetaEventAdapter::ReadEvent(std::ifstream* file, MetaMidiEvent* event, int& lastMidiChannel)
{
    unsigned byteCount = 0;
    char c;
    file->get(c);
    byteCount++;

    event->SetType(c);

    std::cout << "Meta event type: " << std::hex << int(c) << std::dec<<std::endl;

    switch (event->GetType())
    {
    case MetaMidiEvent::Type::SEQ_NUM:
        byteCount += ReadIntEvent(file, event, 2);
        break;
    case MetaMidiEvent::Type::TEXT:
    case MetaMidiEvent::Type::COPYRIGHT:
    case MetaMidiEvent::Type::TRACK_NAME:
    case MetaMidiEvent::Type::INSTRUMENT_NAME:
    case MetaMidiEvent::Type::MARKER:
    case MetaMidiEvent::Type::CUE_POINT:
        byteCount += ReadStringEvent(file, event);
        break;
    case MetaMidiEvent::Type::CHANNEL_PREFIX:
        byteCount += ReadChannelPrefixEvent(file, event, lastMidiChannel);
        break;
    case MetaMidiEvent::Type::END_TRACK:
    {
        int length = 0;
        BinaryFile::GetNextByteAsInt(file, length);
        byteCount ++;
        break;
    }
    case MetaMidiEvent::Type::SMPTE_OFFSET:
        byteCount += ReadTimeCodeEvent(file, event);
        break;
    case MetaMidiEvent::Type::SET_TEMPO:
        byteCount += ReadIntEvent(file, event);
        break;
    case MetaMidiEvent::Type::TIME_SIG:
        byteCount += ReadTimeSignatureEvent(file, event);
        break;
    case MetaMidiEvent::Type::KEY_SIG:
        byteCount += ReadKeySignatureEvent(file, event);
        break;
    case MetaMidiEvent::Type::SEQ_CUSTOM:
        break;
    case MetaMidiEvent::Type::UNKNOWN:
        std::cout << "Unknown meta event " << std::bitset<8>(c) << "|"  <<std::endl;
        byteCount += ReadUnknownEvent(file);
        break;
    default:
        break;
    }
    return byteCount;
}

int MidiMetaEventAdapter::ReadUnknownEvent(std::ifstream* file)
{
    int length = 0;
    BinaryFile::GetNextByteAsInt(file, length);

    char c;
    for(unsigned idx=0; idx<length; idx++)
    {
        file->get(c);
    }
    return length;
}

int MidiMetaEventAdapter::ReadStringEvent(std::ifstream* file, MetaMidiEvent* event)
{
    unsigned byteCount = 0;

    int length = 0;
    BinaryFile::GetNextByteAsInt(file, length);
    byteCount++;

    std::string name;
    BinaryFile::GetNextString(file, name, length);
    byteCount += length;
    event->SetLabel(name);
    return byteCount;
}

int MidiMetaEventAdapter::ReadIntEvent(std::ifstream* file, MetaMidiEvent* event, int lengthIn)
{
    unsigned byteCount = 0;
    int length = 0;
    if(lengthIn > -1)
    {
        length = lengthIn;
    }
    else
    {
        BinaryFile::GetNextByteAsInt(file, length);
        byteCount ++;
    }

    std::string buffer;
    BinaryFile::GetNextNBytes(file, buffer.data(), length);
    byteCount += length;

    const int value = ByteUtils::ToInt(buffer.data(), length);
    event->SetValue(value);
    return byteCount;
}

int MidiMetaEventAdapter::ReadChannelPrefixEvent(std::ifstream* file, MetaMidiEvent* event, int& lastMidiChannel)
{
    unsigned byteCount = 0;
    int length = 0;
    BinaryFile::GetNextByteAsInt(file, length);
    byteCount ++;

    std::string buffer;
    BinaryFile::GetNextNBytes(file, buffer.data(), length);
    byteCount += length;

    const int value = ByteUtils::ToInt(buffer.data(), length);
    event->SetValue(value);
    lastMidiChannel = value;
    return byteCount;
}

int MidiMetaEventAdapter::ReadTimeSignatureEvent(std::ifstream* file, MetaMidiEvent* event)
{
    unsigned byteCount = 0;
    int length = 0;
    BinaryFile::GetNextByteAsInt(file, length);
    byteCount++;

    MidiTimeSignature timeSig;
    BinaryFile::GetNextByteAsInt(file, timeSig.mNumer);
    BinaryFile::GetNextByteAsInt(file, timeSig.mDenom);
    BinaryFile::GetNextByteAsInt(file, timeSig.mMetro);
    BinaryFile::GetNextByteAsInt(file, timeSig.mF32);
    byteCount +=4;

    if (length > 4)
    {
        char c;
        for(unsigned idx=0; idx<length-4; idx++)
        {
            file->get(c);
            byteCount++;
        }
    }
    return byteCount;
}

int MidiMetaEventAdapter::ReadKeySignatureEvent(std::ifstream* file, MetaMidiEvent* event)
{
    unsigned byteCount = 0;
    int length = 0;
    BinaryFile::GetNextByteAsInt(file, length);
    byteCount++;

    MidiKeySignature keySig;
    BinaryFile::GetNextByteAsInt(file, keySig.mSharpsFlats);
    BinaryFile::GetNextByteAsInt(file, keySig.mMinor);
    byteCount +=2;
    return byteCount;
}

int MidiMetaEventAdapter::ReadTimeCodeEvent(std::ifstream* file, MetaMidiEvent* event)
{
    unsigned byteCount = 0;
    int length = 0;
    BinaryFile::GetNextByteAsInt(file, length);
    byteCount++;

    MidiSmtpeTimecode timeCode;
    BinaryFile::GetNextByteAsInt(file, timeCode.mHr);
    BinaryFile::GetNextByteAsInt(file, timeCode.mMin);
    BinaryFile::GetNextByteAsInt(file, timeCode.mSec);
    BinaryFile::GetNextByteAsInt(file, timeCode.mFrame);
    BinaryFile::GetNextByteAsInt(file, timeCode.mFrameFrac);
    byteCount +=5;
    return byteCount;
}
