#include "..\script_component.hpp"

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

#pragma hemtt ignore_variables ["_self"]
GVAR(VArsenalModel) = compileFinal createHashMapObject [[
    ["#type", "VArsenalModel"],
    ["defaults", {
        private _vArsenal = createHashMap;

        _vArsenal set ["backpacks", ["B_AssaultPack_rgr"]];
        _vArsenal set ["items", ["FirstAidKit", "G_Combat", "H_Cap_blk_ION", "H_HelmetB", "ItemCompass", "ItemGPS", "ItemMap", "ItemRadio", "ItemWatch", "U_IG_Guerrilla_6_1", "V_TacVest_oli"]];
        _vArsenal set ["magazines", ["16Rnd_9x21_Mag", "30Rnd_65x39_caseless_black_mag", "Chemlight_blue", "Chemlight_green", "Chemlight_red", "Chemlight_yellow", "HandGrenade", "SmokeShell", "SmokeShellBlue", "SmokeShellGreen", "SmokeShellOrange", "SmokeShellPurple", "SmokeShellRed", "SmokeShellYellow"]];
        _vArsenal set ["weapons", ["arifle_MX_F", "hgun_P07_F"]];

        _vArsenal
    }]
]];

GVAR(VArsenalRepository) = compileFinal createHashMapFromArray [
    ["#base", EGVAR(common,BaseStore)],
    ["#type", "VArsenalRepository"],
    ["#create", {
        GVAR(VArsenalRegistry) = createHashMap;
        ["INFO", "VArsenal Repository Initialized!"] call EFUNC(common,log);
    }],
    ["get", {
        params [["_uid", "", [""]]];

        private _cached = GVAR(VArsenalRegistry) getOrDefault [_uid, nil];
        if !(isNil { _cached }) exitWith { _cached };

        ["owned:locker:exists", [_uid]] call EFUNC(extension,extCall) params ["_result", "_isSuccess"];
        if !(_isSuccess) exitWith {
            ["ERROR", format ["Failed to check if virtual arsenal %1 exists!", _uid]] call EFUNC(common,log);
            createHashMap
        };

        private _finalVArsenal = createHashMap;

        if (_result == "true") then {
            _finalVArsenal = _self call ["fetch", ["owned:locker:fetch", _uid]];
            ["INFO", format ["Found virtual arsenal for %1", _uid]] call EFUNC(common,log);
        } else {
            _finalVArsenal = GVAR(VArsenalModel) call ["defaults", []];

            ["owned:locker:create", [_uid]] call EFUNC(extension,extCall) params ["_result", "_isSuccess"];
            if !(_isSuccess) exitWith {
                ["ERROR", format ["Failed to create virtual arsenal for %1!", _uid]] call EFUNC(common,log);
                createHashMap
            };

            ["INFO", format ["Created new virtual arsenal for %1", _uid]] call EFUNC(common,log);
        };

        GVAR(VArsenalRegistry) set [_uid, _finalVArsenal];
        _finalVArsenal
    }]
];

GVAR(VArsenalStore) = createHashMapObject [[
    ["#base", GVAR(VArsenalRepository)],
    ["#type", "VArsenalStore"],
    ["#create", {
        ["INFO", "VArsenal Store Initialized!"] call EFUNC(common,log);
    }],
    ["init", {
        params [["_uid", "", [""]]];

        private _vArsenal = _self call ["get", [_uid]];
        private _player = [_uid] call EFUNC(common,getPlayer);

        [CRPC(locker,responseInitVA), [_vArsenal], _player] call CFUNC(targetEvent);
    }]
]];

GVAR(VArsenalStore)
