/*
	Function: milsim_common_fnc_checkPlayerInventory

	Description:
		Checks a player's inventory for non-compliant items and logs results to all machines.

	Author: Hizumi, EagleTrooper, IndigoFox

	Parameters:
		0: _player - <OBJECT> - Player to check inventory of.

	Returns:
		<ARRAY> - Array of strings to be logged.

*/
#include "..\script_component.hpp"

params [
	["_player", objNull, [objNull]]
];

if (!isPlayer _player) exitWith {
	[
		LEVEL_ERROR,
		QUOTE(COMPONENT),
		"PARAM PLAYER IS NOT A PLAYER",
		[["player", _player]]
	] call EFUNC(common,log);
};

// testing
// test_old = compile preprocessFileLineNumbers "testold.sqf"; 
// test_new = compile preprocessFileLineNumbers "testnew.sqf";
// player addItem "A3_GPNVG18_BLK_TI";
// player addWeapon "SMA_HK417_16in";
// player addItem "ej_VPS15";
// player addItem "ACE_Banana";

// INSTANTIATE VARS
private _foundItemsKeyValue = [];
private _allFoundItemsSoFar = [];

// GET PLAYER ITEMS
private _playerItems = [
	_player, // Unit
	true, // Include weapons, attachments, loaded magazines
	true, // Include items in backpack
	true, // Include items in vest
	true, // Include items in uniform
	true, // Include assigned items
	true // Include not loaded magazines
] call CBA_fnc_uniqueUnitItems;

_playerItems pushBack (headgear _player);
_playerItems pushBack (uniform _player);


[
	LEVEL_DEBUG,
	QUOTE(COMPONENT),
	"CHECKING PLAYER INVENTORY",
	[_player] call EFUNC(common,addPlayerInfoToArray)
] call EFUNC(common,log);

////////////////////////////////////////
// HARDCODED DISALLOWED ITEMS - see functions/definitions/DisallowedEquipment.hpp

// Get parent class
private _disallowedEquipmentCfg = (missionConfigFile >> "DisallowedEquipment");

// Get all subclasses
private _definitionCfgs = (_disallowedEquipmentCfg call BIS_fnc_getCfgSubClasses) apply {
	(_disallowedEquipmentCfg >> _x)
};

// diag_log text format["DEFINITION CFGS: %1", _definitionCfgs];
// Check all items
{ // forEach _subclassesCfgs
	private _definitionCfg = _x;
	private _definitionLabel = (_definitionCfg >> "label") call BIS_fnc_getCfgData;
	private _definitionItems = (_definitionCfg >> "items") call BIS_fnc_getCfgDataArray;
	// diag_log text format["DEF ITEMS: %1", _definitionItems];
	{ // forEach _playerItems
		private _playerItem = _x;
		// diag_log text format["PLAYER ITEM: %1", _playerItem];
		if (_playerItem in _definitionItems && not (_playerItem in _allFoundItemsSoFar)) then {
			// add under def label
			[_foundItemsKeyValue, _definitionLabel, _playerItem] call BIS_fnc_addToPairs;
			// add to all found items
			_allFoundItemsSoFar pushBackUnique _playerItem;
		};
	} forEach _playerItems;
} forEach _definitionCfgs;

////////////////////////////////////////
// UNLISTED THERMAL - Any item with thermal capabilities that is not already in the list

// Identify thermal items in the player list
private _thermalItems = _playerItems select {
	private _playerItem = _x;
	private _modes = getArray(configfile >> "CfgWeapons" >> _playerItem >> "visionMode");
	private _subModes = [configfile >> "CfgWeapons" >> _playerItem >> "ItemInfo" >> "OpticsModes", 1] call BIS_fnc_returnChildren;
	{
		_modes append getArray( _x >> "visionMode" );
	} forEach _subModes;
	if ((count _modes) isNotEqualTo 0) then {
		// if the item has thermal capabilities and is not in the list anywhere else, include it
		"ti" in (_modes apply { toLower _x }) && not (_playerItem in _allFoundItemsSoFar);
	} else {
		false;
	};
};

{
	[_foundItemsKeyValue, "UNLISTED THERMAL", _x] call BIS_fnc_addToPairs;
} forEach _thermalItems;


// Only log compliance message if no non-compliant items were found
if (count _allFoundItemsSoFar isEqualTo 0) exitWith {
	[
		LEVEL_INFO,
		QUOTE(COMPONENT),
		"PLAYER INVENTORY IS COMPLIANT",
		[_player] call EFUNC(common,addPlayerInfoToArray)
	] call EFUNC(common,log);
};

// Log all non-compliant items
{
	_x params ["_categoryLabel", "_items"];
	if (typeName _items isEqualTo "STRING") then {
		_items = [_items];
	};
	{
		private _itemClassName = _x;
		private _itemConfig = _itemClassName call CBA_fnc_getItemConfig;
		// Log to RPT
		[
			LEVEL_WARNING,
			QUOTE(COMPONENT),
			"NON-COMPLIANT ITEM",
			[_player, [
				["category", _categoryLabel],
				["className", _itemClassName],
				["displayName", [_itemConfig] call BIS_fnc_displayName]
			]] call EFUNC(common,addPlayerInfoToArray)
		] call EFUNC(common,log);
	} forEach _items;
} forEach _foundItemsKeyValue;