#include "..\script_component.hpp"

if ( !hasInterface ) exitWith {};

// Create Resupply Box
[
	QUOTE(MODULE_CATEGORY_NAME),
	"Create Resupply Box",
	{
		// module placement
		params [["_pos", [0,0,0], [[]], 3], ["_target", objNull, [objNull]]];
		// get parent definition
		private _supplyCratesCfg = call EFUNC(resupply,getSupplyCratesCfg);
		// get the subclass names
		private _boxTypesAvailable = _supplyCratesCfg call BIS_fnc_getCfgSubClasses;

		_boxTypesAvailable sort true;
		_comboOptions = _boxTypesAvailable apply {
			[
				// display name
				[_supplyCratesCfg >> _x >> "displayName", "STRING", "ERROR"] call CBA_fnc_getConfigEntry,
				// tooltip
				[_supplyCratesCfg >> _x >> "tooltip", "STRING", "ERROR"] call CBA_fnc_getConfigEntry,
				// icon
				[_supplyCratesCfg >> _x >> "icon", "STRING", "ERROR"] call CBA_fnc_getConfigEntry
			]
		};

		[ // create the dialog
			"Resupply Box Options",
			[
				// [ "COMBO", "Box Type", [[1,2,3], [["Ammo"],["Weapons"],["Medical"]],0] ]
				["COMBO", "Box Type", [_boxTypesAvailable, _comboOptions, 0]]
			],
			{

				params ["_dialog", "_args"];

				_dialog params ["_typeOptionSelected"];
				_args params ["_pos", "_target", "_keysSorted"];


				private _box = [_target, _typeOptionSelected, _pos] call EFUNC(resupply,createBox);
				if (isNull _box) exitWith {
					["Resupply Box", "WARNING: Failed to locate or create box!"] call BIS_fnc_curatorHint;
				};
				[
					"Resupply Box",
					format[
						"Created %1",
						getText((call EFUNC(resupply,getSupplyCratesCfg)) >> _typeOptionSelected >> "displayName")
					]
				] call BIS_fnc_curatorHint;
			},
			{},
			[_pos, _target]
		] call zen_dialog_fnc_create;
	}
] call zen_custom_modules_fnc_register;

// Manage wheels in a vehicle's ACE cargo
[
	QUOTE(MODULE_CATEGORY_NAME),
	"Manage Wheels in Vehicle",
	{
		// module placement
		params [["_pos", [0,0,0], [[]], 3], ["_target", objNull, [objNull]]];

		if (isNull _target) exitWith {
			["Manage Wheels in Vehicle", "WARNING: No vehicle selected!"] call BIS_fnc_curatorHint;
		};

		if (_target isKindOf "CAManBase") exitWith {
			["Manage Wheels in Vehicle", "WARNING: Cannot add wheels/tracks to a person!"] call BIS_fnc_curatorHint;
		};

		if (_target isKindOf "Static") exitWith {
			["Manage Wheels in Vehicle", "WARNING: Cannot add wheels/tracks to a static object!"] call BIS_fnc_curatorHint;
		};

		// get loaded details
		private _loadedItems = _target getVariable ["ace_cargo_loaded", []];
		private _loadedItemsObjects = _loadedItems select { _x isEqualType objNull };
		private _loadedItemsVirtual = _loadedItems select { _x isEqualType "" };
		private _spaceLeft = _target getVariable ["ace_cargo_space", 0];

		private _wheels = _loadedItemsVirtual select { _x isEqualTo "ACE_Wheel" };
		_wheels append (_loadedItemsObjects select { typeOf _x isEqualTo "ACE_Wheel" });


		private _maxWheels = floor ((_spaceLeft + count _loadedItems) / ("ACE_Wheel" call ace_cargo_fnc_getSizeItem));
		private _currentWheels = count _wheels;

		private _comboOptions = [[], [], _currentWheels];
		for "_i" from 0 to _maxWheels do {
			_comboOptions#0 pushBack _i;
			_comboOptions#1 pushBack format["%1", _i];
		};


		[ // create the dialog
			"Manage Wheels in Vehicle",
			[
				// [ "COMBO", "Box Type", [[1,2,3], [["Ammo"],["Weapons"],["Medical"]],0] ]
				["TOOLBOX", "Existing Data", [
					0, // default value
					2, // num rows
					2, // num columns
					[
						format["Loaded Items: %1", count _loadedItems],
						format["Space Left: %1", _spaceLeft],
						format["Loaded Wheels: %1", count _wheels]
					]
				]],
				["COMBO", "Desired ACE Wheels Count", _comboOptions, true]
			],
			{

				params ["_dialogValues", "_args"];

				_dialogValues params ["_listItem", "_desiredWheelsCount"];
				_args params ["_pos", "_target", "_currentWheelsCount"];

				// MANAGE WHEELS
				if (_desiredWheelsCount > _currentWheelsCount) then {
					// add wheels
					private _wheelsToAdd = _desiredWheelsCount - _currentWheelsCount;
					systemChat format["Adding %1 wheels...", _wheelsToAdd];
					for "_i" from 1 to _wheelsToAdd do {
						if (
							["ACE_Wheel", _target] call ace_cargo_fnc_canLoadItemIn
						) then {
							["ACE_Wheel", _target] call ace_cargo_fnc_addCargoItem;
						};
					};
				};
				if (_desiredWheelsCount < _currentWheelsCount) then {
					// remove wheels
					private _wheelsToRemove = _currentWheelsCount - _desiredWheelsCount;
					systemChat format["Removing %1 wheels...", _wheelsToRemove];
					["ACE_Wheel", _target, _wheelsToRemove] call ace_cargo_fnc_removeCargoItem;
				};
			

				// get loaded details
				private _loadedItems = _target getVariable ["ace_cargo_loaded", []];
				private _loadedItemsObjects = _loadedItems select { _x isEqualType objNull };
				private _loadedItemsVirtual = _loadedItems select { _x isEqualType "" };
				private _spaceLeft = _target getVariable ["ace_cargo_space", 0];

				private _wheels = _loadedItemsVirtual select { _x isEqualTo "ACE_Wheel" };
				_wheels append (_loadedItemsObjects select { typeOf _x isEqualTo "ACE_Wheel" });


				private _maxWheels = floor ((_spaceLeft + count _loadedItems) / ("ACE_Wheel" call ace_cargo_fnc_getSizeItem));
				private _currentWheels = count _wheels;

				[
					"Manage Wheels in Vehicle",
					format[
						"Updated count of wheels in vehicle: %1",
						_currentWheels
					]
				] call BIS_fnc_curatorHint;
				true;
			},
			{},
			[_pos, _target, count _wheels, count _tracks]
		] call zen_dialog_fnc_create;
	}
] call zen_custom_modules_fnc_register;


