#include "application/gamestate.h"
#include "widgets/button.h"
#include "widgets/group.h"

#include "core/game.h"

GameState::GameState(const std::shared_ptr<kku::CoreFactory>& core_factory, const std::shared_ptr<kku::Game>& game, Callbacks&& callbacks) :
    _core_factory(core_factory),
    _game(game),
    _onLeaveGameCallback(callbacks.onLeaveGame)
{
    _music = _core_factory->getMusic();
}

void GameState::input(const kku::SystemEvent& event)
{
    switch (event.type)
    {
    default:
        break;

    case kku::SystemEvent::Type::KeyRelease:
        if (std::get<kku::SystemEvent::Key>(event.data).view == kku::SystemEvent::Key::Code::Space)
            _music->isPlaying() ? _music->pause() : _music->play();
    }

    _game->input(kku::GameEvent{_music->fetchOffset(), event});
}

void GameState::update(const kku::microsec& dt)
{
    _game->update(kku::UpdateData{_music->fetchOffset(), dt});
}

void GameState::display() const
{
    _game->display();
}

void GameState::enter()
{
    _game->run();

    _music->open("sources/METEOR.flac");
    _music->setVolume(10);
    _music->play();
}

void GameState::leave()
{
    _onLeaveGameCallback();
}
