#include "../../../config/constants.lua"

ui_timer = {}

local LEFT_TIME_SHOW = 15
local REGISTRY_TIME_LEFT = "level.no_hit_time_left"
local REGISTRY_LAST
local LOUD_SND, SOFT_SND
local LAST_SND_HANDLE, SND_PROGRESS, SND_PLAYING
timerScale = 1
timeToPuls = 0.5

local player_veh
function ui_timer:Init()
    player_veh = GetPlayerVehicle()
    if isElimination then
        REGISTRY_TIME_LEFT = "level.elimination_time_left"
        REGISTRY_LAST = "level.eliminate"
        LEFT_TIME_SHOW = 10
        LOUD_SND = LoadSound("MOD/snd/beeps-loud.ogg")
        SOFT_SND = LoadSound("MOD/snd/beeps-soft.ogg")
    end
end

function ui_timer:UnloadSounds()
    UnloadSound(LOUD_SND)
    UnloadSound(SOFT_SND)
    return SND_PLAYING
end

function ui_timer:Draw(dt)
    local time = GetFloat(REGISTRY_TIME_LEFT)
    if time == 0 then return end

    UiPush()
        local time_str = ("%2.1f"):format(time > 0 and time or 0)
        local time_str_width = 84
        local to_borders = 24
        local spaces = "     "
        local to_notify = time <= LEFT_TIME_SHOW
        local alarm = to_notify and GetInt(REGISTRY_LAST) == player_veh
        if alarm then
            if timerScale == 1 then
                SetValue("timerScale", 1.25, "easein", timeToPuls)
            elseif timerScale == 1.25 then
                SetValue("timerScale", 1, "easeout", timeToPuls)
            end
        else
            if timerScale > 1 then
                SetValue("timerScale", 1, "easeout", timeToPuls)
            else
                timerScale = 1
            end
        end
        local _, h = 517, 72

        UiTranslate(UiCenter(), 24)
        UiAlign("top center")
        UiScale(timerScale)
        UiPush()
            UiFont("bold.ttf", 40)
            UiTextUniformHeight(true)
            local w, _ = UiGetTextSize(GetTranslatedStringByKey("NEXT_ELIMINATION") .. spaces)
            w = w + time_str_width + to_borders * 2
            UiWindow(w, h)
            UiAlign("left top")
            PAGE_COMPONENTS:drawRoundedColorRect(w, h, 12, 0, 0, 0, 0.5)
            UiTranslate(24, UiMiddle())
            UiAlign("middle left")
            local snd
            if alarm then
                SetColorFromHEX("FF8064")
                snd = LOUD_SND
            elseif to_notify then
                snd = SOFT_SND
            end
            if snd then
                SND_PROGRESS = GetSoundProgress(LAST_SND_HANDLE or snd)
                local is_different = LAST_SND_HANDLE ~= snd
                if is_different or not IsSoundPlaying(SND_PLAYING) then
                    if is_different and SND_PLAYING then StopSound(SND_PLAYING) end
                    if LAST_SND_HANDLE ~= nil then SetSoundProgress(snd, SND_PROGRESS) end
                    SND_PLAYING = PlaySound(snd)
                end
                LAST_SND_HANDLE = snd
            else
                if SND_PLAYING and IsSoundPlaying(SND_PLAYING) then StopSound(SND_PLAYING) end
                LAST_SND_HANDLE, SND_PROGRESS, SND_PLAYING = nil, nil, nil
            end
            UiText(GetTranslatedStringByKey("NEXT_ELIMINATION") .. spaces)

            UiTranslate(UiWidth() - to_borders * 2, 0)
            UiAlign("right middle")
            UiWindow(time_str_width, h)
            UiTranslate(0, UiMiddle())
            UiAlign("left middle")
            UiFont(DIGIT_FONT_BOLD, 40)
            UiText(time_str)
        UiPop()
    UiPop()
end
