// updated 2024-02-01 by IndigoFox
// now reads from the battalion config structure to generate the diary entries

#include "../script_component.hpp"

////////////////////////////////////////
// Get info from missionConfigFile
////////////////////////////////////////
private _battalionInfoCfg = call EFUNC(common,getBattalionCfg);
if (isNull _battalionInfoCfg) exitWith {
	[
		LEVEL_ERROR,
		QUOTE(COMPONENT),
		"Null Battalion Config",
		[]
	] call EFUNC(common,log);
	["Null Battalion Config"] call BIS_fnc_error;
};

private _battalionElementCfgs = [_battalionInfoCfg >> "Command"] call BIS_fnc_returnChildren;
if (count _battalionElementCfgs == 0) exitWith {
	[
		LEVEL_ERROR,
		QUOTE(COMPONENT),
		"ERROR: No battalion elements found. Check that the battalion config is correctly structured. See defines/BattalionInfo.hpp and framework/util/functions/getBattalionCfg.sqf.",
		[]
	] call EFUNC(common,log);
	["ERROR: No battalion elements found. Check that the battalion config is correctly structured. See defines/BattalionInfo.hpp and framework/util/functions/getBattalionCfg.sqf."] call BIS_fnc_error;
};

////////////////////////////////////////
// Define formatting constants
////////////////////////////////////////
private _ELEMENT_NAME_SIZE = 10;
private _ELEMENT_NAME_FONT = "EtelkaMonospaceProBold";
private _ELEMENT_FREQ_SIZE = 9;
private _ELEMENT_FREQ_FONT = "EtelkaMonospacePro";
private _FREQ_INDENT_CONSTANT = 6;
private _FREQ_PAD_LENGTH = 17;
private _FREQ_TEXT_COLOR = "#CCCCCC";
// Note: Element colors are defined in the battalion config

////////////////////////////////////////
// ADD DIARY ENTRIES
////////////////////////////////////////

// First is all the battalion-level elements beneath command
// To have the records listed in the order they appear in the battalion config, we need to reverse the array
// Since each entry is added to the top of the list, this will result in the entries being added in the correct order
reverse _battalionElementCfgs;

{
	[
		LEVEL_TRACE,
		QUOTE(COMPONENT),
		format["Processing battalion element %1", configName _x],
		[]
	] call EFUNC(common,log);
	// recursively generate diary text for all child elements of battalion-level elements
	private _diaryTitleText = [_x, true] call FUNC(generateElementFrequencyRecordText);
	[
		GVAR(subjectFrequenciesID),
		_diaryTitleText#0,
		_diaryTitleText#1
	] call EFUNC(common,createOrUpdateDiaryRecord);
} forEach _battalionElementCfgs;

// add the battalion command to the top of the list
// don't process child elements
private _diaryTitleText = [_battalionInfoCfg >> "Command", false] call FUNC(generateElementFrequencyRecordText);
[
	GVAR(subjectFrequenciesID),
	_diaryTitleText#0,
	_diaryTitleText#1
] call EFUNC(common,createOrUpdateDiaryRecord);

true;