#include "script_component.hpp"

PREP_RECOMPILE_START;
#include "XEH_PREP.hpp"
PREP_RECOMPILE_END;

// private _category = [QUOTE(MOD_NAME), LLSTRING(displayName)];

[QGVAR(requestInitLocker), {
    params [["_uid", "", [""]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:Locker] Empty/Invalid UID!" };
    GVAR(LockerStore) call ["init", [_uid]];
}] call CFUNC(addEventHandler);

[QGVAR(requestGetLocker), {
    params [["_uid", "", [""]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:Locker] Empty/Invalid UID!" };
    GVAR(LockerStore) call ["get", [GVAR(LockerRegistry), "locker:get", _uid]];
}] call CFUNC(addEventHandler);

[QGVAR(requestSetLocker), {
    params [["_uid", "", [""]], ["_key", "", [""]], ["_value", nil, [[], "", 0, false, createHashMap]], ["_sync", false, [false]]];

    if (_uid isEqualTo "" || _key isEqualTo "") exitWith { diag_log "[FORGE:Server:Locker] Empty/Invalid UID or Key!" };

    private _hashMap = GVAR(LockerStore) call ["set", [GVAR(LockerRegistry), "locker:update", _uid, _key, _value, _sync]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(locker,responseSyncLocker), [_hashMap], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

[QGVAR(requestMSetLocker), {
    params [["_uid", "", [""]], ["_fieldValuePairs", createHashMap, [createHashMap]], ["_sync", false, [false]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:Locker] Empty/Invalid UID!" };
    if ((_fieldValuePairs isEqualTo createHashMap) || !(_fieldValuePairs isEqualType createHashMap)) exitWith { diag_log "[FORGE:Server:Locker] Empty/Invalid field pairs!" };

    private _hashMap = GVAR(LockerStore) call ["mset", [GVAR(LockerRegistry), "locker:update", _uid, _fieldValuePairs, _sync]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(locker,responseSyncLocker), [_hashMap], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

[QGVAR(requestSaveLocker), {
    params [["_uid", "", [""]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:Locker] Empty/Invalid UID!" };

    private _finalData = GVAR(LockerStore) call ["save", [GVAR(LockerRegistry), "locker:update", _uid]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(locker,responseSyncLocker), [_finalData], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

[QGVAR(requestOverrideLocker), {
    params [["_uid", "", [""]], ["_data", createHashMap, [createHashMap]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:Locker] Empty/Invalid UID!" };
    GVAR(LockerRegistry) set [_uid, _data];

    private _player = [_uid] call EFUNC(common,getPlayer);
    [CRPC(locker,responseSyncLocker), [_data], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

[QGVAR(requestRemoveLocker), {
    params [["_uid", "", [""]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:Locker] Empty/Invalid UID!" };
    GVAR(LockerStore) call ["remove", [_uid]];
}] call CFUNC(addEventHandler);

[QGVAR(requestInitVA), {
    params [["_uid", "", [""]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:VArsenal] Empty/Invalid UID!" };
    GVAR(VArsenalStore) call ["init", [_uid]];
}] call CFUNC(addEventHandler);

[QGVAR(requestGetVA), {
    params [["_uid", "", [""]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:VArsenal] Empty/Invalid UID!" };
    GVAR(VArsenalStore) call ["get", [GVAR(VArsenalRegistry), "owned:locker:fetch", _uid]];
}] call CFUNC(addEventHandler);

[QGVAR(requestSetVA), {
    params [["_uid", "", [""]], ["_key", "", [""]], ["_value", nil, [[], "", 0, false, createHashMap]], ["_sync", false, [false]]];

    if (_uid isEqualTo "" || _key isEqualTo "") exitWith { diag_log "[FORGE:Server:VArsenal] Empty/Invalid UID or Key!" };

    private _hashMap = GVAR(VArsenalStore) call ["set", [GVAR(VArsenalRegistry), "owned:locker:update", _uid, _key, _value, _sync]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(locker,responseSyncVArsenal), [_hashMap], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

[QGVAR(requestMSetVA), {
    params [["_uid", "", [""]], ["_fieldValuePairs", createHashMap, [createHashMap]], ["_sync", false, [false]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:VArsenal] Empty/Invalid UID!" };
    if ((_fieldValuePairs isEqualTo createHashMap) || !(_fieldValuePairs isEqualType createHashMap)) exitWith { diag_log "[FORGE:Server:VArsenal] Empty/Invalid field pairs!" };

    private _hashMap = GVAR(VArsenalStore) call ["mset", [GVAR(VArsenalRegistry), "owned:locker:update", _uid, _fieldValuePairs, _sync]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(locker,responseSyncVArsenal), [_hashMap], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

[QGVAR(requestSaveVA), {
    params [["_uid", "", [""]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:VArsenal] Empty/Invalid UID!" };

    private _finalData = GVAR(VArsenalStore) call ["save", [GVAR(VArsenalRegistry), "owned:locker:update", _uid]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(locker,responseSyncVArsenal), [_finalData], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

[QGVAR(requestRemoveVA), {
    params [["_uid", "", [""]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:VArsenal] Empty/Invalid UID!" };
    GVAR(VArsenalStore) call ["remove", [GVAR(VArsenalRegistry), _uid]];
}] call CFUNC(addEventHandler);
