#include "..\script_component.hpp"

/*
 * File: fnc_initVAClass.sqf
 * Author: IDSolutions
 * Date: 2025-12-16
 * Last Update: 2026-01-30
 * Public: No
 *
 * Description:
 * Initializes the Virtual Arsenal class for managing player arsenal unlocks.
 * Provides methods for syncing, saving, and applying virtual items to BIS Arsenal.
 *
 * Arguments:
 * None
 *
 * Return Value:
 * vArsenal class object [HASHMAP OBJECT]
 *
 * Example:
 * call forge_client_locker_fnc_initVAClass;
 */

#pragma hemtt ignore_variables ["_self"]
GVAR(VArsenalClass) = createHashMapObject [[
    ["#type", "IVArsenalClass"],
    ["#create", {
        _self set ["uid", (getPlayerUID player)];
        _self set ["vArsenal", createHashMap];
        _self set ["isLoaded", false];
        _self set ["lastSave", time];
    }],
    ["init", {
        private _uid = _self get "uid";

        [SRPC(locker,requestInitVA), [_uid]] call CFUNC(serverEvent);
        FORGE_Locker_Box = "ReammoBox_F" createVehicleLocal [0, 0, -999];

        systemChat format ["VArsenal loaded for %1", (name player)];
        diag_log "[FORGE:Client:VArsenal] VArsenal Class Initialized!";
    }],
    ["save", {
        private _uid = _self get "uid";
        [SRPC(locker,requestSaveVA), [_uid]] call CFUNC(serverEvent);

        _self set ["lastSave", time];
    }],
    ["sync", {
        params [["_data", createHashMap, [createHashMap]], ["_jip", false, [false]]];

        private _vArsenal = _self get "vArsenal";
        private _isLoaded = _self get "isLoaded";

        if (_data isEqualTo createHashMap) exitWith { diag_log "[FORGE:Client:VArsenal] Empty data received for sync, skipping."; };

        {
            _vArsenal set [_x, _y];

            if (_jip) then {
                switch (_x) do {
                    case "items": { _self call ["applyItems", []]; };
                    case "weapons": { _self call ["applyWeapons", []]; };
                    case "magazines": { _self call ["applyMagazines", []]; };
                    case "backpacks": { _self call ["applyBackpacks", []]; };
                    default {};
                };
            };
        } forEach _data;

        _self set ["vArsenal", _vArsenal];

        if !(_isLoaded) then { _self set ["isLoaded", true]; };
        diag_log "[FORGE:Client:VArsenal] Sync completed";
    }],
    ["get", {
        params [["_key", "", [""]], ["_default", nil, [[], "", 0, false, createHashMap]]];

        private _vArsenal = _self get "vArsenal";
        _vArsenal getOrDefault [_key, _default];
    }],
    ["applyItems", {
        private _items = _self call ["get", ["items", []]];
        [FORGE_Armory_Box, _items, false, true, 1, 0] call BFUNC(addVirtualItemCargo);
    }],
    ["applyWeapons", {
        private _weapons = _self call ["get", ["weapons", []]];
        [FORGE_Armory_Box, _weapons, false, true, 1, 1] call BFUNC(addVirtualItemCargo);
    }],
    ["applyMagazines", {
        private _magazines = _self call ["get", ["magazines", []]];
        [FORGE_Armory_Box, _magazines, false, true, 1, 2] call BFUNC(addVirtualItemCargo);
    }],
    ["applyBackpacks", {
        private _backpacks = _self call ["get", ["backpacks", []]];
        [FORGE_Armory_Box, _backpacks, false, true, 1, 3] call BFUNC(addVirtualItemCargo);
    }]
]];

GVAR(VArsenalClass)
