#include "..\script_component.hpp"

private _vehicleFlagsCfg = call FUNC(getVehicleFlagsCfg);

if (!isClass _vehicleFlagsCfg) exitWith {
	["WARNING: Vehicle Flags: Vehicle Flags config not found. Vehicle Flags will not be available."] call BIS_fnc_error;
};

private _baseClassesToApplyActionsFor = 
	(_vehicleFlagsCfg >> "baseClassesToApplyActionsFor") call BIS_fnc_getCfgDataArray;
private _flagCategoryCfgs = (_vehicleFlagsCfg >> "FlagCategories") call BIS_fnc_returnChildren;

{ // forEach _baseClassesToApplyActionsFor
	private _parentClass = _x;

	////////////////////////////////////////////////////////////////////////
	// create the root action
	////////////////////////////////////////////////////////////////////////
	private _rootActionID = QGVAR(SetVehicleFlagAction);
	private _flagRootAction = [
		_rootActionID, // id
		"Set Vehicle Flag", // displayed title
		"\A3\ui_f\data\map\markers\flags\nato_ca.paa", // flag icon
		{
			params ["_target", "_player", "_params"];
			// set broadcasted variable of self to indicate we're looking at flags
			_player setVariable [QGVAR(inFlagMenu), true, true];
			[{_this setVariable [QGVAR(inFlagMenu), false, true];}, _player, 3] call CBA_fnc_waitAndExecute;
			true
		}, // statement
		{
			params ["_target", "_player", "_params"];
			// _params params ["_parentActionID", "_flagCategories"];
			
			// check if vehicle is excluded
			private _excluded = [typeOf _target] call FUNC(isClassExcluded);
			if (_excluded || !alive _target) exitWith {false};
			
			true;
		}, // condition
		{
			////////////////////////////////////////////////////////////////////////
			// create the flag category actions (with nested flag actions)
			////////////////////////////////////////////////////////////////////////
			params ["_target", "_player", "_params"];
			_params params ["_rootActionID"];

			private _vehicleFlagsCfg = call FUNC(getVehicleFlagsCfg);
			if (isNull _vehicleFlagsCfg) exitWith {[]};
			private _flagCategoryCfgs = (_vehicleFlagsCfg >> "FlagCategories") call BIS_fnc_returnChildren;

			// return category child actions with individual flag actions nested as children
			[_rootActionID, _flagCategoryCfgs] call FUNC(getActionsFlagCategories);

		}, // child code
		[_rootActionID], // params
		nil, // position
		4, // distance
		[false, false, false, true, false], // other params - run on hover is true
		nil // modifier function code
	] call ace_interact_menu_fnc_createAction;

	////////////////////////////////////////////////////////////////////////
	// add root action to add flags
	////////////////////////////////////////////////////////////////////////
	[
		_parentClass, // parent classname
		0, // action 0 or self-action 1
		["ACE_MainActions"], // parent
		_flagRootAction, // action
		true // apply to child classes
	] call ace_interact_menu_fnc_addActionToClass;
	
	////////////////////////////////////////////////////////////////////////
	// add action to remove flag under the root action
	////////////////////////////////////////////////////////////////////////
	// create action
	private _removeFlagAction = [
		_rootActionID + "_removeflag", // id
		"Remove Flag", // displayed title
		"\A3\ui_f\data\map\markers\flags\nato_ca.paa", // flag icon
		{
			params ["_target", "_player", "_params"];
			_target forceFlagTexture "";
		}, // statement
		{
			params ["_target", "_player", "_params"];
			alive _target && getForcedFlagTexture _target != "";
		}, // condition
		nil // child code
	] call ace_interact_menu_fnc_createAction;

	// add the action to the vehicle
	// in this class event handler, this#0 will be the vehicle
	[
		_parentClass, // parent classname
		0, // action 0 or self-action 1
		["ACE_MainActions", _rootActionID], // parent
		_removeFlagAction, // action
		true // apply to child classes
	] call ace_interact_menu_fnc_addActionToClass;

} forEach _baseClassesToApplyActionsFor;