MySQL.ready(function ()
	Items = Config.Items
	Inventory = {}
	local result = MySQL.Sync.fetchAll("SELECT identifier, item, quantity, slot, data, id FROM inventory", {})
	for key, value in pairs(result) do
		local identifier = value.identifier
		local item = value.item
		local quantity = value.quantity
		local slot = value.slot
		local data = json.decode(value.data)
		local id = value.id

		if Inventory[identifier] == nil then
			Inventory[identifier] = {}
		end

		if quantity <= 0 or Items[item] == nil then
			deleteItemMySQL(id)
		else
			addItem(identifier, { item = item, quantity = quantity, slot = slot, data = data, id = id })
		end
	end
end)

RegisterServerEvent("spyoo_inventory:openInventory")
AddEventHandler("spyoo_inventory:openInventory", function()
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)

	if Inventory[identifier] == nil then
		Inventory[identifier] = {}
	end

	TriggerClientEvent("spyoo_inventory:NUIOpenInventory", _source, Inventory[identifier])
end)

RegisterServerEvent("spyoo_inventory:movedItem")
AddEventHandler("spyoo_inventory:movedItem", function(item, slot, dataNumber)
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	if not dataNumber then
		Inventory[identifier][item].slot = tonumber(slot)
	else
		Inventory[identifier][item][dataNumber].slot = tonumber(slot)
	end
end)

RegisterServerEvent("spyoo_inventory:addItem")
AddEventHandler("spyoo_inventory:addItem", function(me, item, quantity, data, notif)
	local _source = me
	if me == "me" then
		_source = source
	end
	local identifier = exports.spyoo_base:getSteamId(_source)
	addItem(identifier, { item = item, quantity = quantity, slot = slot, data = data, id = id }, true)
end)

RegisterServerEvent("spyoo_inventory:removeItem")
AddEventHandler("spyoo_inventory:removeItem", function(me, item, quantity, data, notif)
	local _source = me
	if me == "me" then
		_source = source
	end
	local identifier = exports.spyoo_base:getSteamId(_source)
	removeItem(identifier, { item = item, quantity = quantity, data = data }, notif)
end)

CanUse = true
RegisterServerEvent("spyoo_inventory:useItem")
AddEventHandler("spyoo_inventory:useItem", function(item, quantity, pos)
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	local arg = false
	if pos ~= nil then
		if CanUse then
			if Inventory[identifier][item] then
				if (Inventory[identifier][item].quantity and Inventory[identifier][item].quantity > 0) then
					arg = quantity
				elseif Inventory[identifier][item][pos] then
					arg = pos
				end
				if arg then
					if RegisterUsableItem[item] then
						RegisterUsableItem[item](_source, item, arg)
						CanUse = false
						Wait(1000)
						CanUse = true
					else
						TriggerClientEvent("spyoo_notif:notif", _source, { notif =  "Vous ne pouvez pas utiliser " .. Items[item].label, type = "image", content = "http://localhost/screenfivem/images/inventaire/items/" .. item .. ".png"})
					end
				end
			end
		end
	end
end)

RegisterServerEvent("spyoo_inventory:renameItem")
AddEventHandler("spyoo_inventory:renameItem", function(item, pos, label)
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	local arg = false
	if pos ~= nil then
		if Inventory[identifier][item][pos] then
			Inventory[identifier][item][pos].data.label = label
		end
	end
end)

RegisterServerEvent("spyoo_inventory:saveMyInventory")
AddEventHandler("spyoo_inventory:saveMyInventory", function()
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	saveInventoryPlayer(identifier)
end)

RegisterServerEvent("spyoo_inventory:savePlayer")
AddEventHandler("spyoo_inventory:savePlayer", function(identifier)
	saveInventoryPlayer(identifier)
end)


RegisterServerEvent("spyoo_inventory:setData")
AddEventHandler("spyoo_inventory:setData", function(item, pos, dataChange, value)
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	Inventory[identifier][item][pos].data[dataChange] = value
end)


RegisterServerEvent("spyoo_inventory:setWearing")
AddEventHandler("spyoo_inventory:setWearing", function(item, pos, bool)
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	Inventory[identifier][item][pos].wearing = not bool
	if not Inventory[identifier][item][pos].wearing then
		Inventory[identifier][item][pos].wearing = nil
	end
end)

RegisterServerEvent("spyoo_inventory:useRaccourciItem")
AddEventHandler("spyoo_inventory:useRaccourciItem", function(raccourci)
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	local find = false
	for key, value in pairs(Inventory[identifier]) do
		if value.slot ~= nil and value.slot == raccourci then
			find = true
			TriggerClientEvent("spyoo_inventory:useItemClient", _source, key, 1, nil)
			break
		elseif value.slot == nil then
			for k,v in pairs(value) do
				if v.slot == raccourci then
					find = true
					TriggerClientEvent("spyoo_inventory:useItemClient", _source, key, 1, k)
					break
				end
			end
		end
	end

	if not find then
		TriggerClientEvent("spyoo_notif:notif", _source, { notif =  "Aucun item trouvé lié à ce raccourci"})
	end

end)

exports("getInventory", function()
	return Inventory
end)

exports("getInventoryUser", function(identifier)
	return Inventory[identifier]
end)

exports("getItems", function()
	return Items
end)