local phoneOpen = false

RegisterCommand('togglephone', function()
    phoneOpen = not phoneOpen
    SetNuiFocus(phoneOpen, phoneOpen)
    SendNUIMessage({ action = 'toggle', open = phoneOpen })
end, false)

RegisterKeyMapping('togglephone', 'Toggle Phone', 'keyboard', 'M')

RegisterNUICallback('close', function(data, cb)
    phoneOpen = false
    SetNuiFocus(false, false)
    SendNUIMessage({ action = 'toggle', open = false })
    cb('ok')
end)

RegisterNUICallback('sendMessage', function(data, cb)
    if type(data.message) == 'string' then
        TriggerServerEvent('gbcore-phone:sendMessage', data.message)
        cb({ stauts = 'sent' })
    else
        cb({ status = 'error' })
    end
end)

RegisterNetEvent('gbcore-phone:receiveMessage')
AddEventHandler('gbcore-phone:receiveMessage', function(sourceId, msg)
    SendNUIMessage({ action = 'receiveMessage', from = sourceeId, message = msg })
end)

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        if phoneOpen then
            DisableControlAction(0, 1, true)
            DisableControlAction(0, 2, true)
        end
    end
end)