/*
	Author: Karel Moricky

	Description:
	Add config defined inventory to an unit

	Parameter(s):
		0: OBJECT - object which will receive the loadout
		1:
			CONFIG - link to CfgVehicles soldier or to CfgRespawnInventory
			ARRAY in format [NAMESPACE or GROUP or OBJECT,STRING] - inventory saved using BIS_fnc_saveInventory
		2: ARRAY of STRINGs - config entries to be ignored (e.g. "weapons", "uniform", ...)

	Returns:
	BOOL
*/

/******************************/
/*****	Elite Warriors	*******/
/*****	 Dark-Raptor	*******/
/******************************/


#define DEFAULT_SLOT 0
#define MUZZLE_SLOT 101
#define OPTICS_SLOT 201
#define FLASHLIGHT_SLOT 301
#define FIRSTAIDKIT_SLOT 401
#define FINS_SLOT 501
#define BREATHINGBOMB_SLOT 601
#define NVG_SLOT 602
#define GOGGLE_SLOT 603
#define SCUBA_SLOT 604
#define HEADGEAR_SLOT 605
#define UNIFORM_SLOT 801// just for DEBUG
#define FACTOR_SLOT 607

#define HMD_SLOT       616
#define BINOCULAR_SLOT 617
#define MEDIKIT_SLOT   619
#define RADIO_SLOT    611

#define VEST_SLOT      701
#define BACKPACK_SLOT  901


call EWDB_fnc_DBGetGear;
sleep 2;
_loadout = EWDBGear select 2;
if(isNil "_loadout") exitWith {EWDB_DBSaveGear_Pause = false;}; //Slot data doesn't exist
if(_loadout isEqualTo "") exitWith {EWDB_DBSaveGear_Pause = false;}; //Slot data doesn't exist
_player = player;

_inventory = _loadout;
_cfg = [_inventory];
_isCfg = count _inventory == 0;



//_blacklist = [];


private ["_items","_linkedItemsMisc","_vest","_headgear","_goggles"];
_items = [];
_linkedItemsMisc = [];
_vest = "";
_headgear = ""; //--- Added as assigned item
_goggles = ""; //--- Added as assigned item
if (_isCfg) then {
	_items = getarray (_cfg >> "items");
	_linkedItems = getarray (_cfg >> "linkedItems");
	_linkedItemsMisc = [];
	{
		_item = _x;
		if (typename _item == typename []) then {_item = _item call bis_fnc_selectrandom;};

		if (isclass (configfile >> "cfgglasses" >> _item)) then {
			_goggles = _item;
		} else {
			_type = getnumber (configfile >> "cfgweapons" >> _item >> "iteminfo" >> "type");
			switch _type do {
				case VEST_SLOT: {_vest = _item;};
				case HEADGEAR_SLOT: {_headgear = _item;};
				//case GOGGLE_SLOT: {_goggles = _item;};
				default {_linkedItemsMisc set [count _linkedItemsMisc,_item];};
			};
		};
	} foreach _linkedItems;
} else {
	_vest = _inventory select 1 select 0;
	_headgear = _inventory select 3;
	_goggles = _inventory select 4;
	_linkedItemsMisc = (_inventory select 9) + (_inventory select 6 select 1) + (_inventory select 7 select 1) + (_inventory select 8 select 1);
};

//--- Remove
//if !("uniform" in _blacklist) then {
	removeuniform _player;
//};
//if !("vest" in _blacklist) then {
	removevest _player;
//};
//if !("headgear" in _blacklist) then {
	removeheadgear _player;
//};
//if !("goggles" in _blacklist) then {
	removegoggles _player;
//};
//if !("backpack" in _blacklist) then {
	removebackpack _player;
//};
//if !("items" in _blacklist) then {
	removeallitems _player;
//};
//if !("linkeditems" in _blacklist) then
{
	private["_headgear","_goggles"];

	//store headgear & goggles to prevent uncontrolled removal
	_headgear = headgear _player;
	_goggles = goggles _player;

	removeallassigneditems _player;

	//re-store headgear & goggles
	if (_headgear != "") then
	{
		_player addheadgear _headgear;
	};
	if (_goggles != "") then
	{
		_player addgoggles _goggles;
	};
};
//if !("weapons" in _blacklist) then {
	removeallweapons _player;
//};
//if !("transportMagazines" in _blacklist) then {
	if (count (getmagazinecargo _player select 0) > 0) then {clearmagazinecargoglobal _player;};
//};
//if !("transportWeapons" in _blacklist) then {
	if (count (getweaponcargo _player select 0) > 0) then {clearweaponcargoglobal _player;};
//};
//if !("transportItems" in _blacklist) then {
	if (count (getitemcargo _player select 0) > 0) then {clearitemcargoglobal _player;};
//};

//--- Add
//if !("uniform" in _blacklist) then {
	private ["_uniform"];
	_uniform = "";
	if (_isCfg) then {
		_uniform = _cfg >> "uniformClass";
		_uniform = if (isarray _uniform) then {(getarray _uniform) call bis_fnc_selectrandom} else {gettext _uniform};
	} else {
		_uniform = _inventory select 0 select 0;
	};
	if (_uniform != "") then {
		if (isclass (configfile >> "cfgWeapons" >> _uniform)) then {
			_player forceadduniform _uniform;
		} else {
			["Uniform '%1' does not exist in CfgWeapons",_uniform] call bis_fnc_error;
		};
	};
//};
//if !("vest" in _blacklist) then {
	if (_vest != "") then {
		if (isclass (configfile >> "cfgWeapons" >> _vest)) then {
			_player addvest _vest;
		} else {
			["Vest '%1' does not exist in CfgWeapons",_vest] call bis_fnc_error;
		};
	};
