#include "script_component.hpp"

PREP_RECOMPILE_START;
#include "XEH_PREP.hpp"
PREP_RECOMPILE_END;

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

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

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

[QGVAR(requestGetBank), {
    params [["_uid", "", [""]], ["_sync", false, [false]]];

    if (_uid isEqualTo "") exitWith { diag_log "[FORGE:Server:Bank] Empty/Invalid UID!" };
    GVAR(BankStore) call ["get", [GVAR(BankRegistry), "bank:get", _uid, _sync]];
}] call CFUNC(addEventHandler);

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

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

    private _hashMap = GVAR(BankStore) call ["set", [GVAR(BankRegistry), "bank:update", _uid, _key, _value, _sync]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(bank,responseSyncBank), [_hashMap], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

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

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

    private _hashMap = GVAR(BankStore) call ["mset", [GVAR(BankRegistry), "bank:update", _uid, _fieldValuePairs, _sync]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(bank,responseSyncBank), [_hashMap], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

[QGVAR(requestSaveBank), {
    params [["_uid", "", [""]], ["_sync", false, [false]]];

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

    private _finalData = GVAR(BankStore) call ["save", [GVAR(BankRegistry), "bank:update", _uid, _sync]];
    private _player = [_uid] call EFUNC(common,getPlayer);

    [CRPC(bank,responseSyncBank), [_finalData], _player] call CFUNC(targetEvent);
}] call CFUNC(addEventHandler);

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

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

[QGVAR(requestDeposit), {
    params [["_uid", "", [""]], ["_amount", 0, [0]]];

    if (_uid isEqualTo "" || _amount isEqualTo 0) exitWith { diag_log "[FORGE:Server:Bank] Empty/Invalid UID or Amount!" };
    GVAR(BankStore) call ["deposit", [_uid, _amount]];
}] call CFUNC(addEventHandler);

[QGVAR(requestPayment), {
    params [["_uid", "", [""]], ["_amount", 0, [0]]];

    if (_uid isEqualTo "" || _amount isEqualTo 0) exitWith { diag_log "[FORGE:Server:Bank] Empty/Invalid UID or Amount!" };
    GVAR(BankStore) call ["payment", [_uid, _amount]];
}] call CFUNC(addEventHandler);

[QGVAR(requestTransfer), {
    params [["_uid", "", [""]], ["_target", "", [""]], ["_from", "", [""]], ["_amount", 0, [0]]];

    if (_uid isEqualTo "" || _target isEqualTo "" || _from isEqualTo "" || _amount isEqualTo 0) exitWith {
        diag_log "[FORGE:Server:Bank] Empty/Invalid UID, Target, From Account, or Amount!"
    };

    if (_uid isEqualTo _target) exitWith {
        diag_log format ["[FORGE:Server:Bank] SECURITY: Player %1 attempted self-transfer!", _uid];

        private _player = [_uid] call EFUNC(common,getPlayer);
        [CRPC(notifications,recieveNotification), ["error", "Bank", "Cannot transfer to yourself!"], _player] call CFUNC(targetEvent);
    };

    GVAR(BankStore) call ["transfer", [_uid, _target, _from, _amount]];
}] call CFUNC(addEventHandler);

[QGVAR(requestWithdraw), {
    params [["_uid", "", [""]], ["_amount", 0, [0]]];

    if (_uid isEqualTo "" || _amount isEqualTo 0) exitWith { diag_log "[FORGE:Server:Bank] Empty/Invalid UID or Amount!" };
    GVAR(BankStore) call ["withdraw", [_uid, _amount]];
}] call CFUNC(addEventHandler);
