#include "..\script_component.hpp"

/*
 * Function: forge_client_medical_fnc_moveInventory
 * Author: IDSolutions
 *
 * [Description]
 * Moves the unit's inventory to the body bag.
 *
 * Arguments:
 * 0: Unit <OBJECT>
 * 1: Body Bag <OBJECT>
 *
 * Return Value:
 * None
 *
 * Example:
 * [player, bodyBag] call forge_client_medical_fnc_moveInventory;
 *
 * Public: Yes
 */

params ["_unit", "_bodyBag"];

private _items = [];
private _weapons = [];
private _backpack = backpack _unit;
private _nearHolders = _bodyBag nearObjects ["WeaponHolderSimulated", 3];

_items pushBack (headgear _unit);
_items pushBack (uniform _unit);
_items append (uniformItems _unit);
_items pushBack (vest _unit);
_items append (vestItems _unit);
_items append (backpackItems _unit);
_weapons pushBack (primaryWeapon _unit);
_items append (primaryWeaponItems _unit);
_items append (primaryWeaponMagazine _unit);
_weapons pushBack (secondaryWeapon _unit);
_items append (secondaryWeaponItems _unit);
_items append (secondaryWeaponMagazine _unit);
_weapons pushBack (handgunWeapon _unit);
_items append (handgunItems _unit);
_items append (handgunMagazine _unit);
_weapons append (_unit getVariable [QGVAR(droppedWeapons), []]);
_items append (_unit getVariable [QGVAR(droppedItems), []]);
_items append (assignedItems _unit);
_items pushBack (_unit call CFUNC(binocularMagazine));

if  !((goggles _unit ) in (_unit getVariable [QGVAR(droppedItems), []])) then {
    _items pushBack (goggles _unit);
};

_items = _items select {_x != ""};
_weapons = _weapons select {_x != ""};

{ _bodyBag addItemCargoGlobal [_x, 1] } forEach _items;

{
    private _weaponNonPresent = [_x] call  CFUNC(getNonPresetClass);

    if (_weaponNonPresent == "") then {
        _weaponNonPresent = _x;
    };

    _bodyBag addWeaponCargoGlobal [_weaponNonPresent, 1];
} forEach _weapons;

if  (_backpack != "") then {
    private _backpackNonPresent = [_backpack, "CfgVehicles"] call CFUNC(getNonPresetClass);

    if (_backpackNonPresent == "") then {
        _backpackNonPresent = _backpack;
    };

    _bodyBag addBackpackCargoGlobal [_backpackNonPresent, 1];
};

{
    private _holderWeapons = ((getWeaponCargo _x) select 0) select { _x in _weapons };

    if (_holderWeapons isNotEqualTo []) then {
        deleteVehicle _x;
    };
} forEach _nearHolders;