//};
//if !("headgear" in _blacklist) then {
	if (_headgear != "") then {
		if (isclass (configfile >> "cfgWeapons" >> _headgear)) then {
			_player addheadgear _headgear;
		} else {
			["Headgear '%1' does not exist in CfgWeapons",_headgear] call bis_fnc_error;
		};
	};
//};
//if !("goggles" in _blacklist) then {
	if (_goggles != "") then {
		if (isclass (configfile >> "cfgGlasses" >> _goggles)) then {
			_player addgoggles _goggles;
		} else {
			["Goggles '%1' does not exist in CfgGlasses",_goggles] call bis_fnc_error;
		};
	};
//};
//if !("backpack" in _blacklist) then {
	private ["_backpack"];
	_backpack = "";
	if (_isCfg) then {
		_backpack = _cfg >> "backpack";
		_backpack = if (isarray _backpack) then {(getarray _backpack) call bis_fnc_selectrandom} else {gettext _backpack};
	} else {
		_backpack = _inventory select 2 select 0;
	};
	if (_backpack != "") then {
		if (isclass (configfile >> "cfgVehicles" >> _backpack)) then {
			_player addbackpack _backpack;
		} else {
			["Backpack '%1' does not exist in CfgVehicles",_backpack] call bis_fnc_error;
		};
	};
//};
//if !("magazines" in _blacklist) then {
	if (_isCfg) then {
		private ["_magazines"];
		_magazines = getarray (_cfg >> "magazines");
		{
			if (_x != "") then {
				_magazine = _x;
				if (typename _magazine == typename []) then {_magazine = _magazine call bis_fnc_selectrandom;};
				_player addmagazine _magazine;
			};
		} foreach _magazines;
	} else {
		//--- Add magazines to be loaded in weapons by default
		if ({!isnil "_x"} count (_inventory select 6) > 2) then {
			{
				if (_x != "") then {_player addmagazine _x;};
			} foreach [_inventory select 6 select 2,_inventory select 7 select 2,_inventory select 8 select 2];
		};
	};
//};
//if !("weapons" in _blacklist) then {
	private ["_weapons"];
	_weapons = if (_isCfg) then {getarray (_cfg >> "weapons")} else {[_inventory select 5,_inventory select 6 select 0,_inventory select 7 select 0,_inventory select 8 select 0]};
	{
		if (_x != "") then {
			_weapon = _x;
			if (typename _weapon == typename []) then {_weapon = _weapon call bis_fnc_selectrandom;};
			_player addweapon _weapon;
		};
	} foreach _weapons;
//};
if !(_isCfg) then {
	//--- Add container items (only after weapons were added together with their default magazines)
	{_player additemtouniform _x;} foreach (_inventory select 0 select 1);
	{_player additemtovest _x;} foreach (_inventory select 1 select 1);
	{_player additemtobackpack _x;} foreach (_inventory select 2 select 1);
	// add magazines
	{if (_x select 4 == "Uniform") then {uniformContainer _player addMagazineAmmoCargo [_x select 0,1,_x select 1];};} foreach (_inventory select 10);
	{if (_x select 4 == "Vest") then {vestContainer _player addMagazineAmmoCargo [_x select 0,1,_x select 1];};} foreach (_inventory select 10);
	{if (_x select 4 == "Backpack") then {backpackContainer _player addMagazineAmmoCargo [_x select 0,1,_x select 1];};} foreach (_inventory select 10);
	{if (_x select 3 == 0) then {_player additem (_x select 0);};} foreach (_inventory select 10);
};
//if !("transportMagazines" in _blacklist) then {
	if (_isCfg) then {
		private ["_transportMagazines"];
		_transportMagazines = [];
		{
			_transportMagazines set [count _transportMagazines,[gettext (_x >> "magazine"),getnumber (_x >> "count")]];
		} foreach ([_cfg >> "transportMagazines"] call bis_fnc_subclasses);
		{
			if ((_x select 0) != "") then {
				_player addmagazinecargoglobal _x;
			};
		} foreach _transportMagazines;
	};
//};
//if !("items" in _blacklist) then {
	{
		if (_x != "") then {
			_player additem _x;
		};
	} foreach _items;
//};
//if !("linkeditems" in _blacklist) then {
	{
		if (_x != "") then {
			_player linkitem _x;
			_player addPrimaryWeaponItem _x;
			_player addSecondaryWeaponItem _x;
			_player addHandgunItem _x;
		};
	} foreach _linkedItemsMisc;
//};
//if !("transportWeapons" in _blacklist) then {
	if (_isCfg) then {
		private ["_transportWeapons"];
		_transportWeapons = [];
		{
			_transportWeapons set [count _transportWeapons,[gettext (_x >> "weapon"),getnumber (_x >> "count")]];
		} foreach ([_cfg >> "transportWeapons"] call bis_fnc_subclasses);
		{
			if ((_x select 0) != "") then {
				_player addweaponcargoglobal _x;
			};
		} foreach _transportWeapons;
	};
//};
//if !("transportItems" in _blacklist) then {
	if (_isCfg) then {
		private ["_transportItems"];
		_transportItems = [];
		{
			_transportItems set [count _transportItems,[gettext (_x >> "name"),getnumber (_x >> "count")]];
		} foreach ([_cfg >> "transportItems"] call bis_fnc_subclasses);
		{
			if ((_x select 0) != "") then {
				_player additemcargoglobal _x;
			};
		} foreach _transportItems;
	};
	if (alive player) then{
EWDB_DBSaveGear_Pause = false;
};
//};
//hint "0";
//true