listing = nil
props = {}
identifier = nil
RegisterNetEvent('mailbox:notification')
AddEventHandler('mailbox:notification', function(message, typeNotif)
    notificationfunction(message, typeNotif)
end)

RegisterNetEvent('mailbox:refresh')
AddEventHandler('mailbox:refresh', function()
    initialiseScripts()
end)

RegisterNetEvent('mailbox:refresh_edit')
AddEventHandler('mailbox:refresh_edit', function(idmailbox_delete, position, props_delete, oldposition)
    if oldposition ~= nil then
        closestPropsAncien = GetClosestObjectOfType(oldposition.x, oldposition.y, oldposition.z, 1.4, GetHashKey(props_delete), false, false, false) 
        if closestPropsAncien ~= 0 then
            DeleteObject(closestPropsAncien)
            ClearAreaOfObjects(vector3(oldposition.x, oldposition.y, oldposition.z), 2.0, 0)
        end
    end

    closestProps = GetClosestObjectOfType(position.x, position.y, position.z, 1.4, GetHashKey(props_delete), false, false, false)
    if closestProps ~= 0 then
        DeleteObject(closestProps)
        ClearAreaOfObjects(vector3(position.x, position.y, position.z), 2.0, 0)
    end

    if Config.interact == "ox_target" then
        for k,v in pairs(target_ox) do 
            exports.ox_target:removeLocalEntity('open_mailbox-'..idmailbox_delete)
        end
    end
    Wait(2000)
    if Config.interact == "ox_target" then
        target_ox = {}
    end
    initialiseScripts()
end)

function initialiseScripts()
    listing = nil

    if identifier == nil then
        TriggerServerCallback('mailbox:getidentifier', function(result_identifier)   
            identifier = result_identifier
        end)
    end

    while identifier == nil do
        Wait(1)
    end

    TriggerServerCallback('mailbox:listing', function(callback)   
        listing = callback
    end)

    while listing == nil do
        Wait(1)
    end

    if Config.enableProps == true then
        for k,v in pairs(listing) do
            if v.enableProps == "yes" then
                closestProps = GetClosestObjectOfType(v.position.x, v.position.y, v.position.z, 1.4, GetHashKey(v.props), false, false, false)
                if closestProps ~= 0 then
                    DeleteObject(closestProps)
                end
                props = CreateObject(v.props, v.position.x, v.position.y, v.position.z, false, true, true)
                SetEntityHeading(props, tonumber(v.heading) + 180)
                Wait(200)
                PlaceObjectOnGroundProperly(props)
                if Config.interact == "ox_target" then
                    target_launch(props, vector3(v.position.x, v.position.y, v.position.z), v.id, v.whitelist)
                end
            end
        end
    end
end

if Config.interact ~= "ox_target" then
    Citizen.CreateThread(function()
        while true do
            if listing ~= nil then
                for k,v in pairs(listing) do
                    local playerPos = GetEntityCoords(PlayerPedId())
                    local distanceProps = vector3(v.position.x, v.position.y, v.position.z )
                    local distanceEntree = #(distanceProps-playerPos)
                    if distanceEntree <= 2.0 then
                        helpnotificationfunction(Locale["interact_mailbox"], vector3(v.position.x, v.position.y, v.position.z + 0.5))
                        if IsControlJustPressed(1,51) then
                            mailbox_menu(v.id, v.job)
                        end
                    end
                end
            end
            Citizen.Wait(0)
        end
    end)
end