#include "..\script_component.hpp"

if (!hasInterface) exitWith {};

// create diary records

// remove any existing asset map markers
call FUNC(removeMarkersOnMap);

// remove existing asset records
call FUNC(removeAssetDiaryRecords);

(call FUNC(getStartingAndCurrentAssets)) params [
	"_startingAssets",
	"_currentAssets"
];

// get distinct vehicle class names
private _distinctVehiclesClassNames = [];
{
	_x params ["_netId", "_cfg"];
	private _className = configName _cfg;
	_distinctVehiclesClassNames pushBackUnique _className;
} forEach _startingAssets;


// for random color cycling
private _colorSelectionIndex = 0;
private _randomColors = [
	["ColorRed", "#FF0000", "Red"],
	["ColorGreen", "#00FF00", "Green"],
	["ColorBlue", "#0000FF", "Blue"],
	["ColorYellow", "#FFFF00", "Yellow"],
	["ColorWhite", "#FFFFFF", "White"]
];

// ForEach unique vehicle class name, we'll find the first and gather its info
{
	private _className = _x;
	private _vehicleCallsign = toUpper (
		[_className] call FUNC(getCallsignFromClassname)
	);

	// Get all starting assets of this type
	private _startingAssetsOfThisType = _startingAssets select {
		_x params ["_netId", "_cfg"];
		_className isEqualTo (configName _cfg);
	};
	// Get all current assets of this type
	private _currentAssetsOfThisType = _currentAssets select {
		_x params ["_netId", "_cfg"];
		private _object = _netId call BIS_fnc_objectFromNetId;
		// objNull if deleted, then check classname and if alive
		!isNull _object && {_className isEqualTo (typeOf _object) && alive _object};
	};
	// This should never happen, but...
	if (count _startingAssetsOfThisType isEqualTo 0) then {continue};

	// Try to find a not null vehicle that can be processed
	private _exampleVehicleToProcess = objNull;
	private _assetCfg = configNull;

	private _exampleVehicleToProcessIndex = _startingAssetsOfThisType findIf {
		_x params ["_netId", "_cfg"];
		!isNull (_netId call BIS_fnc_objectFromNetId);
	};
	// If found, get the data
	if (_exampleVehicleToProcessIndex > -1) then {
		private _exampleData = _startingAssetsOfThisType select _exampleVehicleToProcessIndex;
		_assetNetId = _exampleData#0;
		_assetCfg = _exampleData#1;
		_exampleVehicleToProcess = _assetNetId call BIS_fnc_objectFromNetId;
	} else {
		// otherwise, we only have the config to work with
		private _exampleData = _startingAssetsOfThisType#0;
		_assetCfg = _exampleData#1;
	};

	private _parentClassNames = [_assetCfg, true] call BIS_fnc_returnParents;
	
	

	// Process the vehicle for extended info
	// Exclusion list for display names
	if (
		[_assetCfg] call BIS_fnc_displayName
		in ["Helicopter"]
	) then {continue};
	
	// Get the vehicle data
	private _processed = [configNull, "", []];
	if (!isNull _exampleVehicleToProcess) then {
		_processed = [_exampleVehicleToProcess] call FUNC(getVehicleData);
	};
	_processed params ["_processedVehicleCfg", "_displayName", "_diaryTextSections"];
	_diaryTextSections params [
		["_title", "", [""]],
		["_image", "", [""]],
		["_info", "", [""]],
		["_capacity", "", [""]]
		// ["_weapons", "", [""]],
		// ["_pylonWeapons", "", [""]],
		// ["_inventory", "", [""]]
	];

	// Get what we can from the vehicle cfg

	// Create the diary record
	private _recordText = [];
	// Add the title and image
	if (count _title isEqualTo 0) then {
		_title = format["%1", [_assetCfg] call BIS_fnc_displayName];
	};
	_recordText pushBack format[
		"<font size='24' shadow='1' color='#e1701a' face='PuristaBold'>%1</font>",
		_title
	];
	if (count _image isEqualTo 0) then {
		_image = format["<img width='200' image='%1'/>", getText(_assetCfg >> 'editorPreview')];
	};
	_recordText pushBack _image;
	_recordText pushBack "<br/>";
	_recordText pushBack format[
		"CALLSIGN: %1",
		_vehicleCallsign
	];
	_recordText pushBack format[
		"COUNT STARTED: %1",
		count _startingAssetsOfThisType
	];
	_recordText pushBack format[
		"COUNT ACTIVE: %1",
		count _currentAssetsOfThisType
	];

	// Here, we'll create a link to show markers on the map for all vehicles of this kind
	// get 'picture' for record
	private _icon = getText(_assetCfg >> "picture");
	// determine marker type
	private _markerType = "mil_dot";
	switch (true) do {
		case ("Helicopter" in _parentClassNames): {
			_markerType = "loc_heli";
		};
		case ("Air" in _parentClassNames): {
			_markerType = "loc_plane";
		};
		case ("Ship" in _parentClassNames): {
			_markerType = "loc_boat";
		};
		case ("Car" in _parentClassNames): {
			_markerType = "loc_car";
		};
		default {
			_markerType = "loc_truck";
		};
	};

	private "_randomColor";
	if (_colorSelectionIndex < count _randomColors) then {
			_randomColor = _randomColors select _colorSelectionIndex;
			INC(_colorSelectionIndex);
	} else {
		_colorSelectionIndex = 0;
		_randomColor = _randomColors select _colorSelectionIndex;
	};

	// Link to show markers
	private _showMarkersText = format[
		"<execute expression='[""%1"",""%2"",""%3"",%4] call %5'>SHOW MARKERS for vehicles at base</execute> (in %6)",
		_className,
		_markerType,
		_randomColor#0,
		(_currentAssetsOfThisType apply {
			_x params ["_netId", "_cfg"];
			_netId;
		}),
		QFUNC(showMarkersOnMap),
		format["<font color='%1'>%2</font>", _randomColor#1, _randomColor#2]
	];
	_recordText pushBack _showMarkersText;

	// Link to hide markers
	_recordText pushBack format[
		"<execute expression=""call %1"">REMOVE ALL MARKERS showing asset positions</execute>",
		QFUNC(removeMarkersOnMap)
	];

	// Link to update asset diary entries
	_recordText pushBack format[
		"<execute expression=""call %1"">UPDATE ENTRIES for all assets</execute>",
		QFUNC(updateAssetDiary)
	];

	// link to display hint with all assets
	_recordText pushBack format[
		"<execute expression=""call %1"">SHOW APPROVED ASSET COUNTS via hint</execute>",
		QFUNC(hintAllApprovedAssets)
	];

	_recordText pushBack format[
		"<font size='10' color='#777777'>%1</font>",
		"Notes:<br/>
			- Markers are only displayed on your local machine.<br/>
			- The REMOVE ALL option will remove all assets' markers from the map.<br/>
			- UPDATE ENTRIES will update the asset diary with the latest information.<br/>
			- Markers will only be displayed for assets that are within a certain distance of a base."
	];



	// Add info and capacity sections - exclude if no living examples were found
	if (count _info > 0) then {
		_recordText pushBack _info;
	};
	if (count _capacity > 0) then {
		_recordText pushBack _capacity;
	};


	private _subjectID = "";
	switch (true) do {
		case ("Helicopter" in _parentClassNames): {
			_subjectID = EGVAR(fbcb2_main,subjectAssetsRotaryID);
		};
		case ("Air" in _parentClassNames): {
			_subjectID = EGVAR(fbcb2_main,subjectAssetsFixedWingID);
		};
		default {
			_subjectID = EGVAR(fbcb2_main,subjectAssetsGroundID);
		};
	};

	[
		_subjectID,
		format[
			"[%1/%2] %3",
			count _currentAssetsOfThisType,
			count _startingAssetsOfThisType,
			(_assetCfg) call BIS_fnc_displayName
		],
		_recordText joinString "<br/>",
		_icon
	] call EFUNC(common,createOrUpdateDiaryRecord);

// "\A3\ui_f\data\igui\cfg\simpleTasks\types\car_ca.paa"
} forEach _distinctVehiclesClassNames;

// log to RPT
[
	LEVEL_INFO,
	QUOTE(COMPONENT),
	"UPDATED ASSET DIARY",
	[
		["startingAssetCount", count _startingAssets],
		["startingAssetCountDistinct", count _distinctVehiclesClassNames],
		["currentassetCount", count _currentAssets]
	]
] call EFUNC(common,log);

true;