// SPDX-FileCopyrightText: Copyright 2024-2026 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

// Generated By moduleGenerator
#include "SDL3/SDL_mouse.h"
#include "common/logging/log.h"
#include "core/emulator_settings.h"
#include "core/libraries/error_codes.h"
#include "core/libraries/libs.h"
#include "core/user_settings.h"
#include "mouse.h"
#include "mouse_error.h"
#include "sdl_window.h"

extern Frontend::WindowSDL* g_window;

namespace Libraries::Mouse {

RingBufferQueue<OrbisMouseData> mouse_states[2]{{64}, {64}};
s32 mouse_handles[2]{-1, -1};
s32 mouse_sdl_handles[2]{-1, -1};
bool g_lib_init = false, g_is_merged_mode = false;

int PS4_SYSV_ABI sceMouseClose(s32 handle) {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseConnectPort() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseDebugGetDeviceId() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseDeviceOpen() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseDisconnectDevice() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseDisconnectPort() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseGetDeviceInfo() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseInit() {
    if (EmulatorSettings.IsMiceUsedAsMice()) {
        SDL_WarpMouseInWindow(g_window->GetSDLWindow(), 1, 1);
        SDL_SetWindowRelativeMouseMode(g_window->GetSDLWindow(), true);
    }
    int micecount = 0;
    auto micelist = SDL_GetMice(&micecount);
    LOG_INFO(Input, "{} mice are currently connected", micecount);
    for (int i = 0; i < micecount; i++) {
        LOG_INFO(Lib_Mouse, "{}: {} id: {}", i, SDL_GetMouseNameForID(micelist[i]), micelist[i]);
    }
    SDL_free(micelist);
    g_lib_init = true;
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseMbusInit() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseOpen(Libraries::UserService::OrbisUserServiceUserId userId, s32 type,
                              s32 index, OrbisMouseOpenParam* pParam) {
    LOG_WARNING(Lib_Mouse, "(DUMMY) called, uid: {}, type: {}, index: {}", userId, type, index);
    auto u = UserManagement.GetUserByID(userId);
    if (!u || type != 0 || index < 0 || index > 1) {
        LOG_ERROR(Lib_Mouse, "invalid argument");
        return ORBIS_MOUSE_ERROR_INVALID_ARG;
    }
    MouseOpenBehaviour mouse_mode = pParam ? pParam->flag : MouseOpenBehaviour::Normal;
    bool merged_mode = True(mouse_mode & MouseOpenBehaviour::Merged);
    if (merged_mode && index != 0) {
        LOG_ERROR(Lib_Mouse, "Only one mouse can be opened in merged mode!");
        return ORBIS_MOUSE_ERROR_ALREADY_OPENED;
    }
    g_is_merged_mode = merged_mode;
    if (mouse_handles[index] != -1) {
        LOG_ERROR(Lib_Mouse, "already opened");
        return ORBIS_MOUSE_ERROR_ALREADY_OPENED;
    }
    mouse_handles[index] = index;
    LOG_INFO(Lib_Mouse, "out handle: {}", mouse_handles[index]);
    return mouse_handles[index];
}

int PS4_SYSV_ABI sceMouseRead(s32 handle, OrbisMouseData* pData, s32 num) {
    LOG_DEBUG(Lib_Mouse, "(DUMMY) called, h: {}, n: {}", handle, num);
    if (!pData || num > 64) {
        return ORBIS_MOUSE_ERROR_INVALID_ARG;
    }
    if (mouse_handles[0] != handle && mouse_handles[1] != handle) {
        return ORBIS_MOUSE_ERROR_INVALID_HANDLE;
    }
    u64 m = mouse_handles[0] == handle ? 0 : 1;
    int i = 0;
    for (; i < num; i++) {
        std::optional<OrbisMouseData> st = mouse_states[m].Pop();
        if (!st) {
            break;
        }
        pData[i] = *st;
        pData[i].connected = mouse_sdl_handles[m] != -1;
    }
    return i;
}

int PS4_SYSV_ABI sceMouseSetHandType() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseSetPointerSpeed() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseSetProcessPrivilege() {
    LOG_ERROR(Lib_Mouse, "(STUBBED) called");
    return ORBIS_OK;
}

void RegisterLib(Core::Loader::SymbolsResolver* sym) {
    LIB_FUNCTION("cAnT0Rw-IwU", "libSceMouse", 1, "libSceMouse", sceMouseClose);
    LIB_FUNCTION("Ymyy1HSSJLQ", "libSceMouse", 1, "libSceMouse", sceMouseConnectPort);
    LIB_FUNCTION("BRXOoXQtb+k", "libSceMouse", 1, "libSceMouse", sceMouseDebugGetDeviceId);
    LIB_FUNCTION("WiGKINCZWkc", "libSceMouse", 1, "libSceMouse", sceMouseDeviceOpen);
    LIB_FUNCTION("eDQTFHbgeTU", "libSceMouse", 1, "libSceMouse", sceMouseDisconnectDevice);
    LIB_FUNCTION("jJP1vYMEPd4", "libSceMouse", 1, "libSceMouse", sceMouseDisconnectPort);
    LIB_FUNCTION("QA9Qupz3Zjw", "libSceMouse", 1, "libSceMouse", sceMouseGetDeviceInfo);
    LIB_FUNCTION("Qs0wWulgl7U", "libSceMouse", 1, "libSceMouse", sceMouseInit);
    LIB_FUNCTION("1FeceR5YhAo", "libSceMouse", 1, "libSceMouse", sceMouseMbusInit);
    LIB_FUNCTION("RaqxZIf6DvE", "libSceMouse", 1, "libSceMouse", sceMouseOpen);
    LIB_FUNCTION("x8qnXqh-tiM", "libSceMouse", 1, "libSceMouse", sceMouseRead);
    LIB_FUNCTION("crkFfp-cmFo", "libSceMouse", 1, "libSceMouse", sceMouseSetHandType);
    LIB_FUNCTION("ghLUU2Z5Lcg", "libSceMouse", 1, "libSceMouse", sceMouseSetPointerSpeed);
    LIB_FUNCTION("6aANndpS0Wo", "libSceMouse", 1, "libSceMouse", sceMouseSetProcessPrivilege);
};

} // namespace Libraries::Mouse