#include "script_component.hpp"

[QGVAR(handleEvents), {
    params ["_control", "_isConfirmDialog", "_message"];

    diag_log text format ["[FORGE::Client::Garage::XEH_postInit] Received event: '%1'", _message];

    _message = fromJSON _message;
    private _event = _message get "event";
    private _data = _message get "data";
    
    private _vehicles = GETVAR(player,FORGE_Garage,[]); //TODO: Implement garage from server

    switch (_event) do {
        case "REQUEST::GARAGE::DATA": {
            private _garageData = createHashMap;
            private _vehicleList = [];

            {
                private _vehicle = _x;
                if (isNull _vehicle || { !alive _vehicle }) exitWith {};

                private _vehicleInfo = createHashMapFromArray [
                    ["classname", typeOf _vehicle],
                    ["damage", damage _vehicle],
                    ["fuel", fuel _vehicle],
                    ["hitpoints", getAllHitPointsDamage _vehicle]
                ];

                _vehicleList pushBack _vehicleInfo;
            } forEach _vehicles;

            _garageData set ["vehicles", _vehicleList];
            _control ctrlWebBrowserAction ["ExecJS", format ["handleGarageDataRequest(%1)", (toJSON _vehicleList)]];
        };
        case "STORE::VEHICLE": {
            // Logic to store vehicle in garage
            // This would typically involve saving the vehicle's state to a database or similar
        };
        case "RETRIEVE::VEHICLE": {
            // Logic to retrieve vehicle from garage
            // This would typically involve loading the vehicle's state from a database or similar
        };
        default {
            diag_log text format ["[FORGE::Client::Garage::XEH_postInit] Unknown event: '%1'", _event];
        };
    };
}] call CFUNC(addEventHandler);