local sharedNitro = ac.connect({
  ac.StructItem.key('btm_nitro_audio_v1'),
  nitroPressCounter = ac.StructItem.int32(),
  nitroActive       = ac.StructItem.int32(),
  nitroTimeLeft     = ac.StructItem.double(),
  nitroDuration     = ac.StructItem.double(),
  nitroUsesLeft     = ac.StructItem.int32(),
}, true, ac.SharedNamespace.CarScript)

local displaySize = vec2(700, 365)
local warnFlashHz = 6.0
local warnEmptyDuration = 5.0
local warnText1 = "Warning!!!"
local warnText2 = "Danger to Manifold"
local warnText1Size = 69
local warnText2Size = 69
local warnText1Pos = vec2(430, 180)
local warnText2Pos = vec2(358, 250)
local warnRed    = rgbm(1.0, 0.0, 0.0, 1.0)
local warnYellow = rgbm(1.0, 1.0, 0.0, 1.0)
local warnText   = rgbm(0.0, 0.0, 0.0, 1.0)
local warnShadow = rgbm(0.0, 0.0, 0.0, 0.55)
local TIME_EPS = 0.010

local extraA_last = false
local function extraA_pressed()
  local now = (car and car.extraA) == true
  local pressed = (now == true) and (extraA_last == false)
  extraA_last = now
  return pressed
end

local warningTimeLeft = 0.0
local warningBurstMode = false
local flashT = 0.0

local function startWarningSeconds(sec)
  warningBurstMode = false
  local s = sec or 0.0
  if s < 0.0 then s = 0.0 end
  if s > warningTimeLeft then warningTimeLeft = s end
end

local function startWarningBurst(secLeft)
  warningBurstMode = true
  local s = secLeft or 0.0
  if s < 0.0 then s = 0.0 end
  if s > warningTimeLeft then warningTimeLeft = s end
end

local function stopWarning()
  warningTimeLeft = 0.0
  warningBurstMode = false
end

local prevActive = false
local lockedThisBurst = false

local function fontForSize(px)
  if px >= 70 then return ui.Font.Huge end
  if px >= 52 then return ui.Font.Title end
  if px >= 38 then return ui.Font.Main end
  if px >= 26 then return ui.Font.Small end
  return ui.Font.Tiny
end

local function drawTextWithShadow(text, pos, pxSize, color)
  ui.pushFont(fontForSize(pxSize))
  ui.drawText(text, pos + vec2(2, 2), warnShadow)
  ui.drawText(text, pos, color)
  ui.popFont()
end

local function drawWarningText()
  drawTextWithShadow(warnText1, warnText1Pos, warnText1Size, warnText)
  drawTextWithShadow(warnText2, warnText2Pos, warnText2Size, warnText)
end

local function drawFlash()
  local phase = math.floor(flashT * warnFlashHz) % 2
  local bg = (phase == 0) and warnRed or warnYellow
  ui.drawRectFilled(vec2(330, 169), displaySize, bg)
end

function script.update(dt)
  ui.drawRectFilled(vec2(0, 0), displaySize, rgbm(0, 0, 0, 0))

  local usesLeft = tonumber(sharedNitro.nitroUsesLeft) or 0
  local active   = (tonumber(sharedNitro.nitroActive) or 0) ~= 0
  local timeLeft = tonumber(sharedNitro.nitroTimeLeft) or 0.0

  local pressed = extraA_pressed()

  flashT = flashT + dt
  if warningTimeLeft > 0.0 then
    warningTimeLeft = warningTimeLeft - dt
    if warningTimeLeft < 0.0 then warningTimeLeft = 0.0 end
  end

  if warningBurstMode and (not active) then
    stopWarning()
  end

  if active and (not prevActive) then
    lockedThisBurst = false
  end

  if pressed and active and prevActive and (not lockedThisBurst) then
    lockedThisBurst = true
    startWarningBurst(math.max(timeLeft, TIME_EPS))
  end

  if pressed and (not active) and (usesLeft == 0) then
    startWarningSeconds(warnEmptyDuration)
  end

  prevActive = active

  if warningTimeLeft <= 0.0 then
    return
  end

  drawFlash()
  drawWarningText()
end