local QBCore = exports['qb-core']:GetCoreObject()

local function updatetime()
    local level = exports["mz-skills"]:GetCurrentSkill("Cooking").Current
    local time
    print(level)
    if level <= 100 then
        time = Config.CookTime
    elseif level <=200 then
        time = Config.CookTime * 0.9
    elseif level <=400 then
        time = Config.CookTime * 0.8
    elseif level <=600 then
        time = Config.CookTime * 0.7
    elseif level <=800 then
        time = Config.CookTime * 0.6
    else
        time = Config.CookTime * 0.5
    end
    return time
end

local function progresscircle()
    local ped = PlayerPedId()
    TaskTurnPedToFaceCoord(ped, -1196.35, -901.42, 13.89 , 1000)
    if lib.progressCircle({
        label = "Cooking",
        duration = updatetime(),
        position = 'middle',
        useWhileDead = false,
        canCancel = true,
        anim = {
            dict = 'amb@prop_human_bbq@male@idle_a',
            clip = 'idle_b'
        },
        disable = { car = true, move = true, combat = true },
    })
    then
        return true
    else
        return false
    end
end

lib.registerContext({
    id = 'bs_recipies',
    title = 'Recipies',
    options = {
        {
            title = 'Burgers',
            icon = 'fas fa-burger',
            menu = 'bs_burgers',
            arrow = true,
        },
        {
            title = 'Mains',
            icon = 'fas fa-bowl-food',
            menu = 'bs_mains',
            arrow = true,
        },
        {
            title = 'Sides',
            icon = 'fas fa-apple-whole',
            menu = 'bs_sides',
            arrow = true,
        }
    }
})
lib.registerContext({
    id = 'bs_burgers',
    title = 'Burgers',
    options = {
      {
        title = 'Burger',
        description = '2 bread and 1 Beef',
        icon = 'fas fa-burger',
        event = 'tfc-bs-job:client:burger',
      },
      {
        title = 'Cheese Burger',
        description = '2 bread, 1 cheddar and 1 Beef',
        icon = 'fas fa-burger',
        event = 'tfc-bs-job:client:cheddarburger',
      },
      {
        title = 'Bacon Burger',
        description = '2 bread, 1 Pig Meat and 1 Beef',
        icon = 'fas fa-burger',
        event = 'tfc-bs-job:client:baconburger',
      },
      {
        title = 'Bacon Cheese Burger',
        description = '2 bread, 1 cheddar, 1 Pig Meat and 1 Beef',
        icon = 'fas fa-burger',
        event = 'tfc-bs-job:client:baconcheddarburger',
      },
      {
        title = 'Heartstopper Burger',
        description = '4 bread, 3 cheddar, 4 Pig Meat and 5 Beef',
        icon = 'fas fa-burger',
        event = 'tfc-bs-job:client:heartstopperburger',
      },
      {
        title = 'Back',
        icon = 'fas fa-backward',
        menu = 'bs_recipies',
        arrow = false
      }
    }
})
lib.registerContext({
    id = 'bs_mains',
    title = 'Mains',
    options = {
      {
        title = 'Chief Salad',
        description = '2 tomato, 1 broccoli, 1 pig meat and 3 carrots',
        icon = 'fas fa-leaf',
        event = 'tfc-bs-job:client:salad',
      },
      {
        title = 'Pulled Pork Sandwich',
        description = '2 bread, 3 pig meat and 5 onion',
        icon = 'fas fa-bread-slice',
        event = 'tfc-bs-job:client:pulledpork',
      },
      {
        title = 'Hotdog',
        description = '1 pig meat and 1 bread',
        icon = 'fas fa-hotdog',
        event = 'tfc-bs-job:client:hotdog',
      },
      {
        title = 'Back',
        icon = 'fas fa-backward',
        menu = 'bs_recipies',
        arrow = false
      }
    }
})
lib.registerContext({
    id = 'bs_sides',
    title = 'Sides',
    options = {
      {
        title = 'Fries',
        description = '4 Potatoes',
        icon = 'fas fa-bowl-rice',
        event = 'tfc-bs-job:client:fries',
      },
      {
        title = 'Tits or Tots',
        description = '6 Potatoes',
        icon = 'fas fa-cubes-stacked',
        event = 'tfc-bs-job:client:tots',
      },
      {
        title = 'Pickle Spears',
        description = '6 Pickles',
        icon = 'fas fa-cubes-stacked',
        event = 'tfc-bs-job:client:spears',
      },
      {
        title = 'Back',
        icon = 'fas fa-backward',
        menu = 'bs_recipies',
        arrow = false
      }
    }
})

