local crash_model = `prop_shamal_crash`
local crash_model_laptop = `xm3_prop_xm3_crate_supp_01b`

local CRASH_LOCATION = false

local CRASH_OBJECT_LOCATION = false
local CRASH_BOX_LOCATION = false

local CRASH_OBJECT = false
local CRASH_BOX = false

local blip_pulsing = false
function PulseBlip()
    QBCore.CT(function()
        local alpha = 0
        local increment = 5
        blip_pulsing = true

        while blip_pulsing do
            if alpha >= 255 then
                increment = -5
            elseif alpha <= 0 then
                increment = 5
            end
    
            alpha = alpha + increment
    
            SetBlipAlpha(PULSE_BLIP, alpha)
    
            Wait(5)
        end
    end)

    QBCore.CT(function()
        for i = 1, 10 do
            PlaySoundFrontend(NOTIFICATION_SOUND, "Remote_Enemy_Enter_Line", "GTAO_FM_Cross_The_Line_Soundset", 0)
            Wait(500)
        end
    end)
end

function CreateBlips()
    CRASH_BLIP = AddBlipForCoord(CRASH_OBJECT_LOCATION)
    CRASH_AREA = AddBlipForRadius(CRASH_OBJECT_LOCATION, 150.0)
    PULSE_BLIP = AddBlipForRadius(CRASH_OBJECT_LOCATION, 150.0)

    SetBlipAsMissionCreatorBlip(CRASH_BLIP, true)
    SetBlipAsMissionCreatorBlip(PULSE_BLIP, true)
    SetBlipAsMissionCreatorBlip(CRASH_AREA, true)

    SetBlipSprite(CRASH_BLIP, 653)


    SetBlipColour(CRASH_AREA, 65)
    SetBlipAlpha(CRASH_AREA, 128)

    SetBlipColour(PULSE_BLIP, 1)
    SetBlipAlpha(PULSE_BLIP, 0)

    PulseBlip()
    SendNUIMessage({action = "showWindows"})
end

