MySQL.ready(function()
	Users = {}
	MySQL.Async.fetchAll("SELECT * FROM users", {},
		function(result)
		for k,v in pairs(result) do
			Users[v.identifier] = {}
			local coords = json.decode(v.coords)
			Users[v.identifier].coords = vector3(coords.x, coords.y, coords.z)
			Users[v.identifier].skin = json.decode(v.skin)
			Users[v.identifier].clothes = json.decode(v.clothes)
			Users[v.identifier].permission = v.permission
			Users[v.identifier].job = v.job
			Users[v.identifier].job_grade = v.job_grade
		end
	end)
end)

local function OnPlayerConnecting(name, setKickReason, deferrals)
	local _source = source
	local identifiers = GetPlayerIdentifiers(_source)
	local steamIdentifier = false
	deferrals.defer()
	Wait(100)
	for _, v in pairs(identifiers) do
		if string.find(v, "steam") then
			steamIdentifier = v
			break
		end
	end
	if not steamIdentifier then
		deferrals.done("Tu n'es pas connecté à Steam")
	end

	Wait(500)

	if Users[steamIdentifier] then
		Users[steamIdentifier].source = _source
	else
		MySQL.Sync.execute("INSERT INTO users (identifier, coords) VALUES (@identifier, @coords)",{
			["@identifier"] = steamIdentifier,
			["@coords"] = json.encode(Config.FirstSpawn),
		})
		Users[steamIdentifier] = {}
		Users[steamIdentifier].source = _source
		Users[steamIdentifier].coords = Config.FirstSpawn
		Users[steamIdentifier].skin = {}
		Users[steamIdentifier].clothes = {}
		Users[steamIdentifier].permission = 0
		Users[steamIdentifier].job = "none"
		Users[steamIdentifier].job_grade = 0
	end
	
	deferrals.done()
end

AddEventHandler("playerConnecting", OnPlayerConnecting)

AddEventHandler("playerDropped", function()
	local _source = source
	local identifier = getSteamId(_source)
	SavePlayer(identifier)
	TriggerEvent("spyoo_inventory:savePlayer", identifier)
	Users[identifier].source = nil
end)

RegisterServerEvent("spyoo_base:spawned")
AddEventHandler("spyoo_base:spawned", function()
	local _source = source
	local identifier = getSteamId(_source)
	local coords = Users[identifier].coords
	TriggerClientEvent("spyoo_base:playerSpawned", _source, coords)
end)

RegisterServerEvent("spyoo_base:hasSpawn")
AddEventHandler("spyoo_base:hasSpawn", function()
	local _source = source
	local identifier = getSteamId(_source)
	TriggerClientEvent("spyoo_skin:hasSpawn", _source)
end)

RegisterServerEvent("spyoo_base:updateCoords")
AddEventHandler("spyoo_base:updateCoords", function(coords)
	local _source = source
	local identifier = getSteamId(_source)
	Users[identifier].coords = coords
end)

RegisterServerEvent("spyoo_base:updatePlayerData")
AddEventHandler("spyoo_base:updatePlayerData", function(me, keys, values, specialKey)
	local _source = me
	if me == "me" then
		_source = source
	end
	local identifier = getSteamId(_source)
	for k,v in pairs(keys) do
		if specialKey then
			if Users[identifier][specialKey] == nil then
				Users[identifier][specialKey] = {}
			end
			Users[identifier][specialKey][v] = values[k]
		else
			Users[identifier][v] = values[k]
		end
	end
end)

RegisterServerEvent("spyoo_base:getPlayer")
AddEventHandler("spyoo_base:getPlayer", function()
	local _source = source
	local identifier = getSteamId(_source)
	TriggerClientEvent("spyoo:getData", _source, Users[identifier])
end)

function getSteamId(source)
	local steamIdentifier
	for _, v in pairs(GetPlayerIdentifiers(source)) do
		if string.find(v, "steam") then
			steamIdentifier = v
			break
		end
	end
	return steamIdentifier
end

function getUser(source)
	local _source = source
	local identifier = getSteamId(_source)
	return Users[identifier]
end

function getSource(identifier)
	return Users[identifier].source
end

function isPlayerOnline(source)
	if GetPlayerName(source) == nil then
		return false
	else
		return true
	end
end

function SavePlayer(identifier)
	print("Save : "..identifier)
	MySQL.Sync.execute("UPDATE users SET job = @job, job_grade = @job_grade, permission = @permission, coords = @coords, skin = @skin, clothes = @clothes WHERE identifier = @identifier", {
		["@job"] = Users[identifier].job,
		["@job_grade"] = Users[identifier].job_grade,
		["@permission"] = Users[identifier].permission,
		["@coords"] = json.encode(Users[identifier].coords),
		["@skin"] = json.encode(Users[identifier].skin),
		["@clothes"] = json.encode(Users[identifier].clothes),
		["@identifier"] = identifier,
	})
end

exports("getUsers", function()
	return Users
end)

exports("getUser", function(source)
	return getUser(source)
end)

exports("getSteamId", function(source)
	return getSteamId(source)
end)

exports("getSource", function(identifier)
	return getSource(identifier)
end)

exports("isPlayerOnline", function(source)
	return isPlayerOnline(source)
end)