#include "classicgame.h"
#include "classicmode/classicnote.h"
#include "classicmapcreator.h"
#include "gamecontext.h"
#include "holdmanager.h"
#include "graphics/classicgraphicsmanager.h"

ClassicGame::ClassicGame(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
                         const std::shared_ptr<GameContext>& context) :
    _timeline(timeline),
    _context(context)
{}

void ClassicGame::run()
{
    auto beatmap = classic::createBeatmap("aa", _context);
    _timeline->setNotes(beatmap.notes);
}

void ClassicGame::input(kku::GameEvent&& input)
{
    switch (input.event.type)
    {
    default:
        return;
        break;

    case kku::SystemEvent::Type::KeyPress:
    {
        auto note_it = _timeline->getActiveNote(input.timestamp);

        if (!_timeline->isExpired(note_it))
        {
            auto note = (*note_it);
            note->input(std::move(input));
        }
    }
    break;

    case kku::SystemEvent::Type::KeyRelease:
    {
        _context->getHoldManager()->checkRelease(std::get<kku::SystemEvent::Key>(input.event.data).view);
    }
    break;

    }
}

void ClassicGame::update(kku::UpdateData&& updatedata)
{
    // UNCOMMENT TO TEST AUTOPLAY
    /*auto note_it = _timeline->getActiveNote(updatedata.timestamp);

    if (!_timeline->isExpired(note_it) && updatedata.timestamp >= (*note_it)->getPerfectOffset())
    {
        auto note = (*note_it);
        note->input(kku::GameEvent{updatedata.timestamp, kku::SystemEvent{kku::SystemEvent::Type::None, kku::SystemEvent::Key{}}});
    }*/

    _timeline->update(updatedata.timestamp);
    _context->getGraphicsManager()->update(updatedata.timestamp);
}

void ClassicGame::display() const
{
    _context->getGraphicsManager()->display();
}
