#include <app.h>
#include <GLFW/glfw3.h>
#include "noteRecorder.h"

const int buttons[4] = {
    GLFW_KEY_DOWN,
    GLFW_KEY_RIGHT,
    GLFW_KEY_LEFT,
    GLFW_KEY_UP
};

const int gamepadButtons[4] = {
    GLFW_GAMEPAD_BUTTON_CROSS,
    GLFW_GAMEPAD_BUTTON_CIRCLE,
    GLFW_GAMEPAD_BUTTON_SQUARE,
    GLFW_GAMEPAD_BUTTON_TRIANGLE
};

namespace editor {

NoteRecorder::NoteRecorder()
{
    Reset();
}

NoteRecorder::~NoteRecorder()
{
    for (auto i: recordedNotes) {
        delete i;
    }
}

void NoteRecorder::Start(float time)
{
    Reset();
    songStart = time;
    startTime = glfwGetTime();
    recording = true;
}

void NoteRecorder::Stop()
{
    recording = false;
}

void NoteRecorder::Reset()
{
    for (auto i: recordedNotes) {
        delete i;
    }

    recordedNotes.clear();

    recording = false;
    currentButton = -1;
    modifier = 0;
}

void NoteRecorder::SetPosition(float time)
{
    songTime = time;
}

float NoteRecorder::GetPosition()
{
    /*float time = songStart + glfwGetTime() - startTime;*/
    //return time - fmod(time, 0.1);

    return songTime;
}

void NoteRecorder::Update()
{
    if (!recording) {
        return;
    }

    const Input *input = App::GetInput();

    int pressedButtons[4] = {0,0,0,0};
    int releasedButtons[4] = {0,0,0,0};
    int pressedModidifers[2] = {0,0};

    if (input->IsGamepadPresent(0)) {
        for (int i = 0; i < 4; i++) {
            if (input->GetGamepad(0)->IsButtonJustPressed(gamepadButtons[i])) {
                pressedButtons[i] = 1;
            } else if (input->GetGamepad(0)->IsButtonJustReleased(gamepadButtons[i])) {
                releasedButtons[i] = 1;
            }
        }

        if (input->GetGamepad(0)->GetAxis(GLFW_GAMEPAD_AXIS_LEFT_TRIGGER) > 0.5) {
            pressedModidifers[0] = 1;
        } else if (input->GetGamepad(0)->GetAxis(GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER) > 0.5) {
            pressedModidifers[1] = 1;
        }
    }

    for (int i = 0; i < 4; i++) {
        if (input->IsButtonJustPressed(buttons[i])) {
            pressedButtons[i] = 1;
        } else if (input->IsButtonJustReleased(buttons[i])) {
            releasedButtons[i] = 1;
        }
    }

    if (input->IsButtonPressed(GLFW_KEY_X)) {
        pressedModidifers[0] = 1;
    } else if (input->IsButtonPressed(GLFW_KEY_Z)) {
        pressedModidifers[1] = 1;
    }



    if (currentButton == -1) {

        if (pressedModidifers[0]) {
            modifier = 1;
        } else if (pressedModidifers[1]) {
            modifier = 2;
        } else {
            modifier = 0;
        }

        for (int i = 0; i < 4; i++) {
            if (pressedButtons[i]) {
                if (modifier == 1) {
                    currentButton = i;
                    buttonTime = glfwGetTime();
                }

                float timeBorder = 0;
                float noteTime = this->GetPosition();

                if (!recordedNotes.empty()) {
                    timeBorder = recordedNotes.back()->getEndTime();
                }

                if (noteTime == timeBorder) {
                    break;
                }

                game::noteInfo *note = new game::noteInfo;

                note->button = i;
                note->time = noteTime;
                note->type = modifier;
                note->duration = 0;

                note->position.x = fmod(ceil(noteTime * 5), 24);
                note->position.y = int(ceil(noteTime * 5)) % 2 ? 10 : 9;
                note->angle = M_PI / 2;

                recordedNotes.push_back(note);
            }
        }

    } else if (currentButton != -1) {
        if (releasedButtons[currentButton]) {
            currentButton = -1;

            if (!recordedNotes.empty()) {
                recordedNotes.back()->duration = glfwGetTime() - buttonTime;
                recordedNotes.back()->duration -= fmod(recordedNotes.back()->duration, 0.1);

                if (recordedNotes.back()->duration < 0.1) {
                    recordedNotes.back()->duration = 0.1;
                }
            }
        }
    }
}

}
