#include "..\script_component.hpp"

// adds default base locations to players' microDAGR as waypoints

if (!hasInterface) exitWith {};

[{!isNull player}, {

	// add base locations (respawn modules)
	{
		private _wpName = [_x] call EFUNC(common,getNameOfBase);
		private _posASL = getPosASL _x;
		[_wpName, _posASL] call ace_microdagr_fnc_deviceAddWaypoint;
	} forEach GVARMAIN(baseObjects);

	// add custom waypoints from mission_settings.hpp
	private _customWaypoints = [missionConfigFile >> "custom_microdagr_waypoints", "ARRAY", []] call CBA_fnc_getConfigEntry;
	{
		_x params [
			["_wpName", ""],
			["_pos", [0, 0, 0], [[], ""]],
			["_object", "", [""]]
		];
		private _realPos = nil;
		// if pos was provided, process
		if (count _pos >= 2) then {
			switch (typeName _pos) do {
				case "ARRAY": {
					// pos is provided as an array
					_realPos = _pos select [0, 2];
					_realPos set [2, getTerrainHeightASL _realPos];
				};
				case "STRING": {
					// pos is provided as a string
					_realPos = [_pos, true] call ACE_common_fnc_getMapPosFromGrid;
					_realPos set [2, getTerrainHeightASL _realPos];
				};
				default {
					[
						LEVEL_WARNING,
						QUOTE(COMPONENT),
						format["Invalid position for custom microDAGR waypoint: %1", _wpName],
					[["name", _wpName], ["pos", _pos], ["object", _object]]] call EFUNC(common,log);
					continue;
				};
			};
		};
		// if object was provided, process and override any pos
		if (count _object > 0) then {
			// object is provided as a string variable name
			private _realObject = missionNamespace getVariable _object;
			if (isNull _realObject) then {
				[
					LEVEL_WARNING,
					QUOTE(COMPONENT),
					format["Invalid object for custom microDAGR waypoint: %1", _wpName],
				[["name", _wpName], ["pos", _pos], ["object", _object]]] call EFUNC(common,log);
				continue;
			};
			_realPos = getPosASL (missionNamespace getVariable _object);
		};
		if (isNil "_realPos") then {
			[
				LEVEL_WARNING,
				QUOTE(COMPONENT),
				format["Invalid waypoint position for custom microDAGR waypoint: %1", _wpName],
			[["name", _wpName], ["pos", _pos], ["object", _object]]] call EFUNC(common,log);
			continue;
		};

		[_wpName, _realPos] call ace_microdagr_fnc_deviceAddWaypoint;
		true;
	} count _customWaypoints;
}] call CBA_fnc_waitUntilAndExecute;

nil;