local QBCore = exports['qb-core']:GetCoreObject()
local oxyvehicle = nil
local startedrun = false

RegisterNetEvent("qb-oxyrun:server:StartOxyPayment", function()
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    if startedrun == false then
        TriggerClientEvent("qb-oxyruns:client:StartOxy", src)
        Player.Functions.RemoveMoney('cash', Config.StartOxyPayment, "Oxy Start")
        startedrun = true
    elseif startedrun == true then
        TriggerClientEvent('QBCore:Notify', src, "Du er allerede igang.", "error", 5000)
    end
    if Config.SpawnOxyVehicle == true then
        if oxyvehicle == nil then
            TriggerClientEvent("qb-oxyrun:client:spawnoxyvehicle", src)
            oxyvehicle = true
        elseif oxyvehicle == true then 
            return 
        end
    end
end)

RegisterNetEvent("qb-oxyrun:server:finishedrun", function()
    startedrun = false
    oxyvehicle = nil
end)

RegisterNetEvent("qb-oxyrun:server:reward", function()
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    local cashchance = math.random(1, 100)
    local rareitem = math.random(100)

    local cash = math.random(Config.CashAmount[1], Config.CashAmount[2])
    if Player then
        if cashchance <= Config.CashChance then
            if Player.Functions.AddMoney("cash", cash, "Oxy Penge") then
                TriggerClientEvent('QBCore:Notify', src, "Du fik "..cash.."DKK", "success", 5000)
            end
        else
            if Player.Functions.AddItem(Config.OxyItem, Config.OxyAmount) then
                TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[Config.OxyItem], "add", Config.OxyAmount)
                TriggerClientEvent('QBCore:Notify', src, "Du fik ingen penge, men du fik "..Config.OxyAmount.." Oxy istedet", "primary", 5000)
            end
        end
        if rareitem <= Config.RareItemChance then
            Player.Functions.AddItem(Config.RareItem, Config.RareItemAmmount, false)
            TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[Config.RareItem], "add", Config.RareItemAmmount)
            TriggerClientEvent('QBCore:Notify', src, "Du fik også en tilfældig ting?", "primary", 5000)
        end
    end
end)