#include <GLFW/glfw3.h>
#include <cstring>
#include <app.h>
#include "buttonManager.h"

int gamepadButtons[2][4] = {
    {
        GLFW_GAMEPAD_BUTTON_CROSS,
        GLFW_GAMEPAD_BUTTON_CIRCLE,
        GLFW_GAMEPAD_BUTTON_SQUARE,
        GLFW_GAMEPAD_BUTTON_TRIANGLE,
    },
    {
        GLFW_GAMEPAD_BUTTON_DPAD_DOWN,
        GLFW_GAMEPAD_BUTTON_DPAD_RIGHT,
        GLFW_GAMEPAD_BUTTON_DPAD_LEFT,
        GLFW_GAMEPAD_BUTTON_DPAD_UP,
    }
};

int keyboardButtons[2][4] = {
    {
        GLFW_KEY_DOWN,
        GLFW_KEY_RIGHT,
        GLFW_KEY_LEFT,
        GLFW_KEY_UP,
    },
    {
        GLFW_KEY_S,
        GLFW_KEY_D,
        GLFW_KEY_A,
        GLFW_KEY_W,
    }
};

namespace game {

ButtonManager::ButtonManager()
{
    memset(&currentState, 0, sizeof(buttonState));
}

void ButtonManager::Update()
{
    if (App::GetInput()->IsGamepadPresent(0)) {
        this->UpdateGamepad();
    }

    this->UpdateKeyboard();
}

void ButtonManager::UpdateGamepad()
{
    const control::Gamepad *gamepad = App::GetInput()->GetGamepad(0);

    if (currentState.level) {
        for (int i = 0; i < 2; i++) {
            if (gamepad->IsButtonJustReleased(gamepadButtons[i][currentState.button])) {
                currentState.level--;
            }
        }

        if (currentState.level < 0) {
            currentState.level = 0;
        }
    }

    for (int i = 0; i < 4; i++) {

        if (!currentState.level) {
            for (int j = 0; j < 2; j++) {
                if (gamepad->IsButtonJustPressed(gamepadButtons[j][i])) {
                    currentState.button = i;
                    currentState.level++;
                }
            }
        } else if (currentState.level == 1) {
            for (int j = 0; j < 2; j++) {
                if (gamepad->IsButtonJustPressed(gamepadButtons[j][i])) {
                    if (i == currentState.button) {
                        currentState.level++;
                    } else {
                        currentState.button = i;
                        currentState.level = 1;
                    }
                }
            }
        }
    }
}

void ButtonManager::UpdateKeyboard()
{
    const Input *gamepad = App::GetInput();

    if (currentState.level) {
        for (int i = 0; i < 2; i++) {
            if (gamepad->IsButtonJustReleased(keyboardButtons[i][currentState.button])) {
                currentState.level--;
            }
        }

        if (currentState.level < 0) {
            currentState.level = 0;
        }
    }

    for (int i = 0; i < 4; i++) {

        if (!currentState.level) {
            for (int j = 0; j < 2; j++) {
                if (gamepad->IsButtonJustPressed(keyboardButtons[j][i])) {
                    currentState.button = i;
                    currentState.level++;
                }
            }
        } else if (currentState.level == 1) {
            for (int j = 0; j < 2; j++) {
                if (gamepad->IsButtonJustPressed(keyboardButtons[j][i])) {
                    if (i == currentState.button) {
                        currentState.level++;
                    } else {
                        currentState.button = i;
                        currentState.level = 1;
                    }
                }
            }
        }
    }
}

}
