/*
 * Author: Hizumi & IndigoFox
 *
 * Create Mortar resupply box for the 17th Battalion. Primarily called from milsim_fnc_addZenModules
 *
 * Arguments:
 * 0: Vehicle - <OBJECT>
 * 1: Type - <STRING>
 * 2: Position - <ARRAY>
 *
 * Return Value:
 * Function executed <BOOL>
 *
 * Example:
 * [box, "AmmoSquad"] call milsim_fnc_createBox; 			// create mortar ammo box via init line of editor object
 * [objNull, "AmmoHeavy", pos] call milsim_fnc_createBox;		// create mortar ammo box via zeus module
 *
 * Public: Yes
 *
 * Note: For gathering:
	formatText ["%1", [
		["containerClassname", typeOf cursorObject],
		["backpack", (backpackCargo cursorObject) call BIS_fnc_consolidateArray],
		["item", (itemCargo cursorObject) call BIS_fnc_consolidateArray],
		["magazine", (magazineCargo cursorObject) call BIS_fnc_consolidateArray],
		["magazineAmmo", magazinesAmmoCargo cursorObject],
		["weapon", (weaponCargo cursorObject) call BIS_fnc_consolidateArray]
	]];
 */
 
#include "..\script_component.hpp"

params [
	["_box", objNull, [objNull]],
	["_type", "", [""]],
	["_pos", [0,0,0], [[]], 3]
];

// get defs class
private _supplyCratesCfg = call FUNC(getSupplyCratesCfg);
if (!isClass _supplyCratesCfg) exitWith {
	["Resupply Boxes: Failed to load crate definitions, possibly a bad edit?"] call BIS_fnc_error;
	objNull;
};

// get the subclass names
private _boxTypesAvailable = _supplyCratesCfg call BIS_fnc_getCfgSubClasses;

// if no type is provided, show the available types
if (_type isEqualTo "") exitWith {
	[
		"%1 | %2 %3",
		"Resupply Boxes: [_boxObject, _type, _pos] call milsim_fnc_createBox",
		"No type provided. Please use one of the following types for _type:",
		_boxTypesAvailable joinString ", "
	] call BIS_fnc_error;
	objNull;
};


//////////////////////////////////////////////////////////////////////////
// get the box definition class
//////////////////////////////////////////////////////////////////////////
private _boxDef = (_supplyCratesCfg >> _type);
// validate it exists
if (!isClass _boxDef) exitWith {
	[
		"%1 | %2 %3",
		"Resupply Boxes: [_boxObject, _type, _pos] call milsim_fnc_createBox",
		"Invalid type provided. Please use one of the following types for _type:",
		_boxTypesAvailable joinString ", "
	] call BIS_fnc_error;
	objNull;
};

//////////////////////////////////////////////////////////////////////////
// Create box if one wasn't provided in parameters
//////////////////////////////////////////////////////////////////////////
if (isNull _box) then {
	private _neededBoxType = getText(_boxDef >> "boxClass");
	if (_neededBoxType isEqualTo "") exitWith {
		[
			"Resupply Boxes: Failed to create box. No boxClass defined in the box definition."
		] call BIS_fnc_error;
		objNull;
	};
	_box = _neededBoxType createVehicle _pos;
};


//////////////////////////////////////////////////////////////////////////
// Clear box
//////////////////////////////////////////////////////////////////////////
clearBackpackCargoGlobal _box;
clearItemCargoGlobal _box;
clearMagazineCargoGlobal _box;
clearWeaponCargoGlobal _box;

//////////////////////////////////////////////////////////////////////////
// Get [item, quantity] arrays from definition
//////////////////////////////////////////////////////////////////////////
private _backpacks = ([_boxDef >> "backpacks"] call BIS_fnc_getCfgDataArray);
if (isNil "_backpacks") exitWith {
	[
		"Resupply Boxes: Failed to create box. No backpacks defined in the box definition."
	] call BIS_fnc_error;
	objNull;
};

private _weapons = ([_boxDef >> "weapons"] call BIS_fnc_getCfgDataArray);
if (isNil "_weapons") exitWith {
	[
		"Resupply Boxes: Failed to create box. No weapons defined in the box definition."
	] call BIS_fnc_error;
	objNull;
};

private _magazines = ([_boxDef >> "magazines"] call BIS_fnc_getCfgDataArray);
if (isNil "_magazines") exitWith {
	[
		"Resupply Boxes: Failed to create box. No magazines defined in the box definition."
	] call BIS_fnc_error;
	objNull;
};

private _items = ([_boxDef >> "items"] call BIS_fnc_getCfgDataArray);
if (isNil "_items") exitWith {
	[
		"Resupply Boxes: Failed to create box. No items defined in the box definition."
	] call BIS_fnc_error;
	objNull;
};

//////////////////////////////////////////////////////////////////////////
// Add stuff to box
//////////////////////////////////////////////////////////////////////////
{
	_x params ["_class", "_qty"];
	_box addBackpackCargoGlobal [_class, _qty]
} foreach _backpacks;

{
	_x params ["_class", "_qty"];
	_box addItemCargoGlobal [_class, _qty]
} foreach _items;

{
	_x params ["_class", "_qty"];
	_box addMagazineCargoGlobal [_class, _qty]
} foreach _magazines;

{
	_x params ["_class", "_qty"];
	_box addWeaponCargoGlobal [_class, _qty]
} foreach _weapons;


//////////////////////////////////////////////////////////////////////////
// Adjust ACE settings
//////////////////////////////////////////////////////////////////////////
// set box size
[_box,1] call ace_cargo_fnc_setSize;

// ignore weight restrictions for carry/drag
_box setVariable ["ace_ignoreWeightCarry", true, true];

// Return the box
_box;