/*
	Used to generate flag category and nested flag child options for the ACE3 interaction menu.

	Parameters:
		0: STRING - Parent action ID
		1: ARRAY - Flag category configs
*/

params ["_rootActionID", "_flagCategoryCfgs"];

private _allCategoryActions = [];

{
	// diag_log format ["NewFlagCategory: %1 %2", _flagCategory, _flagOptions];

	private _flagCategoryCfg = _x;
	private _flagCategoryActionID = getText(_flagCategoryCfg >> "actionID");
	private _flagCategoryTitle = getText(_flagCategoryCfg >> "actionTitle");

	private _flagSubclassesCfgs = _flagCategoryCfg call BIS_fnc_returnChildren;

	private _flagCategoryAction = [
		_rootActionID + "_" + _flagCategoryActionID, // id
		_flagCategoryTitle, // displayed title
		getText((_flagSubclassesCfgs#0) >> "texture"), // flag icon for category - use first flag option
		{true}, // statement
		{
			params ["_target", "_player", "_params"];
			alive _target;
			// true;
		}, // condition
		{
			// generate child code under category
			// this is the level where actual flag options will be displayed
			params ["_target", "_player", "_params"];
			_params params ["_rootActionID", "_flagCategoryActionID", "_flagSubclassesCfgs"];

			private _individualFlagActions = [];
			{ // forEach _flagSubclassesCfgs;
				private _flagOptionCfg = _x;
				private _flagOptionID = getText(_flagOptionCfg >> "actionID");
				private _flagOptionTitle = getText(_flagOptionCfg >> "actionTitle");
				private _flagOptionTexture = getText(_flagOptionCfg >> "texture");

				// if the texture doesn't exist at the config path, skip this flag option
				if (not (fileExists _flagOptionTexture)) then {continue};

				// diag_log format ["NewFlagOption: %1 %2", _flagOptionID, _flagOptionData];

				private _newFlagOption = [
					
					_rootActionID + "_" + _flagCategoryActionID + "_" + _flagOptionID, // id 
					_flagOptionTitle, // displayed title
					_flagOptionTexture, // flag icon
					{
						params ["_target", "_player", "_params"];
						_params params ["_flagOptionTexture"];
						_target forceFlagTexture _flagOptionTexture;
					}, // statement
					{
						params ["_target", "_player", "_params"];
						alive _target;
						// true;
					}, // condition
					nil, // child code
					[_flagOptionTexture], // params
					nil, // position
					4, // distance
					[false, false, false, false, false] // other params
				] call ace_interact_menu_fnc_createAction;

				// add flag option to category subactions
				_individualFlagActions pushBack [_newFlagOption, [], _target];
			} forEach _flagSubclassesCfgs;

			// return the generated flag options to the category as child actions
			_individualFlagActions;
		}, // child code
		[_rootActionID, _flagCategoryActionID, _flagSubclassesCfgs], // params
		nil, // position
		4, // distance
		[false, false, false, false, false], // other params
		nil // modifier function code
	] call ace_interact_menu_fnc_createAction;

	// add category to all category actions array
	_allCategoryActions pushBack [_flagCategoryAction, [], _target];
} forEach _flagCategoryCfgs;

_allCategoryActions;