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

ClassicGame::ClassicGame(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
                         const std::shared_ptr<ClassicGraphicsManager>& graphics_manager) :
    _timeline(timeline),
    _graphics_manager(graphics_manager),
    _hold_manager(std::make_unique<HoldManager>())
{}

ClassicGame::~ClassicGame()
{}

void ClassicGame::run()
{
    auto beatmap = classic::createBeatmap("aa", _hold_manager);
    _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:
    {
        _hold_manager->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);
    _graphics_manager->update(updatedata.timestamp);
}

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