MySQL.ready(function ()
    Skills = {}
    Levels = {}
    Boost = {}

    for key, value in pairs(Config.Skill) do
        Levels[key] = {}
        Levels[key][1] = 1
        local add = value.baseAdd
        for i = 2, value.maxLevel do
            local number = i / 10
            if number == math.floor(number + 0.5) then
                add = add + value.addPourcentage
            end
            local multiplicateur = 1 + add 
            exp = (Levels[key][i - 1] + value.baseNeedExp) * multiplicateur
            Levels[key][i] = math.floor(exp + 0.5)
        end
    end

    MySQL.Async.fetchAll('SELECT * FROM skills', {}, function(result)
        for key, value in pairs(result) do
            local identifier = value.identifier
            Skills[identifier] = {}
            for k,v in pairs(value) do
                if k ~= "identifier" then
                    Skills[identifier][k] = v
                end
            end
        end
    end)

end)

function isUserMaxLevel(identifier, skill)
    local level = calculateLevel(identifier, skill)
    local maxLevel = Config.Skill[skill].maxLevel
    if level == maxLevel then
        return true
    end
    return false
end

function calculateLevel(identifier, skill)
    local level = 1
    local maxLevel = Config.Skill[skill].maxLevel
    for i = 1, maxLevel do
        if i == maxLevel then
            level = i
            break
        end
        if Levels[skill][i] <= Skills[identifier][skill] and Levels[skill][i+1] >= Skills[identifier][skill] then
            level = i
            break
        end
    end
    return level
end

function calculateNeedExpForNextLevel(identifier, skill)
    if not isUserMaxLevel(identifier, skill) then
        local level = calculateLevel(identifier, skill)
        return Levels[skill][level + 1] - Skills[identifier][skill]
    end
end

function earnExp(identifier, skill, exp)
    if not isUserMaxLevel(identifier, skill) then
        local _source = exports.spyoo_base:getSource(identifier)
        local nextLevel = calculateNeedExpForNextLevel(identifier, skill)
        Skills[identifier][skill] = Skills[identifier][skill] + exp

        if nextLevel <= exp then
            local labelSkill = Config.Skill[skill].label
            local levelSkill = calculateLevel(identifier, skill)
            local needForNextLevel = calculateNeedExpForNextLevel(identifier, skill)
            TriggerClientEvent("spyoo_notif:notif", _source, { notif = "LEVEL UP !<br>Compétence : " .. labelSkill .."<br>Level : " .. levelSkill .. "<br>Prochain level : " .. needForNextLevel .. " points d'exp", time = 9000})
        end
    end
end
function loseExp(identifier, skill, exp)
    Skills[identifier][skill] = Skills[identifier][skill] - exp
end

RegisterServerEvent("spyoo_skill:earnExp")
AddEventHandler("spyoo_skill:earnExp", function(identifier, skill, exp)
    earnExp(identifier, skill, exp)
end)

function boostPlayer(identifier, skill)
    local level = calculateLevel(identifier, skill)
    return Boost[skill][level]
end

exports("getBoost", function(identifier, skill)
	return boostPlayer(identifier, skill)
end)

exports("getSkillInfo", function(skill)
	return Config.Skill[skill].info
end)