#include "..\script_component.hpp"

/*
 * Function: forge_client_garage_fnc_spawnVehicle
 * Author: IDSolutions
 *
 * Description:
 * Spawns a vehicle from the garage
 *
 * Arguments:
 * None
 *
 * Return Value:
 * None
 */

disableSerialization;
private _display = findDisplay IDD_GARAGEDIALOG;
private _garageList = _display displayCtrl IDC_GARAGEVEHICLELIST;
private _vehicleList  = _display displayCtrl IDC_VEHICLELIST;
private _selectedVehicle = lbCurSel _garageList;
private _selectedVehicleData = _garageList lbData _selectedVehicle;
private _data = call compile _selectedVehicleData;

ctrlEnable [IDC_SPAWNBUTTON, false];

if ((isNil { _data })) exitWith { ctrlEnable [IDC_SPAWNBUTTON, true]; };

private _vehType = _data select 0;
private _className = _data select 1;
// private _vehData = _data select 2;
private _locations = (missionConfigFile >> "CfgGarages" >> "locations") call BFUNC(getCfgData);

{
    if ((_x select 0) == _vehType) then {
        private _vehicle = createVehicle [_className, (_x select 1)];
        // private _fuel = _vehData select 0;
        // private _damage = _vehData select 1;
        // private _hitPointsData = _vehData select 2;

        // _hitPointsData = call compile _hitPointsData;

        // if (count _hitPointsData > 0) then {
        //     private _hitPoints = _hitPointsData select 0;
        //     private _damages = _hitPointsData select 2;

        //     {
        //         _vehicle setHitPointDamage [_x, _damages select _forEachIndex];
        //     } forEach _hitPoints;
        // };
        
        
        // {
        //     _vehicle setHitPointDamage [_x, _damages select _forEachIndex];
        // } forEach _hitPoints;


        // {
        //     _vehicle setHitPointDamage [_x, _damages select _forEachIndex];
        // } forEach _hitPoints;

        // _vehicle setFuel _fuel;
        // _vehicle setDamage _damage;
        // _vehicle setVariable ["FORGE_VehicleType", _vehType, true];
        SETPVAR(_vehicle,FORGE_VehicleType,_vehType);
        _vehicle setDir (_x select 2);
    };
} forEach _locations;

lbClear _garageList;
lbClear _vehicleList;
_garageList lbSetCurSel -1;
_vehicleList lbSetCurSel -1;

private _garage = GETVAR(player,FORGE_Garage,[]);
private _index = _garage findIf { _x isEqualTo _data };

if (_index != -1) then {
    _garage deleteAt _index;
    SETPVAR(player,FORGE_Garage,_garage);
};

[] call FUNC(fetchGarage);
[] call FUNC(fetchNearby);

ctrlEnable [IDC_SPAWNBUTTON, true];
playSound "FD_Finish_F";