#include "input.h"
#include "Logging.h"
#include "Profiler.h"

void Input::Init(GLFWwindow* window)
{
    s_Window = window;
    Logger::LogOk("Input Core");
}

void Input::Update()
{
    PROFILE_ENGINE_SCOPE("Input::Update");

    std::copy(std::begin(s_KeysCurrent), std::end(s_KeysCurrent), s_KeysPrevious);
    std::copy(std::begin(s_MouseCurrent), std::end(s_MouseCurrent), s_MousePrevious);

    for (int key = 0; key <= GLFW_KEY_LAST; ++key)
        s_KeysCurrent[key] = glfwGetKey(s_Window, key) == GLFW_PRESS;

    for (int button = 0; button <= GLFW_MOUSE_BUTTON_LAST; ++button)
        s_MouseCurrent[button] = glfwGetMouseButton(s_Window, button) == GLFW_PRESS;
}

bool Input::IsKeyDown(Keycode key)
{
    return s_KeysCurrent[static_cast<int>(key)];
}

bool Input::IsKeyPressed(Keycode key)
{
    int k = static_cast<int>(key);
    return s_KeysCurrent[k] && !s_KeysPrevious[k];
}

bool Input::IsKeyReleased(Keycode key)
{
    int k = static_cast<int>(key);
    return !s_KeysCurrent[k] && s_KeysPrevious[k];
}

bool Input::IsMouseButtonDown(int button)
{
    return s_MouseCurrent[button];
}

bool Input::IsMouseButtonPressed(int button)
{
    return s_MouseCurrent[button] && !s_MousePrevious[button];
}

bool Input::IsMouseButtonReleased(int button)
{
    return !s_MouseCurrent[button] && s_MousePrevious[button];
}

core::types::Vec2 Input::GetMousePosition()
{
    double x, y;
    glfwGetCursorPos(s_Window, &x, &y);
    return { static_cast<float>(x), static_cast<float>(y) };
}
