/*
 SWAP_BPACK_WEAPON.SQF © Pumpkin  -  V2.0 (appel par event_inventory)
 -------------------------------

 gère les menus d'alternance des armes. Script obligatoirement appelé par menu : récupérer le no du menu et retirer ce menu

 RAPPEL :
 WeaponNoSlot		0
 WeaponSlotPrimary	1
 WeaponSlotHandGun	2
 WeaponSlotSecondary	4
 Mitrailleuse lourde = WeaponSlotPrimary + WeaponSlotSecondary
*/

// Appel systématique par menu : récupérer le no du menu et retirer ce menu
_nomenu = _this select 2;
player removeaction _nomenu;

// Argument
_bpackweapon = _this select 3;

_slot = getNumber (configFile >> "cfgWeapons" >> (format ["%1",_bpackweapon]) >> "type");

_pWeap = primaryWeapon player;
_sWeap = secondaryWeapon player;
_hWeap = "";
{ if (getNumber (configFile >> "cfgWeapons" >> (format ["%1",_x]) >> "type") == 2) then {_hWeap = _x } } foreach weapons player;

// Vérifier si le slot est occupé, par défaut le slot 4 (= arme secondaire) est occupé par le sac à dos lui-même
// Exception : les mitrailleuses lourdes interdisant le sac à dos : slot = 4 + 1 soit arme secondaire + arme primaire (d'où le modulo)
_occupiedslot = 0;
{
	_slotested = getNumber (configFile >> "cfgWeapons" >> (format ["%1",_x]) >> "type");
	if ((_slot % 4) == (_slotested % 4)) then {
		_occupiedslot = _slot;
	};
} foreach weapons player;

// Alterner les armes, mettre à jour la variable "Pumpkin_BackpackContents" et le(s) menu(s)
Switch (_slot) do {
	// Arme primaire
	case 1: {
		player playmove "AmovPercMstpSrasWrflDnon_AmovPercMstpSnonWnonDnon";
		sleep 3;
		if (_occupiedslot > 0) then {
			player removeweapon _pWeap;
			_wpnstored = getText (configFile >> "cfgWeapons" >> (format ["%1",_pWeap]) >> "displayName");
			Pumpkin_menu_primweapon = player addAction [format["Arme %1 (sac à dos)",_wpnstored], "plr_respawn\Swap_bpack_weapon.sqf", _pWeap, 0, false, true, "", "true"];
			Pumpkin_BackpackContents = [Pumpkin_BackpackContents select 0, (Pumpkin_BackpackContents select 1) + [_pWeap]];
		};
		player addweapon _bpackweapon;
		Pumpkin_BackpackContents = [Pumpkin_BackpackContents select 0, (Pumpkin_BackpackContents select 1) - [_bpackweapon]];
	};
	// Arme de poing
	case 2: {
		player playmove "AmovPercMstpSrasWrflDnon_AmovPercMstpSnonWnonDnon";
		sleep 3;
		if (_occupiedslot > 0) then {
			if (_sWeap == "") then {};
			player removeweapon _hWeap;
			_wpnstored = getText (configFile >> "cfgWeapons" >> (format ["%1",_hWeap]) >> "displayName");
			Pumpkin_menu_handweapon = player addAction [format["Arme %1 (sac à dos)",_wpnstored], "plr_respawn\Swap_bpack_weapon.sqf", _hWeap, 0, false, true, "", "true"];
			Pumpkin_BackpackContents = [Pumpkin_BackpackContents select 0, (Pumpkin_BackpackContents select 1) + [_hWeap]];
		};
		player addweapon _bpackweapon;
		Pumpkin_BackpackContents = [Pumpkin_BackpackContents select 0, (Pumpkin_BackpackContents select 1) - [_bpackweapon]];
	};
	// Arme secondaire
	//case 4: {
		// L'arme secondaire (LR) est entièrement gérée par le script "Set_weapons_menu.sqf"
	//};
	// Mitrailleuse lourde interdisant (normalement) le sac à dos
	case 5: {
		player playmove "AmovPercMstpSrasWrflDnon_AmovPercMstpSnonWnonDnon";
		sleep 3;
		if (_occupiedslot > 0) then {
			player removeweapon _pWeap;
			_wpnstored = getText (configFile >> "cfgWeapons" >> (format ["%1",_pWeap]) >> "displayName");
			Pumpkin_menu_primweapon = player addAction [format["Arme %1 (sac à dos)",_wpnstored], "plr_respawn\Swap_bpack_weapon.sqf", _pWeap, 0, false, true, "", "true"];
			Pumpkin_BackpackContents = [Pumpkin_BackpackContents select 0, (Pumpkin_BackpackContents select 1) + [_pWeap]];
		};
		player addweapon _bpackweapon;
		Pumpkin_BackpackContents = [Pumpkin_BackpackContents select 0, (Pumpkin_BackpackContents select 1) - [_bpackweapon]];
	};

	default { hint format ["Erreur Swap_bpack_weapon.sqf : type d'arme non géré - slot %1 (occup=%2)", _slot,_occupiedslot] };
};

_bag = unitBackpack player;
if ((not isnil "Pumpkin_BackpackContents") and (not isNull _bag)) then {

	_backpackmags = Pumpkin_BackpackContents select 0;
	_backpackwpns = Pumpkin_BackpackContents select 1;

	clearMagazineCargo _bag;
	clearWeaponCargo _bag;

	if (count _backpackmags > 0) then {
		for [{_i = 0},{_i < count _backpackmags},{_i = _i + 1}] do { _bag addmagazinecargo [(_backpackmags select _i),1] };
	};
	if (count _backpackwpns > 0) then {
		for [{_i = 0},{_i < count _backpackwpns},{_i = _i + 1}] do { _bag addweaponcargo [(_backpackwpns select _i),1] };
	};
};

if (not isnil "Pumpkin_WeaponsInventory") then {
	Pumpkin_WeaponsInventory = [magazines player, weapons player];
};

_pWeap = primaryWeapon player;
if (format["%1", _pWeap] != "") then {
	player selectweapon _pWeap;
	_wpncfg = configFile >> "CfgWeapons" >> _weap;
	_wpnname = getText (_wpncfg >> "displayName");
	_wpnmuzzles = getArray (_wpncfg >> "muzzles");
	player selectweapon (_wpnmuzzles select 0);
};


reload player;
player playmove "null";

sleep 0.2;
player action ["WEAPONONBACK",player];
sleep 0.2;
player action ["WEAPONINHAND",player];
sleep 0.2;
