#include "../engine/Shared.h"
#include "Global.h"
#include "Input.h"
#include "imgui.h"

namespace Game
{
    bool IsKeyboardAllowed()
    {
        auto& IO = ImGui::GetIO();
        return !IO.WantCaptureKeyboard || GetFlag(IO.ConfigFlags, ImGuiConfigFlags_NoKeyboard);
    }

    bool IsMouseAllowed()
    {
        auto& IO = ImGui::GetIO();
        return !IO.WantCaptureMouse || GetFlag(IO.ConfigFlags, ImGuiConfigFlags_NoMouse);
    }

    bool GetKey(ScanCode key)
    {
        if (!IsKeyboardAllowed()) return false;
        return GetShared().Window.HeldScanCodes[(int32_t)key];
    }
    bool GetKeyPressedNow(ScanCode key)
    {
        if (!IsKeyboardAllowed()) return false;
        auto& win = GetShared().Window;
        return win.HeldScanCodes[(int32_t)key] && !win.LastHeldScanCodes[(int32_t)key];
    }
    bool GetKeyReleasedNow(ScanCode key)
    {
        if (!IsKeyboardAllowed()) return false;
        auto& win = GetShared().Window;
        return !win.HeldScanCodes[(int32_t)key] && win.LastHeldScanCodes[(int32_t)key];
    }
    bool GetMouseButton(MouseButton button)
    {
        if (!IsMouseAllowed()) return false;
        return GetShared().Window.HeldMouseButtons[(int32_t)button];
    }
    bool GetMouseButtonPressedNow(MouseButton button)
    {
        if (!IsMouseAllowed()) return false;
        auto& win = GetShared().Window;
        return win.HeldMouseButtons[(int32_t)button] && !win.LastHeldMouseButtons[(int32_t)button];
    }
    bool GetMouseButtonReleasedNow(MouseButton button)
    {
        if (!IsMouseAllowed()) return false;
        auto& win = GetShared().Window;
        return !win.HeldMouseButtons[(int32_t)button] && win.LastHeldMouseButtons[(int32_t)button];
    }
    Vec2 GetMouseMovement()
    {
        if (!IsMouseAllowed()) return {};
        return {GetShared().Window.MouseDeltaX, GetShared().Window.MouseDeltaY};
    }
} // namespace Game
