RegisterServerEvent("spyoo_skin:spawned")
AddEventHandler("spyoo_skin:spawned", function()
	local _source = source
	local User = exports.spyoo_base:getUser(_source)
	local firstJoin = true
	if User.skin then
		for k,v in pairs(User.skin) do
			if k ~= nil then
				firstJoin = false
				break
			end
		end
	end
	if firstJoin then
		TriggerClientEvent("spyoo_skin:CharCreation", _source)
	else
		TriggerClientEvent("skinchanger:loadSkin", _source, User.skin)
	end
end)

RegisterServerEvent("spyoo_skin:saveSkin")
AddEventHandler("spyoo_skin:saveSkin", function(skin, first)
	local _source = source
	local keys = { "skin" }
	local values = { skin }
	TriggerEvent("spyoo_base:updatePlayerData", _source, keys, values, false)
	if first then
		local datatenue
		if skin["sex"] == 0 then
			datatenue = { 
				["tshirt_1"] = 2, ["tshirt_2"] = 1, ["torso_1"] = 2, ["torso_2"] = 2, ["arms"] = 1, 
				["pants_1"] = 7, ["pants_2"] = 2, 
				["shoes_1"] = 2, ["shoes_2"] = 1 
			}
		else
			datatenue = { 
				["tshirt_1"] = 3, ["tshirt_2"] = 1, ["torso_1"] = 1, ["torso_2"] = 3, ["arms"] = 1, 
				["pants_1"] = 26, ["pants_2"] = 3, 
				["shoes_1"] = 2, ["shoes_2"] = 2 
			}
		end
		TriggerEvent("spyoo_inventory:addItem", _source, "tenue", 1, datatenue)
		-- TriggerEvent("spyoo_inventory:giveEspece", _source, 3500)
	end
end)

RegisterServerEvent("spyoo_skin:buyClothes")
AddEventHandler("spyoo_skin:buyClothes", function(item, data)
	local _source = source
	TriggerEvent("spyoo_inventory:addItem", _source, item, 1, data)
end)

RegisterServerEvent("spyoo_skin:loadClothes")
AddEventHandler("spyoo_skin:loadClothes", function()
	local _source = source
	local User = exports.spyoo_base:getUser(_source)
	TriggerClientEvent("spyoo_skin:getClothes", _source, User.clothes)
end)

RegisterServerEvent("spyoo_skin:saveClothes")
AddEventHandler("spyoo_skin:saveClothes", function(data)
	local _source = source
	local keys = {}
	local values = {}
	for k,v in pairs(data) do
		if Config.Data[k] then
			table.insert(keys, k)
			table.insert(values, v)
		end
	end

	TriggerEvent("spyoo_base:updatePlayerData", _source, keys, values, "clothes")
end)