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

-------------------- Gloabl Player Emails --------------------

-- Emergency Alert
RegisterNetEvent('strez:client:SendEmergencyEmail', function(text)
    Wait(math.random(7000, 13000))
    if Config.Phone == 'qb' then
        TriggerServerEvent('qb-phone:server:sendNewMail', {
        sender = "City of Los Santos",
        subject = "Emergency Alert",
        message = text,
        button = {}
    })
    elseif Config.Phone == 'qs' then
        TriggerServerEvent('qs-smartphone:server:sendNewMail', {
        sender = "City of Los Santos",
        subject = "Emergency Alert",
        message = text,
        button = {}
    })
    elseif Config.Phone == 'gks' then
        TriggerServerEvent('gksphone:NewMail', {
        sender = "City of Los Santos",
        subject = "Emergency Alert",
        message = text,
        button = {}
    })
    elseif Config.Phone == 'lb' then
        TriggerServerEvent("strez:server:lb-phone:sendMail", {
        sender = "City of Los Santos", 
        subject = "Emergency Alert",
        message = text, 
        })
    elseif Config.Phone == 'ys' then
        local insertId, received = exports.yseries:SendMail({
        sender = 'City Of Los Santos',
        title = "Emergency Alert",
        content = text,
        }, 'all')
    end
end)

-- Announcement Email
RegisterNetEvent('strez:client:SendAnnouncementEmail', function(text)
    Wait(math.random(7000, 13000))
    if Config.Phone == 'qb' then
        TriggerServerEvent('qb-phone:server:sendNewMail', {
        sender = "City of Los Santos",
        subject = "Announcement",
        message = text,
        button = {}
    })
    elseif Config.Phone == 'qs' then
        TriggerServerEvent('qs-smartphone:server:sendNewMail', {
        sender = "City of Los Santos",
        subject = "Announcement",
        message = text,
        button = {}
    })
    elseif Config.Phone == 'gks' then
        TriggerServerEvent('gksphone:NewMail', {
        sender = "City of Los Santos",
        subject = "Announcement",
        message = text,
        button = {}
    })
    elseif Config.Phone == 'lb' then
        TriggerServerEvent("strez:server:lb-phone:sendMail", {
        sender = "City of Los Santos", 
        subject = "Announcement",
        message = text,
        })
    elseif Config.Phone == 'ys' then
        local insertId, received = exports.yseries:SendMail({
        sender = 'City Of Los Santos',
        title = "Announcement",
        content = text,
        }, 'all')
    end
end)

-------------------- Manual Tsunami Restart --------------------

