#include "..\script_component.hpp"

/*
 * File: fnc_initGarageClass.sqf
 * Author: IDSolutions
 * Date: 2025-12-17
 * Last Update: 2026-01-30
 * Public: No
 *
 * Description:
 * Initializes the Garage class for managing player vehicles.
 * Provides methods for syncing, saving, and applying vehicles to the player's garage.
 *
 * Arguments:
 * None
 *
 * Return Value:
 * Garage class object [HASHMAP OBJECT]
 *
 * Example:
 * call forge_client_garage_fnc_initGarageClass
 */

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

        [SRPC(garage,requestInitGarage), [_uid, _garage]] call CFUNC(serverEvent);

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

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

        private _garage = _self get "garage";
        private _isLoaded = _self get "isLoaded";

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

        {
            _garage set [_x, _y];
        } forEach _data;

        _self set ["garage", _garage];

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

        private _garage = _self get "garage";
        _garage getOrDefault [_key, _default];
    }]
]];

GVAR(GarageClass)
