#include "..\script_component.hpp"

params [
	["_className", "", [""]],
	["_markerType", "hd_dot", [""]],
	["_markerColor", "", [""]],
	["_assetObjects", [], []]
];

if (count _className isEqualTo 0) exitWith {
	["No class name provided!"] call BIS_fnc_error;
};
if (count _markerColor isEqualTo 0) exitWith {
	["No marker color provided!"] call BIS_fnc_error;
};
if (count _assetObjects isEqualTo 0) exitWith {
	["No vehicles to draw markers for!"] call BIS_fnc_error;
};

private _baseMarkerStoreVar = QGVAR(baseMarkerStore);
private _assetMarkerStoreVar = QGVAR(assetMarkerStore);

private _baseMarkerStore = localNamespace getVariable [
	_baseMarkerStoreVar,
	[]
];
private _assetMarkerStore = localNamespace getVariable [
	_assetMarkerStoreVar,
	[]
];

///////////////////////////////////////////////////////////////////////////////
// Create base markers if not already present
if (not (count _baseMarkerStore > 0)) then {
	{ // milsim_baseObjects is accessed directly, so are valid objects
		private _base = _x;

		// create a circle marker with range as the detection range of assets
		_newMarker = createMarkerLocal [
			format["%1_%2", QGVAR(baseCircleMarker), _forEachIndex + 1],
			getPosASL _base
		];
		_newMarker setMarkerTypeLocal "mil_dot";
		_newMarker setMarkerColorLocal "ColorGreen";
		_newMarker setMarkerShapeLocal "ELLIPSE";
		_newMarker setMarkerSizeLocal [
			GVAR(setting_detectionRangeFromBase),
			GVAR(setting_detectionRangeFromBase)
		];
		_newMarker setMarkerAlphaLocal 0.5;
		_newMarker setMarkerTextLocal str(_forEachIndex + 1);

		_baseMarkerStore pushBack [
			_base,
			_newMarker
		];

		// create a flag marker at base position
		_newMarker = createMarkerLocal [
			format["%1_%2", QGVAR(baseFlagMarker), _forEachIndex + 1],
			getPosASL _base
		];
		_newMarker setMarkerTypeLocal "mil_flag";
		_newMarker setMarkerColorLocal "ColorGreen";
		_newMarker setMarkerSizeLocal [0.7, 0.7];
		_newMarker setMarkerTextLocal ([_base] call EFUNC(common,getNameOfBase));

		_baseMarkerStore pushBack [
			_base,
			_newMarker
		];
	} forEach GVARMAIN(baseObjects);

	localNamespace setVariable [_baseMarkerStoreVar, _baseMarkerStore];
};


private _start = (count _assetMarkerStore) + 1;
{ // _assetObjects is a serialized parameter, so we pass the netIds (strings)
	private _assetNetIdStr = _x;
	private _asset = _assetNetIdStr call BIS_fnc_objectFromNetId;
	// if asset was removed since last update
	if (isNull _asset) then {continue};
	// check if a marker is already placed for this asset
	if (
		(_assetMarkerStore findIf { _x select 0 isEqualTo _asset })
		> -1
	) then {continue};

	// check if the asset is within base detection range
	if (not ([_asset] call FUNC(isAssetInRangeOfBase))) then {continue};

	// create a marker for the asset
	private _newMarker = createMarkerLocal [
		format["%1_%2", QGVAR(assetMarker), _start],
		getPosASL _asset
	];
	_newMarker setMarkerAlphaLocal 1;
	_newMarker setMarkerTypeLocal _markerType;
	_newMarker setMarkerColorLocal _markerColor;
	// _newMarker setMarkerTextLocal ([configOf _asset] call BIS_fnc_displayName);

	_assetMarkerStore pushBack [
		_asset,
		_newMarker
	];
	_start = _start + 1;
} forEach _assetObjects;

// update store var
localNamespace setVariable [_assetMarkerStoreVar, _assetMarkerStore];
