function OpenInventory()
	TriggerServerEvent("spyoo_inventory:GetInventory")
end

function SelectedItem(item, count)
	local isIn = true
	local close = false
	RageUI.Visible(MainMenuInventory, not RageUI.Visible(MainMenuInventory))

	local SelectedItemInventory = RageUI.CreateMenu(Config.Items[item].label, "Que souhaitez-vous faire ?");
	SelectedItemInventory.EnableMouse = false;
	function RageUI.PoolMenus:SelectedItemInventory()
		if not RageUI.Visible(SelectedItemInventory) and isIn then
			OpenInventory()
			isIn = false
		end
		if close then
			close = false
			isIn = false
			RageUI.Visible(SelectedItemInventory, false)
		end
		SelectedItemInventory:IsVisible(function(Items)
			if Config.Items[item].usable then
				Items:AddButton("Utiliser", "", { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						TriggerServerEvent("spyoo_inventory:useItem", item, count)
					end
				end)
			end
			if Config.Items[item].giveable then
				Items:AddButton("Donner", "", { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						close = true
						TriggerEvent("spyoo_inventory:giveItem", item, count)
					end
				end)
			end
			if Config.Items[item].dropable then
				Items:AddButton("Jeter", "", { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						close = true
						TriggerServerEvent("spyoo_inventory:dropItem", item, count, pCoords)
					end
				end)
			end
		end, function(Panels)
	end)
	end

	RageUI.Visible(SelectedItemInventory, not RageUI.Visible(SelectedItemInventory))
end

function SelectedUniqueItem(item, pos, inventory, type)
	local isIn = true
	local close = false
	RageUI.Visible(MainMenuInventory, not RageUI.Visible(MainMenuInventory))

	local SelectedUniqueItemInventory = RageUI.CreateMenu(Config.Items[item].label, "Que souhaitez-vous faire ?");
	SelectedUniqueItemInventory.EnableMouse = false;
	function RageUI.PoolMenus:SelectedUniqueItemInventory()
		if not RageUI.Visible(SelectedUniqueItemInventory) and isIn then
			OpenInventoryTypeUnique(inventory, type)
			isIn = false
		end
		if close then
			close = false
			isIn = false
			RageUI.Visible(SelectedUniqueItemInventory, false)
		end
		SelectedUniqueItemInventory:IsVisible(function(Items)
			if Config.Items[item].usable then
				Items:AddButton("Utiliser", "", { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						close = true
						TriggerServerEvent("spyoo_inventory:useUniqueItem", item, pos)
					end
				end)
			end
			if Config.Items[item].giveable then
				Items:AddButton("Donner", "", { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						close = true
						TriggerEvent("spyoo_inventory:giveItem", item, pos, true)
					end
				end)
			end
			if Config.Items[item].dropable then
				Items:AddButton("Jeter", "", { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						close = true
						TriggerServerEvent("spyoo_inventory:dropUniqueItem", item, pos, pCoords)
					end
				end)
			end
		end, function(Panels)
	end)
	end

	RageUI.Visible(SelectedUniqueItemInventory, not RageUI.Visible(SelectedUniqueItemInventory))
end

RegisterNetEvent("spyoo_inventory:OpenInventory")
AddEventHandler("spyoo_inventory:OpenInventory", function(inventory)
	RageUI.CloseAll()
	local ListIndex = {}
	local maxIndex = {}

	local listesOutils = {}
	local listesArmes = {}
	for k,v in pairs(inventory) do
		if Config.Items[k].type == "standard" then
			ListIndex[k] = 1
			maxIndex[k] = {}
			for i = 1, v.count, 1 do
				table.insert(maxIndex[k], i)
			end
		end
		local item = k
		if Config.Items[k].type == "outils" then
			local label = Config.Items[item].label
			for key, value in pairs(v) do
				local labelA
				labelA = label
				if value.data.label then
					labelA = value.data.label
				end
				table.insert(listesOutils, { item = item, pos = key, label = labelA, dura = value.data.dura, count = 1 })
			end
		end
		if Config.Items[k].type == "arme" then
			local label = Config.Items[item].label
			for key, value in pairs(v) do
				local labelA
				labelA = label
				if value.data.label then
					labelA = value.data.label
				end
				table.insert(listesArmes, { item = item, pos = key, label = labelA, weapon = value.data.weapon, ammo = value.data.ammo, count = 1 })
			end
		end
	end

	local MainMenuInventory = RageUI.CreateMenu("Inventaire", "Votre inventaire");
	MainMenuInventory.EnableMouse = false;
	function RageUI.PoolMenus:MainMenuInventory()
		MainMenuInventory:IsVisible(function(Items)
			Items:AddButton("Espèces", "", { IsDisabled = false, RightLabel = "→→→"  }, function(Selected, Hovered, Active)
				if (Selected) then
					OpenInventoryType(inventory, "money")
				end
			end)
			Items:AddButton("Portefeuille", "", { IsDisabled = false, RightLabel = "→→→"  }, function(Selected, Hovered, Active)
				if (Selected) then
					OpenInventoryTypeUnique(inventory, "portefeuille")
				end
			end)
			Items:AddButton("Vêtements", "", { IsDisabled = false, RightLabel = "→→→"  }, function(Selected, Hovered, Active)
				if (Selected) then
					OpenInventoryTypeUnique(inventory, "clothes")
				end
			end)
			for k,v in pairs(inventory) do
				if Config.Items[k].type == "standard" then
					Items:AddList(Config.Items[k].label.." - "..v.count, maxIndex[k], ListIndex[k], Config.Items[k].poids * v.count.." kg", {}, function(Index, Selected, ListChange, Hovered, Active)
						if (ListChange) then
							ListIndex[k] = Index
						end
						if (Selected) then
							SelectedItem(k, ListIndex[k])
						end
					end)
				end
			end
			for k,v in pairs(listesOutils) do
				Items:AddButton(v.label, v.dura.." / 100", { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						SelectedUniqueItem(v.item, v.pos, inventory, type)
					end
				end)
			end
			for k,v in pairs(listesArmes) do
				Items:AddButton(v.label, v.ammo, { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						SelectedUniqueItem(v.item, v.pos, inventory, type)
					end
				end)
			end
		end, function(Panels)
	end)
	end

	RageUI.Visible(MainMenuInventory, not RageUI.Visible(MainMenuInventory))
end)

function OpenInventoryType(inventory, type)
	RageUI.CloseAll()
	local addItem = {}
	local ListIndex = {}
	local maxIndex = {}
	local listes = {}
	local isIn = true
	for k,v in pairs(inventory) do
		if Config.Items[k].type == type then
			table.insert(addItem, { item = k, count = v.count, value = Config.Items[k].value })
			ListIndex[k] = 1
			maxIndex[k] = {}
			for i = 1, v.count, 1 do
				table.insert(maxIndex[k], i)
			end
		end
	end

	table.sort(addItem, function(a, b)
		return a.value < b.value
	end)

	local MenuInventoryType = RageUI.CreateMenu("Inventaire", "Votre inventaire");
	MenuInventoryType.EnableMouse = false;
	function RageUI.PoolMenus:MenuInventoryType()
		if not RageUI.Visible(MenuInventoryType) and isIn then
			OpenInventory()
			isIn = false
		end
		MenuInventoryType:IsVisible(function(Items)
			for i = 1, #addItem, 1 do
				local k = addItem[i].item
				Items:AddList(Config.Items[k].label.." - "..addItem[i].count, maxIndex[k], ListIndex[k], "", {}, function(Index, Selected, ListChange, Hovered, Active)
					if (ListChange) then
						ListIndex[k] = Index
					end
					if (Selected) then
						isIn = false
						SelectedItem(k, ListIndex[k])
					end
				end)
			end
		end, function(Panels)
	end)
	end

	RageUI.Visible(MenuInventoryType, not RageUI.Visible(MenuInventoryType))
end

function OpenInventoryTypeUnique(inventory, type)
	RageUI.CloseAll()
	local ListIndex = {}
	local maxIndex = {}
	local listes = {}
	local isIn = true
	for key, value in pairs(inventory) do
		if Config.Items[key].type == type then
			local label = Config.Items[key].label
			for k,v in pairs(value) do
				local labelA
				labelA = label
				if v.data.label then
					labelA = v.data.label
				end
				table.insert(listes, { item = key, pos = k, label = labelA, count = 1 })
			end
		end
	end

	local MenuInventoryTypeUnique = RageUI.CreateMenu("Inventaire", "Votre inventaire");
	MenuInventoryTypeUnique.EnableMouse = false;
	function RageUI.PoolMenus:MenuInventoryTypeUnique()
		if not RageUI.Visible(MenuInventoryTypeUnique) and isIn then
			OpenInventory()
			isIn = false
		end
		MenuInventoryTypeUnique:IsVisible(function(Items)
			for k,v in pairs(listes) do
				Items:AddButton(v.label, "", { IsDisabled = false }, function(Selected, Hovered, Active)
					if (Selected) then
						isIn = false
						Wait(100)
						SelectedUniqueItem(v.item, v.pos, inventory, type)
					end
				end)
			end
		end, function(Panels)
	end)
	end

	RageUI.Visible(MenuInventoryTypeUnique, not RageUI.Visible(MenuInventoryTypeUnique))
end