#include "..\script_component.hpp"

params [
	["_types", [], [[]]], // classnames to assign these action to
	["_useInheritance", false, [false]]
]; 

{ // forEach _types
	private _classToAddActionsTo = _x;

	private _fileForReinsertAction = [
		QGVAR(fileReinsertRequestAction),
		"File Re-insert Request",
		"\A3\ui_f\data\igui\cfg\simpleTasks\types\takeoff_ca.paa",
		{ // statement
			params ["_target", "_player", "_params"];
			// find nearest base or location
			private _base = [_player] call EFUNC(common,getNearestBase);
			private _baseName = [_base] call EFUNC(common,getNameOfBase);
			// send event to server
			[QGVAR(fileReinsertRequest), [_player, _base]] call CBA_fnc_serverEvent;
			// notify player their request was filed
			[["Re-insert Request Filed"], [format["Location: %1", _baseName]]] call CBA_fnc_notify;
		},
		{ // condition
			params ["_target", "_player", "_params"];
			// find nearest base or location
			private _base = [_player] call EFUNC(common,getNearestBase);
			private _baseDistance = _player distance _base;

			private _maxRangeToReady = [QGVAR(setting_maxRangeToReady)] call CBA_settings_fnc_get;
			private _existingQueue = missionNamespace getVariable [QGVAR(reinsertionQueue), []];

			// check if module is enabled, player is near a base, and player is not already in the queue
			// (serverTime - GVAR(missionStartServerTime)) > 60*5 && // only allow after 15 minutes
			[QGVAR(setting_enabled)] call CBA_settings_fnc_get &&
			(_baseDistance < _maxRangeToReady) &&
			not (_player in (_existingQueue apply {_x#0}))
		}
	] call ace_interact_menu_fnc_createAction;
	[_classToAddActionsTo, 1, ["ACE_SelfActions"], _fileForReinsertAction, _useInheritance] call ace_interact_menu_fnc_addActionToClass;

	private _removeFileForReinsertAction = [
		QGVAR(removeReinsertRequestAction),
		"Remove Re-insert Request",
		"\A3\ui_f\data\igui\cfg\simpleTasks\types\land_ca.paa",
		{ // statement
			params ["_target", "_player", "_params"];
			// send event to server
			[QGVAR(removeReinsertRequest), [_player]] call CBA_fnc_serverEvent;
			// notify player their request was rescinded
			"Re-insert Request Rescinded" call CBA_fnc_notify;
		},
		{ // condition
			params ["_target", "_player", "_params"];
			private _existingQueue = missionNamespace getVariable [QGVAR(reinsertionQueue), []];

			// check if module is enabled, player is in the queue
			// (serverTime - GVAR(missionStartServerTime)) > 60*5 && // only allow after 15 minutes
			[QGVAR(setting_enabled)] call CBA_settings_fnc_get &&
			(_player in (_existingQueue apply {_x#0}))
		}
	] call ace_interact_menu_fnc_createAction;
	[_classToAddActionsTo, 1, ["ACE_SelfActions"], _removeFileForReinsertAction, _useInheritance] call ace_interact_menu_fnc_addActionToClass;
} forEach _types;