RegisterNetEvent('tfc-bs-job:client:burger', function()
    if QBCore.Functions.HasItem('bread', 2) and QBCore.Functions.HasItem('meat_cow', 1) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeBurger')
            QBCore.Functions.Notify("You recieved a Burger", "success")
            local gain = math.random(1, 3)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:cheddarburger', function()
    if QBCore.Functions.HasItem('bread', 2) and QBCore.Functions.HasItem('meat_cow', 1) and QBCore.Functions.HasItem('cheddar', 1) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeCheeseBurger')
            QBCore.Functions.Notify("You recieved a Cheese Burger", "success")
            local gain = math.random(1, 4)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)

RegisterNetEvent('tfc-bs-job:client:baconburger', function()
    if QBCore.Functions.HasItem('bread', 2) and QBCore.Functions.HasItem('meat_cow', 1) and QBCore.Functions.HasItem('meat_pig', 1) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeBaconBurger')
            QBCore.Functions.Notify("You recieved a Bacon Burger", "success")
            local gain = math.random(1, 4)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:baconcheddarburger', function()
    if QBCore.Functions.HasItem('bread', 2) and QBCore.Functions.HasItem('cheddar', 1) and QBCore.Functions.HasItem('meat_cow', 1) and QBCore.Functions.HasItem('meat_pig', 1) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeBaconCheeseBurger')
            QBCore.Functions.Notify("You recieved a Bacon Cheese Burger", "success")
            local gain = math.random(1, 5)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:heartstopperburger', function()
    if QBCore.Functions.HasItem('bread', 4) and QBCore.Functions.HasItem('cheddar', 4) and QBCore.Functions.HasItem('meat_cow', 5) and QBCore.Functions.HasItem('meat_pig', 4) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeHeartstopperBurger')
            QBCore.Functions.Notify("You recieved a Heartstopper Burger", "success")
            local gain = math.random(2, 7)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:salad', function()
    if QBCore.Functions.HasItem('tomato', 2) and QBCore.Functions.HasItem('broccoli', 1) and QBCore.Functions.HasItem('meat_pig', 1) and QBCore.Functions.HasItem('carrot', 3) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeSalad')
            QBCore.Functions.Notify("You recieved a Salad", "success")
            local gain = math.random(1, 5)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:pulledpork', function()
    if QBCore.Functions.HasItem('meat_pig', 3) and QBCore.Functions.HasItem('bread', 2) and QBCore.Functions.HasItem('onion', 5) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadePulledPork')
            QBCore.Functions.Notify("You recieved a Pulled Pork Sandwich", "success")
            local gain = math.random(1, 4)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:hotdog', function()
    if QBCore.Functions.HasItem('meat_pig', 1) and QBCore.Functions.HasItem('bread', 1) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeHotdog')
            QBCore.Functions.Notify("You recieved a Hotdog", "success")
            local gain = math.random(1, 2)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:fries', function()
    if QBCore.Functions.HasItem('potato', 4) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeFries')
            QBCore.Functions.Notify("You recieved Fries", "success")
            local gain = math.random(1, 3)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:tots', function()
    if QBCore.Functions.HasItem('potato', 6) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeTots')
            QBCore.Functions.Notify("You recieved Tits or Tots", "success")
            local gain = math.random(1, 3)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
RegisterNetEvent('tfc-bs-job:client:spears', function()
    if QBCore.Functions.HasItem('pickle', 6) then
        if progresscircle() then
            TriggerServerEvent('tfc-bs-job:server:MadeSpears')
            QBCore.Functions.Notify("You recieved Pickle Spears", "success")
            local gain = math.random(1, 3)
            exports["mz-skills"]:UpdateSkill("Cooking", gain)
        else
            QBCore.Functions.Notify("Canceled..", "error")
        end
    else
        QBCore.Functions.Notify("You need some more ingredients!", "error")
    end
end)
