#include "classiceditor.h"

#include "graphics/classicgraphicsmanager.h"
#include "editor/selectionmanager.h"

// Replace with interface by dependency injection
#include "graphics/animations/classicflyinganimationscenario.h"
#include "graphics/animations/classicdyinganimationscenario.h"
//

#include "callbacks/callbacksimple.h"

ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
                             const std::shared_ptr<ClassicGraphicsManager>& graphics_manager,
                             const std::shared_ptr<SelectionManager<ClassicNote>>& selection_manager) :
    _timeline(timeline),
    _graphics_manager(graphics_manager),
    _selection_manager(selection_manager),
    _selected_type(Type::UP),
    _current_time(0),
    _scroll_step(500000)
{
    kku::microsec starting_beat_offset = 402162;
    int amount_of_beats = 209;
    kku::microsec interval = 1412162;
    kku::microsec tempo_interval = interval / 4;
    kku::microsec note_input_offset = 412162 / 2;
    //microsec note_input_offset_fast = 412162 / 6;
    kku::microsec bpm_iterator = starting_beat_offset;
    kku::microsec bpm_end = starting_beat_offset + (interval * amount_of_beats);

    std::vector<kku::microsec> input_intervals = {note_input_offset / 3, note_input_offset / 3 * 2, note_input_offset};
    std::set<ClassicNote*, kku::NotePtrComparator> notes;
    input_intervals.shrink_to_fit();

    bpm_iterator += tempo_interval;

    float x = 90.;

    int counter = 3;

    while (bpm_iterator < bpm_end)
    {
        ArrowNoteInitializer init;
        ArrowElementInitializer element;
        init.initializer.intervals = input_intervals;
        init.initializer.perfect_offset = bpm_iterator;
        init.hold = false;

        element.element.position = kku::Point(x, 390.f);
        element.element.falling_curve_interpolation = {};

        element.keys = {kku::SystemEvent::Key::Code::W,
                        kku::SystemEvent::Key::Code::Up};

        element.element.type = Type::UP;

        if (counter == 0)
        {
            init.hold = true;

            element.keys = {kku::SystemEvent::Key::Code::D,
                            kku::SystemEvent::Key::Code::Right};

            element.element.type = Type::RIGHT;
        }

        --counter;

        init.elements = {element};

        notes.insert(new ClassicMockNote(std::move(init), _selection_manager));

        bpm_iterator += tempo_interval;
        x += 70;

        if (x >= 1200)
            x = 90.;
    }

    _timeline->setNotes(notes);
}

kku::microsec ClassicEditor::adjustOffset(kku::microsec offset) const noexcept
{
    const auto& section = getBPMSectionAt(offset);
    const kku::microsec actual_offset = offset - section.offset_start;

    return actual_offset + (actual_offset % section.interval);
}

void ClassicEditor::input(kku::GameEvent&& input)
{
    _current_time = input.timestamp;
    const auto& event = input.event;

    switch (input.event.type)
    {

    default:
        break;

    case kku::SystemEvent::Type::KeyPress:
    {
        const auto key_data = std::get<kku::SystemEvent::Key>(input.event.data);
        if (key_data.view == kku::SystemEvent::Key::Code::LControl)
        {
            _selection_manager->enableMultiselection(true);
        }
        break;
    }

    case kku::SystemEvent::Type::KeyRelease:
    {
        const auto key_data = std::get<kku::SystemEvent::Key>(input.event.data);
        if (key_data.view == kku::SystemEvent::Key::Code::LControl)
        {
            _selection_manager->enableMultiselection(false);
        }
        break;
    }

    case kku::SystemEvent::Type::MousePress:
    {
        const auto note = _timeline->getNoteBy(_current_time);
        if (_timeline->isExpired(note) && !_bpm_sections.empty() && _current_time >= (*_bpm_sections.begin()).offset_start)
        {
            ArrowNoteInitializer init;
            ArrowElementInitializer element;
            init.initializer.intervals = {};
            init.initializer.perfect_offset = input.timestamp;
            init.hold = false;

            element.element.position = std::get<kku::SystemEvent::Mouse>(event.data).position;
            element.element.falling_curve_interpolation = {};

            element.keys = {kku::SystemEvent::Key::Code::W,
                            kku::SystemEvent::Key::Code::Up};

            element.element.type = Type::UP;

            init.elements = { element };

            _timeline->insertNote(new ClassicMockNote(std::move(init), _selection_manager));
        }

        if (!_selection_manager->isMultiselectionEnabled())
            _selection_manager->discard();

        _graphics_manager->input(std::move(input));

        break;
    }

    }
}

void ClassicEditor::update(kku::UpdateData&& updatedata)
{
    _timeline->update(updatedata.timestamp);
    _graphics_manager->update(updatedata.timestamp);
}

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

void ClassicEditor::recalculate(const kku::microsec& timestamp)
{
    _timeline->recalculate(timestamp);
}

void ClassicEditor::selectNoteType(Type type) noexcept
{
    _selected_type = type;
}
