#include "editorcontext.h"
#include "game/classicarrownote.h"
#include "graphics/animations/classicanimationscenario.h"
#include "graphics/classicgraphicsmanager.h"
#include "graphics/classicnotegraphics.h"

EditorContext::EditorContext(const std::shared_ptr<SelectionManager<ClassicNote>>& selection_manager,
                             std::vector<std::shared_ptr<ClassicGraphicsManager>>&& graphics_managers) :
    _selection_manager(selection_manager),
    _graphics_managers(std::move(graphics_managers))
{}

void EditorContext::input(ClassicArrowNote *note, kku::GameEvent&& input) const
{
    (void)note;
    switch (input.event.type)
    {

    default:
        break;

    /*case kku::SystemEvent::Type::MousePress:
    {
        bool selection_changed = false;
        std::size_t amount_selected = 0;

        const auto position = std::get<kku::SystemEvent::Mouse>(input.event.data).position;
        for (auto& element : note->getElements())
        {
            if (element.sprite->getRectangle()->contains(position))
            {
                element.selected = !element.selected;
                selection_changed = true;
                if (element.selected)
                    ++amount_selected;
            }
        }

        if (selection_changed)
        {
            if (amount_selected > 0)
                _selection_manager->fetch(note);
            else
                _selection_manager->remove(note);
        }

        break;
    }*/

    }
}

void EditorContext::update(ClassicArrowNote *note, const kku::microsec& music_offset) const
{
    auto& elements = note->getElements();

    switch (note->getState())
    {
    default: return;
        break;

    case ClassicArrowNote::State::INITIAL:
        note->setState(ClassicArrowNote::State::FLYING);
        for (auto& manager : _graphics_managers)
            manager->update(music_offset, note);
        break;

    case ClassicArrowNote::State::FLYING:
        if (!note->isActive(music_offset) && music_offset > note->getPerfectOffset())
        {
            note->setState(ClassicArrowNote::State::DYING);
            for (auto& element : elements)
                element.animations[note->getState()]->launch(element.sprite, music_offset, note->getPerfectOffset());
        }
        break;

    case ClassicArrowNote::State::DYING:
        if (elements[0].animations[note->getState()]->isDone())
        {
            note->setState(ClassicArrowNote::State::DEAD);
            for (auto& manager : _graphics_managers)
                manager->update(music_offset, note);
        }
        break;
    }

    for (auto& element : elements)
        if (element.animations[note->getState()])
            element.animations[note->getState()]->update(music_offset);
}

std::shared_ptr<SelectionManager<ClassicNote>> EditorContext::getSelectionManager() const
{
    return _selection_manager;
}

void EditorContext::updateGraphics(const kku::microsec& music_offset)
{
    for (auto& manager : _graphics_managers)
        manager->update(music_offset);
}

void EditorContext::displayGraphics() const
{
    for (const auto& manager : _graphics_managers)
        manager->display();
}
