/*

 EVENT_INVENTORY.SQF © Pumpkin
 -----------------------------
 Scrute les évènements de l'inventaire pour gérer ensuite le contenu du sac à dos (cf. variable globale non publique "Pumpkin_BackpackContents").

 Aucuns paramètres :
 nul = execVM "plr_respawn\Event_Inventory.sqf"

*/


_testclient = call compile preprocessFile "lib\Client_detection.sqf";
if (not _testclient) exitwith {};

waitUntil {alive player};
sleep 0.5;

// FONCTION(S)

_extractCargo = compile preprocessFile "lib\Extract_Cargo.sqf";

_init_backpackcontents = {
	_bag = unitBackpack player;
	if ( (not (isNull _bag)) ) then {
		_storedmags = getMagazineCargo _bag;
		_storedwpns = getWeaponCargo _bag;

		_backpackmags = _storedmags call _extractCargo;
		_backpackwpns = _storedwpns call _extractCargo;
		Pumpkin_BackpackContents = [_backpackmags, _backpackwpns];
	};
};


// EXECUTION

// Init préliminaire des menus
Pumpkin_menu_primweapon = -1;
Pumpkin_menu_scndweapon = -1;
Pumpkin_menu_handweapon = -1;

Pumpkin_WeaponsInventory = [magazines player, weapons player];

// Sauver le contenu du sac dès le départ et réinitaliser les menus
if ( (not (isNull (unitBackpack player))) ) then {
	[] call _init_backpackcontents;
	nul = execVM "plr_respawn\Set_weapons_menu.sqf";
};

for [{_infinite = 0},{_infinite <= 1},{_infinite = _infinite + 1}] do {

	// attendre un accès à l'inventaire ou l'abandon du sac ou la mort du joueur
	waituntil { (not (isnull findDisplay 106)) or (isnull unitbackpack player) or (not alive player) };

	// Cas du dialogue inventaire
	if (not (isnull findDisplay 106)) then {

		// attendre la fin du dialogue inventaire
		waituntil {not dialog};

		if ( (not (isNull (unitBackpack player))) ) then {
			[] call _init_backpackcontents;
			nul = execVM "plr_respawn\Set_weapons_menu.sqf";
		};

		Pumpkin_WeaponsInventory = [magazines player, weapons player];
	};

	// Mort du joueur
	if (not alive player) exitwith {};

	// Le joueur n'a pas de sac à dos ou l'a abondonné
	if (isnull unitbackpack player) then {

		if (Pumpkin_menu_primweapon != -1) then {player removeaction Pumpkin_menu_primweapon};
		if (Pumpkin_menu_scndweapon != -1) then {player removeaction Pumpkin_menu_primweapon};
		if (Pumpkin_menu_handweapon != -1) then {player removeaction Pumpkin_menu_primweapon};
		Pumpkin_menu_primweapon = -1;
		Pumpkin_menu_scndweapon = -1;
		Pumpkin_menu_handweapon = -1;

		// Mettre le script en attente tant que le joueur n'a pas de sac
		waituntil { (not (isnull findDisplay 106)) or (not (isnull (unitbackpack player))) or (not alive player) };

		if ( (not (isNull (unitBackpack player))) ) then {
			[] call _init_backpackcontents;
			nul = execVM "plr_respawn\Set_weapons_menu.sqf";
		};

	};

	sleep 0.01;
	_infinite = 0;
};
