#include "gamepad.hpp"
#include <GLFW/glfw3.h>
#include <cstring>

namespace control
{

Gamepad::Gamepad(int number)
{
    this->id = number;
    std::memset(this->buttons, 0, 15);
    std::memset(this->axesValues, 0, 6 * sizeof(float));
}

Gamepad::~Gamepad()
{

}

void Gamepad::Update() noexcept
{
    GLFWgamepadstate state;

    glfwGetGamepadState(this->id, &state);
    std::memcpy(this->oldButtons, this->buttons, 15);
    std::memcpy(this->buttons, state.buttons, 15);
    std::memcpy(this->axesValues, state.axes, 6 * sizeof(float));
}

bool Gamepad::IsButtonPressed(uint32_t button) const noexcept
{
    if (button >= 15) {
        return false;
    }

    return this->buttons[button];
}

bool Gamepad::IsButtonJustPressed(uint32_t button) const noexcept
{
    if (button >= 15) {
        return false;
    }

    return this->buttons[button] && !this->oldButtons[button];
}

float Gamepad::GetAxis(uint32_t axis) const noexcept
{
    if (axis >= 6) {
        return false;
    }

    return this->axesValues[axis];
}


}
