#include <GLFW/glfw3.h>
#include <imgui/imgui.h>
#include <app.h>
#include <window.h>
#include "levelEditor.h"
#include "editor.h"

namespace editor {

const char *buttonNames[] = {
    "Cross",
    "Circle",
    "Squad",
    "Triangle"
};

const char *buttonTypes[] = {
    "Simple",
    "Long",
    "Double"
};

void LevelEditor::UpdateNoteSelector()
{
    if (App::GetInput()->IsButtonJustPressed(0)) {
        vec2f worldCursor = root->camera->screenToWorld(App::GetInput()->cursor);

        for (auto i = notesOnScreen.rbegin(); i != notesOnScreen.rend(); i++) {

            game::noteInfo *note = (*i);

            if (fabs(note->position.x - worldCursor.x + 3) < 0.5 && fabs(note->position.y - worldCursor.y + 2) < 0.5) {
                activeNote = find(notes.begin(), notes.end(), note);
                break;
            }
        }
    }
}

void LevelEditor::UpdateNoteEditor()
{
    const Input *input = App::GetInput();

    if (input->IsButtonJustPressed(GLFW_KEY_W)) {
        (*activeNote)->position.y -= 0.5;
        (*activeNote)->position.y = clamp((*activeNote)->position.y, 0.0f, 12.0f);
    } else if (input->IsButtonJustPressed(GLFW_KEY_S)) {
        (*activeNote)->position.y += 0.5;
        (*activeNote)->position.y = clamp((*activeNote)->position.y, 0.0f, 12.0f);
    } else if (input->IsButtonJustPressed(GLFW_KEY_A)) {
        (*activeNote)->position.x -= 0.5;
        (*activeNote)->position.x = clamp((*activeNote)->position.x, 0.0f, 24.0f);
    }  else if (input->IsButtonJustPressed(GLFW_KEY_D)) {
        (*activeNote)->position.x += 0.5;
        (*activeNote)->position.x = clamp((*activeNote)->position.x, 0.0f, 24.0f);
    }

    if (input->IsButtonPressed(GLFW_KEY_E)) {
        (*activeNote)->angle = fmod((*activeNote)->angle + window::deltaTime, (float) M_PI * 2);
    } else if (input->IsButtonPressed(GLFW_KEY_Q)) {
        (*activeNote)->angle = fmod((*activeNote)->angle - window::deltaTime, (float) M_PI * 2);
    }

    ImGui::Text("Path:");
    ImGui::SliderAngle("##note-angle", &(*activeNote)->angle, 0, 360);

    if (ImGui::InputFloat("##note-angle2", &(*activeNote)->angle, 0.01, 0.1, 2)) {
        (*activeNote)->angle = clamp((*activeNote)->angle, 0.0f, (float) M_PI * 2);
    }

    if (ImGui::Button("Copy previous##angle-copy-prev")) {
        if (activeNote != notes.begin()) {
            (*activeNote)->angle = (*std::prev(activeNote))->angle;
        }
    }

    ImGui::SameLine();

    if (ImGui::Button("Copy next##angle-copy-next")) {
        if (activeNote != std::prev(notes.end())) {
            (*activeNote)->angle = (*std::next(activeNote))->angle;
        }
    }

    ImGui::Separator();

    ImGui::Text("Position on screen:");

    if (ImGui::InputFloat("X", &(*activeNote)->position.x, 0.5, 1, 1)) {
        (*activeNote)->position.x = clamp((*activeNote)->position.x, 0.0f, 24.0f);
    }

    if (ImGui::InputFloat("Y", &(*activeNote)->position.y, 0.5, 1, 1)) {
        (*activeNote)->position.y = clamp((*activeNote)->position.y, 0.0f, 12.0f);
    }

    if (ImGui::Button("Copy previous##position-copy-prev")) {
        if (activeNote != notes.begin()) {
            (*activeNote)->position = (*std::prev(activeNote))->position;
        }
    }

    ImGui::SameLine();

    if (ImGui::Button("Copy next##position-copy-next")) {
        if (activeNote != std::prev(notes.end())) {
            (*activeNote)->position = (*std::next(activeNote))->position;
        }
    }

    ImGui::Separator();
    ImGui::Text("Note:");

    ImGui::Combo("Button", &(*activeNote)->button, buttonNames, 4);

    if (ImGui::InputFloat("Time##time-input", &(*activeNote)->time, 0.01, 0.1, 2)) {
        (*activeNote)->time = clamp((*activeNote)->time, 0.5f, songLength);
    }

    ImGui::Combo("Type", &(*activeNote)->type, buttonTypes, 3);

    if ((*activeNote)->type == 1) {
        if (ImGui::InputFloat("Duration##time-input2", &(*activeNote)->duration, 0.01, 0.1, 2)) {
            (*activeNote)->duration = clamp((*activeNote)->duration, 0.1f, songLength - (*activeNote)->time);
        }
    }

    if (ImGui::Button("Copy previous##noty-copy-prev")) {
        if (activeNote != notes.begin()) {

            auto prevNote = (*std::prev(activeNote));

            (*activeNote)->type = prevNote->type;
            (*activeNote)->button = prevNote->button;
            (*activeNote)->duration = prevNote->duration;
        }
    }
    ImGui::SameLine();
    if (ImGui::Button("Copy next##noty-copy-next")) {
        if (activeNote != std::prev(notes.end())) {

            auto nextNote = (*std::next(activeNote));

            (*activeNote)->type = nextNote->type;
            (*activeNote)->button = nextNote->button;
            (*activeNote)->duration = nextNote->duration;
        }
    }

    if (ImGui::Button("Copy previous time##noty-copy-prev")) {
        if (activeNote != notes.begin()) {

            auto prevNote = (*std::prev(activeNote));
            (*activeNote)->time = prevNote->time;
        }
    }
    ImGui::SameLine();
    if (ImGui::Button("Copy next time##noty-copy-next")) {
        if (activeNote != std::prev(notes.end())) {

            auto nextNote = (*std::next(activeNote));
            (*activeNote)->time = nextNote->time;
        }
    }

    ImGui::Separator();

    ImGui::Text("Go to note:");

    if (ImGui::Button("Previous")) {
        if (activeNote != notes.begin()) {
            activeNote--;
        }
    }
    ImGui::SameLine();
    if (ImGui::Button("Next")) {
        if (activeNote != std::prev(notes.end())) {
            activeNote++;
        }
    }

    ImGui::Separator();

    if (ImGui::Button("Delete note")) {
        auto nextNote = notes.end();

        if (notes.size() > 1) {
            if (activeNote != notes.begin()) {
                nextNote = std::prev(activeNote);
            } else {
                nextNote = std::next(activeNote);
            }
        }

        delete *activeNote;
        notes.erase(activeNote);

        activeNote = nextNote;
    }
}

}
