params ["_vic", "_path", "_wep", "_magsHere", "_pylons"];

if (_path isEqualTo []) then {_path = [-1]};
// "debug_console" callExtension (_wep + "#0100");
if (_wep in [
	"rhs_weap_MASTERSAFE",
	"rhs_weap_fcs_ammo",
	"rhs_weap_laserDesignator_AI",
	"rhs_weap_fcs_ah64",
	"rhs_weap_DummyLauncher",
	"rhs_weap_fcs_nolrf_ammo",
	"rhsusf_weap_duke",
	"TruckHorn",
	"TruckHorn2",
	"TruckHorn3"
]) exitWith {[[]]};

private _thisArr = [];
_wepConfig = (configFile >> "CfgWeapons" >> _wep);
_wepDisp = [(_wepConfig >> "displayName"), "STRING", "Weapon"] call CBA_fnc_getConfigEntry;
if (_wep == "rhs_weap_smokegen") then {_wepDisp = "Smoke Generator"};
if (_wepDisp isEqualTo "") exitWith {[[]]};

// "debug_console" callExtension (_wepDisp + "#0100");
_magsToCheck = (_vic magazinesTurret _path);


// See if the first default magazine in the config of this weapon is meant to be used on a pylon. If so, process differently
// _mag1pylonWeapon = "";
// _magsConfigArr = [(_wepConfig >> "magazines"), "ARRAY", []] call CBA_fnc_getConfigEntry;
// if (_magsConfigArr isEqualTo []) then {
// 	_mag1pylonWeapon = "";
// } else {
// 	_mag1Name = _magsConfigArr # 0;
// 	_mag1pylonWeapon = [(configFile >> "CfgMagazines" >> _mag1Name >> "pylonWeapon"), "STRING", ""] call CBA_fnc_getConfigEntry;
// };

_pylonMagNames = _pylons apply {_x # 3};

_compatMagsRef = [_wepConfig, true] call CBA_fnc_compatibleMagazines;
_magsFound = _compatMagsRef select {_magsToCheck find _x  != -1};

private _magsArr = [];

if (!((_magsFound # 0) in _pylonMagNames)) then {
	// Not a pylon weapon, so check mags in turret storage
	_thisArr pushBack format["        <font color='#87ffbb' face='PuristaSemiBold'>%1</font>", _wepDisp];
	{
	_thisMag = _x;
		_magAmmo = _vic magazineTurretAmmo [_x, _path];
		_magCount = (_magsHere select {_x # 0 == _thisMag} apply {_x # 1}) # 0;

		private _magConfig = (configFile >> "CfgMagazines" >> _thisMag);
		private _magName = [(_magConfig >> "displayName"), "STRING", "Magazine"] call CBA_fnc_getConfigEntry;
		if (_magName isEqualTo "") then {_magName = "Magazine"};



		_magsArr pushBack format[
			"            + <font color='#4de4ff' size='12' face='PuristaMedium'><font color='#ffffff'>%1</font> <font color='#c2c2c2'>|</font> %2 <font color='#c2c2c2'>rounds</font> <font color='#c2c2c2'>|</font> %3 <font color='#c2c2c2'>mags</font></font>",
			_magName,
			_magAmmo,
			_magCount
		];
	} forEach _magsFound;
} else {
	// This is a pylon weapon, so make a unique entry and directly check ammo on pylon
	_thisArr pushBack format["        <font color='#87ffbb' face='PuristaSemiBold'>%1</font> <font face='PuristaMedium'>[Pylon]</font>", _wepDisp];
};

_thisArr pushBack _magsArr;
// "debug_console" callExtension (str _magsArr + "#0100");
_thisArr;
// _thisArr;
// "debug_console" callExtension (format[
// 	"    %1 | %2 rounds | %3 mags#0110",
// 	getText(configFile >> "CfgMagazines" >> _thisMag >> "displayName"),
// 	_magAmmo,
// 	_magCount
// ]);