-- Colors
local white  = rgb(.7, .9, 1)
local yellow = rgb(.7, .9, 1)
local red    = rgb(1, 0.01, 0)

-- Percentage thresholds:
--   < YELLOW_THRESHOLD_PERCENT   => White
--   < RED_THRESHOLD_PERCENT      => Yellow
--   >= RED_THRESHOLD_PERCENT     => Red (with optional blinking)
local YELLOW_THRESHOLD_PERCENT = 90
local RED_THRESHOLD_PERCENT    = 92

-- Update intervals
local REFRESH      = 0.05 -- Interval (in seconds) to refresh displayed RPM
local BLINK_PERIOD = 0.15  -- Speed of blinking if enabled

-- Toggles
local USE_BLINKING = true -- Set to false if you do NOT want blinking at red zone

-- Track timers and states
local timeSinceLastRefresh = 0
local blinkTimer = 0
local blinkState = false

-- Store last displayed RPM
local lastDisplayedRpm = 0

function update(dt)
    -- Only update the displayed RPM after the specified REFRESH interval
    timeSinceLastRefresh = timeSinceLastRefresh + dt
    if timeSinceLastRefresh >= REFRESH then
        lastDisplayedRpm = math.floor(car.rpm)
        timeSinceLastRefresh = 0
    end
    
    -- Make sure we have a valid rpmLimiter; if zero or nil, fallback to 7500
    local rpmLimiter = car.rpmLimiter or 7500
    if rpmLimiter <= 0 then
        rpmLimiter = 7500
    end

    -- Calculate percentage of rev limiter
    local rpmPercentage = (car.rpm / rpmLimiter) * 100

    -- Default color
    local displayColor = white
    
    if rpmPercentage < YELLOW_THRESHOLD_PERCENT then
        ---------------------------------------------------------
        -- Stage 1: Normal (White)
        ---------------------------------------------------------
        displayColor = white
        
        -- Reset blinking if we drop back down
        blinkTimer = 0
        blinkState = false
        
    elseif rpmPercentage < RED_THRESHOLD_PERCENT then
        ---------------------------------------------------------
        -- Stage 2: Yellow
        ---------------------------------------------------------
        displayColor = yellow
        
        -- Reset blinking if we drop back down
        blinkTimer = 0
        blinkState = false
        
    else
        ---------------------------------------------------------
        -- Stage 3: Red (Blinking or Solid)
        ---------------------------------------------------------
        if USE_BLINKING then
            -- If blinking is enabled, toggle between red and yellow
            blinkTimer = blinkTimer + dt
            if blinkTimer >= BLINK_PERIOD then
                blinkState = not blinkState
                blinkTimer = 0
            end
            
            if blinkState then
                displayColor = red
            else
                displayColor = yellow
            end
        else
            -- If blinking is off, remain solid red
            displayColor = red
        end
    end

    -- Display the RPM (numeric).
    display.text({
        text      = lastDisplayedRpm,
        pos       = vec2(-40, 130),
        letter    = vec2(100, 200),
        font      = 'digital_big',
        color     = displayColor,
        alignment = 0.5,
        width     = 555,
        spacing   = 1
    })
end