function Draw3DText(x, y, z, text)
    local onScreen, _x, _y = World3dToScreen2d(x, y, z)
    local camCoords = GetGameplayCamCoords()
    local dist = #(vector3(camCoords.x, camCoords.y, camCoords.z) - vector3(x, y, z))

    local scale = (1 / dist) * 1.5
    local fov = (1 / GetGameplayCamFov()) * 100
    scale = scale * fov

    if onScreen then
        SetTextScale(0.0 * scale, 0.35 * scale)
        SetTextFont(4)
        SetTextProportional(1)
        SetTextColour(255, 255, 255, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(2, 0, 0, 0, 150)
        SetTextDropShadow()
        SetTextOutline()
        SetTextCentre(true)

        SetDrawOrigin(x, y, z, 0) -- Set fixed world origin for text
        BeginTextCommandDisplayText("STRING")
        AddTextComponentSubstringPlayerName(text)
        EndTextCommandDisplayText(0.0, 0.0) -- Draw the text without rotating it
        ClearDrawOrigin() -- Clear the origin after drawing
    end
end

function FormatTime(seconds)
    local minutes = math.floor(seconds / 60)
    local remainingSeconds = seconds % 60
    return string.format("%02d:%02d", minutes, remainingSeconds)
end

local was_hovering_blip = false
local notif_sent = false

QBCore.CT(function()
    while not LocalPlayer.state.player_loaded do
        Wait(100)
    end

    while true do
        Wait(1000)
        local CTF_DATA = GlobalState['CTF_DATA']

        if CTF_DATA.initiated and not CTF_DATA.finished then
            CRASH_LOCATION = CTF_DATA.selected_location
            CRASH_BOX_LOCATION = CRASH_LOCATIONS[CRASH_LOCATION]['box']
            CRASH_OBJECT_LOCATION = CRASH_LOCATIONS[CRASH_LOCATION]['plane']

            if CTF_DATA.initiated_gang == QBPlayer.job.name or CTF_DATA.total_timer < 15*60 then
                if CTF_DATA.initiated_gang ~= QBPlayer.job.name and CTF_DATA.total_timer < 15*60 then
                    if not notif_sent then
                        notif_sent = true
                        QBCore.Functions.Notify("Kutija je skoro spremna za preuzimanje, oznacena je lokacija! Ostalo je jos 10 minuta pripreme za organizaciju. Kutija se NE MOZE oznaciti dok ne istekne vrijeme pripreme!", "success", 20000)
                    end
                end

                if not CRASH_OBJECT then
                    CRASH_OBJECT = CreateMissionObject(crash_model, CRASH_OBJECT_LOCATION, 0, 1)
                    CRASH_BOX = CreateMissionObject(crash_model_laptop, CRASH_BOX_LOCATION, 0, 1)

                    exports.qtarget:AddTargetEntity(CRASH_BOX, {
                        options = {
                            {
                                canInteract = function()
                                    if GlobalState['CTF_DATA'].current_gang == QBPlayer.job.name then return false end
                                    if GlobalState['CTF_DATA'].finished then return false end
                                    if GlobalState['CTF_DATA'].total_timer > 0 then return false end
                                    if QBPlayer.job.name == "unemployed" then return false end
                                    
                                    if IsPedInAnyVehicle(PlayerPedId(), false) then return false end
                                    if LocalPlayer.state.is_dead then return false end

                                    return true
                                end,
                                action = function()
                                    exports.progressbar:Progress({ name = "claimctf", duration = 30000, label = "Oznacavanje...", useWhileDead = false, canCancel = false,
                                        controlDisables = { disableMovement = true, disableCarMovement = true, disableMouse = false, disableCombat = true},
                                        animation = { animDict = "amb@world_human_bum_wash@male@high@idle_a", anim = "idle_a", flags = 49 },
                                    }, function(isCanceled)
                                        if not isCanceled then
                                            QBCore.TSE("mCTF:MarkBox")
                                        end
                                    end)
                                end,
                                icon = "fas fa-car",
                                label = "Oznaci kutiju"
                            },
                        },
                        distance = 2
                    })

                    CreateBlips()
                end
                SendNUIMessage({action = "windowsUpdate", CTF_DATA = CTF_DATA})
            end
        end

        if CRASH_OBJECT and CTF_DATA.finished then
            DeleteObject(CRASH_OBJECT)
            DeleteObject(CRASH_BOX)
            CRASH_OBJECT = false
            CRASH_BOX = false
            CRASH_LOCATION = false
            
            RemoveBlip(CRASH_BLIP)
            RemoveBlip(CRASH_AREA)
            RemoveBlip(PULSE_BLIP)

            CRASH_BLIP = false
            CRASH_AREA = false
            PULSE_BLIP = false

            SendNUIMessage({action = "hideWindows"})
        end

        local selected_blip = GetNewSelectedMissionCreatorBlip()
        if not was_hovering_blip and selected_blip ~= 0 and selected_blip == CRASH_BLIP then
            SendNUIMessage({action = "show"})
            was_hovering_blip = true
        elseif was_hovering_blip and not IsHoveringOverMissionCreatorBlip() then
            SendNUIMessage({action = "hide"})
            was_hovering_blip = false
        end
    end
end)

QBCore.RNE("mCTF:InitiateCTF", function()
    exports.progressbar:Progress({ name = "jammingctf", duration = 15000, label = "Pronalazenje...", useWhileDead = false, canCancel = false,
        controlDisables = { disableMovement = true, disableCarMovement = true, disableMouse = false, disableCombat = true},
        animation = { animDict = "anim@amb@business@cfid@cfid_desk_no_work_bgen_chair_no_work@", anim = "phone_search_lazyworker", flags = 49 },
    }, function(isCanceled)
        if not isCanceled then
            QBCore.TSE("mCTF:InitiateCTF")
        end
    end)
end)
prevedi na esx sve