#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 "game/classicarrownote.h"
#include "editorcontext.h"
#include "callbacks/callbacksimple.h"

ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
                             const std::shared_ptr<EditorContext>& context) :
    _timeline(timeline),
    _context(context),
    _selected_type(Type::UP),
    _current_time(0),
    _scroll_step(500000),
    _note_id(0)
{
    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)
    {
        ArrowElement element;

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

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

        element.type = Type::UP;

        bool hold = false;

        if (_note_id == 0)
        {
            element.keys = {kku::SystemEvent::Key::Code::S,
                            kku::SystemEvent::Key::Code::Down};

            element.type = Type::DOWN;
        }

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

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

            element.type = Type::RIGHT;

            counter = 13;
        }

        --counter;

        ClassicArrowNote::Init init
        {
            _note_id++,
            context,
            bpm_iterator,
            input_intervals,
            {element},
            hold
        };

        notes.insert(new ClassicArrowNote(std::move(init)));

        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 + 1));
}

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)
        {
            _context->getSelectionManager()->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)
        {
            _context->getSelectionManager()->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)
        {
            ArrowElement element;

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

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

            element.type = Type::UP;

            ClassicArrowNote::Init init
            {
                _note_id++,
                _context,
                _current_time,
                {},
                {element},
                false
            };

            _timeline->insertNote(new ClassicArrowNote(std::move(init)));
        }

        if (!_context->getSelectionManager()->isMultiselectionEnabled())
            _context->getSelectionManager()->discard();

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

        break;
    }
    }

    _context->inputUI(std::move(input));
}

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

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

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

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