-- Reload inventaire du joueur lors de la sauvegarde
-- au cas ou un item est été enregistrer entre temps

function deleteItemMySQL(id)
    MySQL.Sync.execute("DELETE FROM inventory WHERE id = @id",{
        ['@id'] = id
    })
end

function saveInventoryPlayer(identifier)
    print("Save Inventaire : "..identifier)
    for item, value in pairs(Inventory[identifier]) do
        local quantity = value.quantity
        local slot = value.slot
        local data = value.data
        local id = value.id
        if value[1] == nil then
            if quantity > 0 then
                if id ~= nil then
                    MySQL.Sync.execute("UPDATE inventory SET quantity = @quantity, slot = @slot, data = @data WHERE id = @id",{
                        ['@quantity'] = quantity,
                        ['@slot'] = slot,
                        ['@data'] = data,
                        ['@id'] = id,
                    })                
                else
                    MySQL.Sync.execute("INSERT INTO inventory VALUES (@identifier, @item, @quantity, @slot, @data, @id)",{
                        ['@identifier'] = identifier,
                        ['@item'] = item,
                        ['@quantity'] = quantity,
                        ['@slot'] = slot,
                        ['@data'] = json.encode(data),
                        ['@id'] = nil
                    })
                end
            else
                if id then
                    deleteItemMySQL(id)
                end
            end
        else
            for i = 1, #value do
                local quantity = value[i].quantity
                local slot = value[i].slot
                local data = value[i].data
                local id = value[i].id
                if quantity > 0 then
                    if id ~= nil then
                        MySQL.Sync.execute("UPDATE inventory SET quantity = @quantity, slot = @slot, data = @data WHERE id = @id",{
                            ['@quantity'] = 1,
                            ['@slot'] = slot,
                            ['@data'] = json.encode(data),
                            ['@id'] = id,
                        })                     
                    else
                        MySQL.Sync.execute("INSERT INTO inventory VALUES (@identifier, @item, @quantity, @slot, @data, @id)",{
                            ['@identifier'] = identifier,
                            ['@item'] = item,
                            ['@quantity'] = 1,
                            ['@slot'] = slot,
                            ['@data'] = json.encode(data),
                            ['@id'] = nil
                        })                
                    end
                else
                    if id then
                        deleteItemMySQL(id)
                    end            
                end
            end
        end
    end
end

function createInventoryPlayer(identifier)
    if Inventory[identifier] == nil then
        Inventory[identifier] = {}
    end
end

function addItem(identifier, info, notif)
    createInventoryPlayer(identifier)
    local item = info.item
    local quantity = info.quantity
    local slot = info.slot
    local data = info.data
    local id = info.id

    if data == nil then
        if Inventory[identifier][item] == nil then
            Inventory[identifier][item] = { slot = slot, quantity = quantity, data = data, id = id}
        else
            Inventory[identifier][item].quantity = Inventory[identifier][item].quantity + quantity
        end
    else
        if Inventory[identifier][item] == nil then
            Inventory[identifier][item] = {}
        end
        local pos = #Inventory[identifier][item] + 1
        Inventory[identifier][item][pos] = { slot = slot, quantity = quantity, data = data, id = id}
    end

    if notif then
        local _source = exports.spyoo_base:getSource(identifier)
        TriggerClientEvent("spyoo_notif:notif", _source, { notif = '+ ' .. quantity .. ' ' .. Items[item].label, type = "image", content = "http://localhost/screenfivem/images/inventaire/items/" .. item .. ".png"})
    end

end

function removeItem(identifier, info, notif)
    local item = info.item
    local quantity = info.quantity
    local data = info.data

    if data == nil then
        Inventory[identifier][item].quantity = Inventory[identifier][item].quantity - quantity
    else
        local id = Inventory[identifier][item][data].id
        Inventory[identifier][item][data] = {}
        if id then
            deleteItemMySQL(id)
        end
        loadInventory(identifier)
    end

    if notif then
        local _source = exports.spyoo_base:getSource(identifier)
        TriggerClientEvent("spyoo_notif:notif", _source, { notif = '- ' .. quantity .. ' ' .. Items[item].label, type = "image", content = "http://localhost/screenfivem/images/inventaire/items/" .. item .. ".png"})
    end
end

function loadInventory(identifier)
	local result = MySQL.Sync.fetchAll("SELECT identifier, item, quantity, slot, data, id FROM inventory WHERE identifier = @identifier", {
        ['@identifier'] = identifier
    })
    Inventory[identifier] = {}
	for key, value in pairs(result) do
		local item = value.item
		local quantity = value.quantity
		local slot = value.slot
		local data = json.decode(value.data)
		local id = value.id
		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