-- Manual Warning Email
RegisterNetEvent('strez:client:TsunamiManual', function(text)
    local message = (Config.Messages.ManualRestart)
    Wait(math.random(5000, 15000))
    TriggerEvent('InteractSound_CL:PlayOnOne', 'Alert', 0.2)
    Wait(math.random(5000, 15000))
    if Config.Notify == 'qb' then
        TriggerEvent('QBCore:Notify', message, 'success', 15000)
    elseif Config.Notify == 'ox' then
        lib.notify({ description = message, duration = 15000,type = 'info', position = 'top-right'})
    elseif Config.Notify == 'email' then
        if Config.Phone == 'qb' then
            TriggerServerEvent('qb-phone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Emergency Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'qs' then
            TriggerServerEvent('qs-smartphone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Emergency Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'gks' then
            TriggerServerEvent('gksphone:NewMail', {
            sender = "City of Los Santos",
            subject = "Emergency Broadcast",
            message = message,
            button = {}
            })
        elseif Config.Phone == 'lb' then
            TriggerServerEvent("strez:server:lb-phone:sendMail", {
            sender = "City of Los Santos", 
            subject = "Emergency Broadcast",
            message = message, 
            })
        elseif Config.Phone == 'ys' then
            local insertId, received = exports.yseries:SendMail({
            sender = 'City Of Los Santos',
            title = "Emergency Broadcast",
            content = message,
            }, 'all')
        end
    end
    Wait(math.random(5000, 15000))
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:setWeather', 'THUNDER')
    elseif Config.Weather == 'cd' then
        data = { weather = "THUNDER", instantweather = false }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.weather = { weather = "THUNDER" }
    end
    Wait(math.random(5000, 15000))
    TriggerEvent('InteractSound_CL:PlayOnOne', 'AirSiren', 0.1)
    TriggerEvent('strez:raisewater')
    if Config.Water == 'calm' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/calmflood.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    elseif Config.Water == 'wavy' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/wavyflood.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    end
    Wait(math.random(5000, 15000))
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:toggleBlackout')
    elseif Config.Weather == 'cd' then
        data = { blackout = true }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.blackOut = true
    end
    Wait(math.random(30000, 60000))
    if Config.Water == 'calm' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/calmwater.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    elseif Config.Water == 'wavy' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/wavywater.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    end
end)

-------------------- Tsunami Showcase  --------------------

-- Tsunami Showcase 
RegisterNetEvent('strez:client:TsunamiShowcase', function(text)
    Wait(math.random(15000, 30000))
    if Config.Showcase == 'qb' then
        TriggerEvent('QBCore:Notify', 'Stage 1/30min Restart: Will get a Weather Alert letting everyone know its a CLEAR day', 'success', 15000)
    elseif Config.Showcase == 'ox' then
        lib.notify({ description = 'Stage 1/30min Restart: Will get a Weather Alert letting everyone know its a CLEAR day', duration = 15000,type = 'info', position = 'top-right'})
    end
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:setWeather', 'CLEAR')
    elseif Config.Weather == 'cd' then
        data = { weather = "CLEAR", instantweather = false }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.weather = { weather = "CLEAR" }
    end
    Wait(math.random(15000, 30000))
    if Config.Showcase == 'qb' then
        TriggerEvent('QBCore:Notify', 'Stage 2/15min Restart: Will get a Weather Alert letting everyone know the weather took a turn and is now RAINING', 'success', 15000)
    elseif Config.Showcase == 'ox' then
        lib.notify({ description = 'Stage 2/15min Restart: Will get a Weather Alert letting everyone know the weather took a turn and is now RAINING', duration = 15000,type = 'info', position = 'top-right'})
    end
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:setWeather', 'RAIN')
    elseif Config.Weather == 'cd' then
        data = { weather = "RAIN", instantweather = false }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.weather = { weather = "RAIN" }
    end
    TriggerEvent('InteractSound_CL:PlayOnOne', 'Alert', 0.2)
    Wait(math.random(15000, 30000))
    if Config.Showcase == 'qb' then
        TriggerEvent('QBCore:Notify', 'Stage 3/5min Restart: Will get a Weather Alert letting everyone know the weather took a turn and is now THUNDER', 'success', 15000)
    elseif Config.Showcase == 'ox' then
        lib.notify({ description = 'Stage 3/5min Restart: Will get a Weather Alert letting everyone know the weather took a turn and is now THUNDER', duration = 15000,type = 'info', position = 'top-right'})
    end
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:setWeather', 'THUNDER')
    elseif Config.Weather == 'cd' then
        data = { weather = "THUNDER", instantweather = false }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.weather = { weather = "THUNDER" }
    end
    Wait(math.random(15000, 30000))
    if Config.Showcase == 'qb' then
        TriggerEvent('QBCore:Notify', 'Stage 3/5min Restart: Will play a global warning sound to air siren all players Tsunami is coming get to high ground (water rising)', 'success', 15000)
    elseif Config.Showcase == 'ox' then
        lib.notify({ description = 'Stage 3/5min Restart: Will play a global warning sound to air siren all players Tsunami is coming get to high ground (water rising)', duration = 15000,type = 'info', position = 'top-right'})
    end

    TriggerEvent('InteractSound_CL:PlayOnOne', 'AirSiren', 0.1)
    TriggerEvent('strez:raisewater')
    if Config.Water == 'calm' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/calmflood.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    elseif Config.Water == 'wavy' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/wavyflood.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    end
    if Config.Showcase == 'qb' then
        TriggerEvent('QBCore:Notify', 'Stage 4/1min Restart: Will get a final email warning & air siren alert to warn people get to high ground for safety & BLACKOUT to the city', 'success', 15000)
    elseif Config.Showcase == 'ox' then
        lib.notify({ description = 'Stage 4/1min Restart: Will get a final email warning & air siren alert to warn people get to high ground for safety & BLACKOUT to the city', duration = 15000,type = 'info', position = 'top-right'})
    end
    Wait(math.random(15000, 30000))
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:toggleBlackout')
    elseif Config.Weather == 'cd' then
        data = { blackout = true }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.blackOut = true
    end
    Wait(math.random(30000, 60000))
    if Config.Water == 'calm' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/calmwater.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    elseif Config.Water == 'wavy' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/wavywater.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    end
end)

-------------------- TxAdmin Auto Restart Tsunami --------------------

-- 30 Minutes Reminder
RegisterNetEvent('strez:client:SendMailTsunamiAuto30', function(text)
    Wait(math.random(5000, 15000))
    local message = (Config.Messages.ThirtyMin)
    if Config.Notify == 'qb' then
        TriggerEvent('QBCore:Notify', message, 'success', 15000)
    elseif Config.Notify == 'ox' then
        lib.notify({ description = message, duration = 15000,type = 'info', position = 'top-right'})
    elseif Config.Notify == 'email' then
        if Config.Phone == 'qb' then
            TriggerServerEvent('qb-phone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Weather Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'qs' then
            TriggerServerEvent('qs-smartphone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Weather Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'gks' then
            TriggerServerEvent('gksphone:NewMail', {
            sender = "City of Los Santos",
            subject = "Weather Broadcast",
            message = message,
            button = {}
            })
        elseif Config.Phone == 'lb' then
            TriggerServerEvent("strez:server:lb-phone:sendMail", {
            sender = "City of Los Santos", 
            subject = "Weather Broadcast",
            message = message, 
            })
        elseif Config.Phone == 'ys' then
            local insertId, received = exports.yseries:SendMail({
            sender = 'City Of Los Santos',
            title = "Weather Broadcast",
            content = message,
            }, 'all')
        end
    end
    Wait(5000)
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:setWeather', 'CLEAR')
    elseif Config.Weather == 'cd' then
        data = { weather = "CLEAR", instantweather = false }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.weather = { weather = "CLEAR" }
    end
end)

-- 15 Minutes Reminder
RegisterNetEvent('strez:client:SendMailTsunamiAuto15', function(text)
    Wait(math.random(5000, 15000))
    TriggerEvent('InteractSound_CL:PlayOnOne', 'Alert', 0.2)
    local message = (Config.Messages.FifteenMin)
    if Config.Notify == 'qb' then
        TriggerEvent('QBCore:Notify', message, 'success', 15000)
    elseif Config.Notify == 'ox' then
        lib.notify({ description = message, duration = 15000,type = 'info', position = 'top-right'})
    elseif Config.Notify == 'email' then
        if Config.Phone == 'qb' then
            TriggerServerEvent('qb-phone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Weather Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'qs' then
            TriggerServerEvent('qs-smartphone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Weather Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'gks' then
            TriggerServerEvent('gksphone:NewMail', {
            sender = "City of Los Santos",
            subject = "Weather Broadcast",
            message = message,
            button = {}
            })
        elseif Config.Phone == 'lb' then
            TriggerServerEvent("strez:server:lb-phone:sendMail", {
            sender = "City of Los Santos", 
            subject = "Weather Broadcast",
            message = message, 
            })
        elseif Config.Phone == 'ys' then
            local insertId, received = exports.yseries:SendMail({
            sender = 'City Of Los Santos',
            title = "Weather Broadcast",
            content = message,
            }, 'all')
        end
    end
    Wait(5000)
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:setWeather', 'RAIN')
    elseif Config.Weather == 'cd' then
        data = { weather = "RAIN", instantweather = false }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.weather = { weather = "RAIN" }
    end
end)

-- 5 Minutes Reminder
RegisterNetEvent('strez:client:SendMailTsunamiAuto5', function(text)
    local message = (Config.Messages.FiveMin)
    TriggerEvent('InteractSound_CL:PlayOnOne', 'AirSiren', 0.1)
    Wait(math.random(5000, 15000))
    if Config.Notify == 'qb' then
        TriggerEvent('QBCore:Notify', message, 'success', 15000)
    elseif Config.Notify == 'ox' then
        lib.notify({ description = message, duration = 15000,type = 'info', position = 'top-right'})
    elseif Config.Notify == 'email' then
        if Config.Phone == 'qb' then
            TriggerServerEvent('qb-phone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Emergency Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'qs' then
            TriggerServerEvent('qs-smartphone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Emergency Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'gks' then
            TriggerServerEvent('gksphone:NewMail', {
            sender = "City of Los Santos",
            subject = "Emergency Broadcast",
            message = message,
            button = {}
            })
        elseif Config.Phone == 'lb' then
            TriggerServerEvent("strez:server:lb-phone:sendMail", {
            sender = "City of Los Santos", 
            subject = "Emergency Broadcast",
            message = message, 
            })
        elseif Config.Phone == 'ys' then
            local insertId, received = exports.yseries:SendMail({
            sender = 'City Of Los Santos',
            title = "Emergency Broadcast",
            content = message,
            }, 'all')
        end
    end
    Wait(math.random(15000, 30000))
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:setWeather', 'THUNDER')
    elseif Config.Weather == 'cd' then
        data = { weather = "THUNDER", instantweather = false }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.weather = { weather = "THUNDER" }
    end
    Wait(math.random(30000, 60000))
    TriggerEvent('strez:raisewater')
    if Config.Water == 'calm' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/calmflood.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    elseif Config.Water == 'wavy' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/wavyflood.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    end
    Wait(math.random(15000, 30000))
    if Config.Weather == 'qb' then
        TriggerServerEvent('qb-weathersync:server:toggleBlackout')
    elseif Config.Weather == 'cd' then
        data = { blackout = true }
        TriggerEvent('cd_easytime:ForceUpdate', data)
    elseif Config.Weather == 'renewed' then
        GlobalState.blackOut = true
    end
    Wait(math.random(30000, 60000))
    if Config.Water == 'calm' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/calmwater.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    elseif Config.Water == 'wavy' then
        local success = LoadWaterFromPath(GetCurrentResourceName(), 'water/wavywater.xml')
        local waterQuadCount = GetWaterQuadCount()
        local calmingQuadCount = GetCalmingQuadCount()
        local waveQuadCount = GetWaveQuadCount()
    end
end)

-- 1 Minutes Reminder
RegisterNetEvent('strez:client:SendMailTsunamiAuto1', function(text)
    local message = (Config.Messages.OneMin)
    Wait(math.random(5000, 15000))
    if Config.Notify == 'qb' then
        TriggerEvent('QBCore:Notify', message, 'success', 15000)
    elseif Config.Notify == 'ox' then
        lib.notify({ description =  message, duration = 15000,type = 'info', position = 'top-right'})
    elseif Config.Notify == 'email' then
        if Config.Phone == 'qb' then
            TriggerServerEvent('qb-phone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Emergency Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'qs' then
            TriggerServerEvent('qs-smartphone:server:sendNewMail', {
            sender = 'City of Los Santos',
            subject = 'Emergency Broadcast',
            message =  message,
            button = {}
            })
        elseif Config.Phone == 'gks' then
            TriggerServerEvent('gksphone:NewMail', {
            sender = "City of Los Santos",
            subject = "Emergency Broadcast",
            message = message,
            button = {}
            })
        elseif Config.Phone == 'lb' then
            TriggerServerEvent("strez:server:lb-phone:sendMail", {
            sender = "City of Los Santos", 
            subject = "Emergency Broadcast",
            message = message, 
            })
        elseif Config.Phone == 'ys' then
            local insertId, received = exports.yseries:SendMail({
            sender = 'City Of Los Santos',
            title = "Emergency Broadcast",
            content = message,
            }, 'all')
        end
    end
end)

-------------------- Water Raising --------------------

RegisterNetEvent('strez:raisewater', function()
    CreateThread(function()
        
        local waterQuadCount = GetWaterQuadCount()
        local isFlooding = true

        local maxFloodHeight = 28 -- Change this value to set the maximum flood height

        local increaseRate = 0.5 -- Change this value to increase/decrease the rate at which the water height changes

        local threadWait = 1000 -- Change this value to increase/decrease the time it takes to reach maxFloodHeight.

        while isFlooding do
            for i = 1, waterQuadCount, 1 do
                local success, waterQuadLevel = GetWaterQuadLevel(i)
                if success == 1 then
                    SetWaterQuadLevel(i, waterQuadLevel + increaseRate)
                end

                if waterQuadLevel >= maxFloodHeight then
                    isFlooding = false
                end
            end
            Wait(threadWait)
        end

        local pCoords, wCoords, allPeds = nil, nil, nil
        local ped = PlayerPedId()

        while true do
            pCoords = GetEntityCoords(ped)
            wCoords = GetWaterQuadAtCoords_3d(pCoords.x, pCoords.y, pCoords.z)

            if wCoords ~= -1 then
                allPeds = GetGamePool('CPed')
                for i = 1, #allPeds do
                    SetPedConfigFlag(allPeds[i], 65, true)
                    SetPedDiesInWater(allPeds[i], true)
                end
            end
            Wait(5000)
        end
    end)
end)
