#include "..\script_component.hpp"

// adds a scroll wheel action to all arsenal boxes to spawn different supply crate types

private _arsenalBoxClassName = "Land_PaperBox_open_full_F";

// get all instances of the arsenal item
private _arsenalBoxes = (allMissionObjects _arsenalBoxClassName) select {
	// only select the ones that already have user actions attached
	count (actionIDs _x) > 0;
};

private _supplyCratesCfg = call FUNC(getSupplyCratesCfg);
private _supplyCrateTypesCfgs = _supplyCratesCfg call BIS_fnc_returnChildren;

{
	// add scroll wheel action to spawn different supply box types
	private _arsenalBox = _x;

	// always remove old actions if they exist
	private _actionIDs = _arsenalBox getVariable [QGVAR(supplyCrateActionIDs), []];
	{
		_arsenalBox removeAction _x;
	} forEach _actionIDs;
	_arsenalBox setVariable [QGVAR(supplyCrateActionIDs), []];


	// if setting disabled, skip adding actions
	if (not (
		[QGVAR(setting_allowSupplyBoxScrollWheelSpawning)] call CBA_settings_fnc_get
	)) then {continue};

	
	{ // add an action for each supply crate type
		private _cfg = _x;
		private _supplyCrateDisplayName = (_cfg >> "displayName") call BIS_fnc_getCfgData;

		// add action to spawn supply crate
		private _actionID = _arsenalBox addAction [format ["<t color='#ffffff'>Spawn %1</t>", _supplyCrateDisplayName], {
			params ["_target", "_caller", "_actionId", "_arguments"];
			_arguments params ["_supplyCrateCfg"];
			[
				objNull,
				configName _supplyCrateCfg,
				getPos _target
			] call FUNC(createBox);

			// log action use in server RPT
			private _supplyCrateDisplayName = (_supplyCrateCfg >> "displayName") call BIS_fnc_getCfgData;
			[
				LEVEL_INFO,
				QUOTE(COMPONENT),
				"Supply crate spawned",
				[player, [
					["supplyCrateDisplayName", _supplyCrateDisplayName],
					["supplyCrateCfgName", configName _supplyCrateCfg],
					["position", getPos _target]
				]] call EFUNC(common,addPlayerInfoToArray)
			] remoteExec [QEFUNC(common,log), 2];
		}, [_cfg], 0, false, true, "", ""];
		(_arsenalBox getVariable [QGVAR(supplyCrateActionIDs), []]) pushBack _actionID;
	} forEach _supplyCrateTypesCfgs;
} forEach _arsenalBoxes;