// Manage tracks in a vehicle's ACE cargo
[
	QUOTE(MODULE_CATEGORY_NAME),
	"Manage Tracks in Vehicle",
	{
		// module placement
		params [["_pos", [0,0,0], [[]], 3], ["_target", objNull, [objNull]]];

		if (isNull _target) exitWith {
			["Manage Tracks in Vehicle", "WARNING: No vehicle selected!"] call BIS_fnc_curatorHint;
		};

		if (_target isKindOf "CAManBase") exitWith {
			["Manage Tracks in Vehicle", "WARNING: Cannot add wheels/tracks to a person!"] call BIS_fnc_curatorHint;
		};

		if (_target isKindOf "Static") exitWith {
			["Manage Tracks in Vehicle", "WARNING: Cannot add wheels/tracks to a static object!"] call BIS_fnc_curatorHint;
		};

		// get loaded details
		private _loadedItems = _target getVariable ["ace_cargo_loaded", []];
		private _loadedItemsObjects = _loadedItems select { _x isEqualType objNull };
		private _loadedItemsVirtual = _loadedItems select { _x isEqualType "" };
		private _spaceLeft = _target getVariable ["ace_cargo_space", 0];

		private _tracks = _loadedItemsVirtual select { _x isEqualTo "ACE_Track" };
		_tracks append (_loadedItemsObjects select { typeOf _x isEqualTo "ACE_Track" });

		private _maxTracks = floor ((_spaceLeft + count _loadedItems) / ("ACE_Track" call ace_cargo_fnc_getSizeItem));
		private _currentTracks = count _tracks;

		private _comboOptions = [[], [], _currentTracks];
		for "_i" from 0 to _maxTracks do {
			_comboOptions#0 pushBack _i;
			_comboOptions#1 pushBack format["%1", _i];
		};

		[ // create the dialog
			"Manage Tracks in Vehicle",
			[
				// [ "COMBO", "Box Type", [[1,2,3], [["Ammo"],["Weapons"],["Medical"]],0] ]
				["TOOLBOX", "Existing Data", [
					0, // default value
					2, // num rows
					2, // num columns
					[
						format["Loaded Items: %1", count _loadedItems],
						format["Space Left: %1", _spaceLeft],
						format["Loaded Tracks: %1", count _tracks]
					]
				]],
				["COMBO", "Desired ACE Tracks Count", _comboOptions, true]
			],
			{

				params ["_dialogValues", "_args"];

				_dialogValues params ["_listItem", "_desiredTracksCount"];
				_args params ["_pos", "_target", "_currentTracksCount"];

				// MANAGE TRACKS
				if (_desiredTracksCount > _currentTracksCount) then {
					// add tracks
					private _tracksToAdd = _desiredTracksCount - _currentTracksCount;
					systemChat format["Adding %1 tracks...", _tracksToAdd];
					for "_i" from 1 to _tracksToAdd do {
						if (
							["ACE_Track", _target] call ace_cargo_fnc_canLoadItemIn
						) then {
							["ACE_Track", _target] call ace_cargo_fnc_addCargoItem;
						};
					};
				};
				if (_desiredTracksCount < _currentTracksCount) then {
					// remove tracks
					private _tracksToRemove = _currentTracksCount - _desiredTracksCount;
					systemChat format["Removing %1 tracks...", _tracksToRemove];
					["ACE_Track", _target, _tracksToRemove] call ace_cargo_fnc_removeCargoItem;
				};
			

				// get loaded details
				private _loadedItems = _target getVariable ["ace_cargo_loaded", []];
				private _loadedItemsObjects = _loadedItems select { _x isEqualType objNull };
				private _loadedItemsVirtual = _loadedItems select { _x isEqualType "" };
				private _spaceLeft = _target getVariable ["ace_cargo_space", 0];

				private _tracks = _loadedItemsVirtual select { _x isEqualTo "ACE_Track" };
				_tracks append (_loadedItemsObjects select { typeOf _x isEqualTo "ACE_Track" });

				private _maxTracks = floor ((_spaceLeft + count _loadedItems) / ("ACE_Track" call ace_cargo_fnc_getSizeItem));
				private _currentTracks = count _tracks;

				[
					"Manage Tracks in Vehicle",
					format[
						"Updated count of tracks in vehicle: %1",
						_currentTracks
					]
				] call BIS_fnc_curatorHint;
				true;
			},
			{},
			[_pos, _target, _currentTracks]
		] call zen_dialog_fnc_create;
	}
] call zen_custom_modules_fnc_register;





[
	LEVEL_INFO,
	QUOTE(COMPONENT),
	"ZEUS ENHANCED MODULES ADDED",
	[]
] call EFUNC(common,log);

nil;