#include "..\script_component.hpp"

params [
	["_isInit", false, [false]],
	["_logCurrentAssets", false, [false]]
];

if (!isServer) exitWith {};

GVARMAIN(baseObjects) = allMissionObjects "ModuleRespawnPosition_F";

// Get all approved assets on map, find the closest base
// Then determine if it's within range
// If it is, add it to the base's assets list
// This is to ensure bases with overlapping detection range don't have duplicate assets
private _allVehicles = vehicles;
private _allSaved = [];

private _assetsStartedAtThisBaseVar = QGVAR(assetsStartedAtThisBase);
private _assetsAtThisBaseVar = QGVAR(assetsAtThisBase);

private _approvedAssetsCfg = call EFUNC(common,getApprovedAssetsCfg);
if (isNull _approvedAssetsCfg) exitWith {};

private _currentBaseAssetsGathered = GVARMAIN(baseObjects) apply {[_x, []]};;

{
	private _className = configName _x;
	private _callsign = getText(_x >> "callsign");
	private _found = _allVehicles select { typeOf _x == _className };
	{
		private _asset = _x;

		// ignore assets beyond the range of bases
		if (not ([_asset] call FUNC(isAssetInRangeOfBase))) then {continue};


		// add to base's assets list
		private _closestBase = [_asset] call EFUNC(common,getNearestBase);
		private _closestBaseCurrentAssets = (_currentBaseAssetsGathered select { _x select 0 isEqualTo _closestBase })#0#1;
		_closestBaseCurrentAssets pushBackUnique  [
			_asset call BIS_fnc_netId,
			configOf _asset
		];
	} forEach _found;
} forEach (_approvedAssetsCfg call BIS_fnc_returnChildren);

// Add all ground vehicles (LandVehicle)
{
	private _asset = _x;

	// ignore assets beyond the range of bases
	if (not ([_asset] call FUNC(isAssetInRangeOfBase))) then {continue};

	// add to base's assets list
	private _closestBase = [_asset] call EFUNC(common,getNearestBase);
	private _closestBaseCurrentAssets = (_currentBaseAssetsGathered select { _x select 0 isEqualTo _closestBase })#0#1;
	_closestBaseCurrentAssets pushBackUnique  [
		_asset call BIS_fnc_netId,
		configOf _asset
	];
} forEach (_allVehicles select { _x isKindOf "LandVehicle" });

////////////////////////////////////////////////////////////////////////
// publish updated base variables
////////////////////////////////////////////////////////////////////////
{
	private _base = _x;

	private _thisBaseCurrentAssets = (_currentBaseAssetsGathered select { _x select 0 isEqualTo _base })#0#1;
	_base setVariable [_assetsAtThisBaseVar, _thisBaseCurrentAssets, true];

	if (_isInit) then {
		_base setVariable [_assetsStartedAtThisBaseVar, _thisBaseCurrentAssets, true];
	};
} forEach GVARMAIN(baseObjects);

// send a CBA event to let other scripts know that assets have been gathered
[{[QGVAR(assetsGathered)] call CBA_fnc_globalEvent;}, 2] call CBA_fnc_waitAndExecute;

////////////////////////////////////////////////////////////////////////
// log starting assets if init
// log current assets if requested (for end of mission counts)
////////////////////////////////////////////////////////////////////////
if !(_isInit || _logCurrentAssets) exitWith {};

{
	private _base = _x;

	// get current assets
	private _baseAssets = (_currentBaseAssetsGathered select { _x select 0 isEqualTo _base })#0#1;

	// prepare key value for logging
	private _baseAssetsHashesPrep = _baseAssets apply {
		_x params ["_netId", "_cfg"];
		[
			["callsign", [configName _cfg] call FUNC(getCallsignFromClassname)],
			["className", configName _cfg],
			["displayName", [_cfg] call BIS_fnc_displayName]
		];
	};

	_baseAssetsHashesPrep = _baseAssetsHashesPrep call BIS_fnc_consolidateArray;

	private _baseAssetsHashes = [];
	{
		private _out = createHashMapFromArray (_x#0);
		_out set ["count", _x#1];
		_baseAssetsHashes pushBack _out;
	} forEach _baseAssetsHashesPrep;

	// if logging current assets
	if (_logCurrentAssets) then {
		{
			[
				LEVEL_INFO,
				QUOTE(COMPONENT),
				"CURRENT ASSETS",
				[
					["baseName", [[_base] call EFUNC(common,getNameOfBase)]],
					["asset", _x]
				]
			] call EFUNC(common,log);
		} forEach _baseAssetsHashes;
	};

	// if init, log starting assets
	if (_isInit) then {		
		{
			[
				LEVEL_INFO,
				QUOTE(COMPONENT),
				"STARTING ASSETS",
				[
					["baseName", [[_base] call EFUNC(common,getNameOfBase)]],
					["asset", _x]
				]
			] call EFUNC(common,log);
		} forEach _baseAssetsHashes;
	};
} forEach GVARMAIN(baseObjects);