// called from milsim_fnc_processFBCB2RadioFrequencies ONLY
// this function is called recursively to process all child elements of a battalion element in missionConfigFile

#include "../script_component.hpp"

params [
	["_elementCfg", configNull, [configNull]],
	["_shouldProcessChildCfgs", true]
];

if (isNull _elementCfg) exitWith {
	["_elementCfg parameter is NULL"] call BIS_fnc_error;
};

// change reference variable for clarity
private _battalionElement = _elementCfg;

// Generate title from callsign and shortDescription
private _recordTitle = format[
	"%1 (%2)",
	getText(_battalionElement >> "callsign"),
	getText(_battalionElement >> "shortDescription")
];
// systemChat _recordTitle;


//////////////////////////////////////////////////////////
// Generate frequency table header line's leading space
//////////////////////////////////////////////////////////
private _freqLeadingSpace = [
	format["<font size='%1' face='%2'>| ", _ELEMENT_NAME_SIZE, _ELEMENT_NAME_FONT]
];
for "_i" from 1 to _FREQ_INDENT_CONSTANT do {
	_freqLeadingSpace pushBack " ";
};
_freqLeadingSpace pushBack "</font>";
_freqLeadingSpace = _freqLeadingSpace joinString "";

//////////////////////////////////////////////////////////
// Generate header line and frequency table header line
//////////////////////////////////////////////////////////

private _headers = [
	format[
		"<font size='%1' color='%2' face='%3'>%4</font>",
		GVAR(recordTitleSize),
		GVAR(recordTitleColor),
		GVAR(recordTitleFont),
		_recordTitle
	],
	format[
		"%1<font size='%2' face='%3' color='%4'>- %5%6%7</font>",
		_freqLeadingSpace,
		_ELEMENT_FREQ_SIZE,
		_ELEMENT_FREQ_FONT,
		_FREQ_TEXT_COLOR,
		["ROLE", "right", " ", _FREQ_PAD_LENGTH] call EFUNC(common,padString),
		["SR", "right", " ", _FREQ_PAD_LENGTH] call EFUNC(common,padString),
		"LR"
	]
];


//////////////////////////////////////////////////////////
// Generate the list of element headers and frequencies
//////////////////////////////////////////////////////////
private _allText = [];

// get all child elements recursively and format them
if (_shouldProcessChildCfgs) then {
	[
		LEVEL_TRACE,
		QUOTE(COMPONENT),
		"Processing child elements for battalion element %1",
		[]
	] call EFUNC(common,log);

	[_battalionElement, {
		params ["_cfg", "_recurseCounter"];
		// add config
		private _lines = [_cfg, _recurseCounter+1] call FUNC(formatRadioElementForDiary);
		// private _lines = [_cfg, _indentCount] call t;
		_allText pushBack (_lines joinString "<br/>");
	}] call EFUNC(common,recurseSubclasses);
} else {
	[
		LEVEL_TRACE,
		QUOTE(COMPONENT),
		"Skipping processing of child elements for battalion element %1",
		[]
	] call EFUNC(common,log);

	// or if the param was false, just add the battalion element
	private _lines = [_battalionElement, 1] call FUNC(formatRadioElementForDiary);
	// private _lines = [_cfg, _indentCount] call t;
	_allText pushBack (_lines joinString "<br/>");
};

// add headers, add all other lines and format them as monospace
_allText = format[
	"%1<br/><br/><font face='EtelkaMonospaceProBold'>%2</font>", _headers joinString "<br/>", _allText joinString "<br/><br/>"];

// return the title and all text
[_recordTitle, _allText];