ui.setAsynchronousImagesLoading(true)

local rpmframe
local rpmmaxframe = 300
local rpmvideosize = vec2(2000, 1000)*1
local rpmposition = vec2(0,00)
local carRPM = 0

local blinkerstate = 0
local leftstate = 0
local rightstate = 0
local hazardstate = 0

local indicator_1 = ui.MediaPlayer() 
local indicator_2 = ui.MediaPlayer()
indicator_1:setSource("/sounds/on.mp3")
indicator_2:setSource("/sounds/indicator.mp3")

function update(dt)
	ui.setAsynchronousImagesLoading(false)

    local time = string.format("%02d:%02d",sim.timeHours,sim.timeMinutes)

    -- RPM
    carRPM = math.applyLag(carRPM, car.rpm, 0.8, dt)

    rpmframe = math.min(rpmmaxframe, (rpmmaxframe/9000)*carRPM)

    if carRPM <= 2000 then
        rpmframe = (35/2000)*carRPM
    elseif carRPM <= 3000 then
        rpmframe = ((65-35)/(3000-2000))*carRPM+(35-(((65-35)/(3000-2000))*2000))
    elseif carRPM <= 4000 then
        rpmframe = ((90-65)/(4000-3000))*carRPM+(65-(((90-65)/(4000-3000))*3000))
    elseif carRPM <= 5000 then
        rpmframe = ((120-90)/(5000-4000))*carRPM+(90-(((120-90)/(5000-4000))*4000))
    elseif carRPM <= 6000 then
        rpmframe = ((150-120)/(6000-5000))*carRPM+(120-(((150-120)/(6000-5000))*5000))
    elseif carRPM <= 7000 then
        rpmframe = ((180-150)/(7000-6000))*carRPM+(150-(((180-150)/(7000-6000))*6000))
    elseif carRPM <= 8000 then
        rpmframe = ((260-180)/(8000-7000))*carRPM+(180-(((260-180)/(8000-7000))*7000))
    else
        rpmframe = ((300-260)/(9000-8000))*carRPM+(260-(((300-260)/(9000-8000))*8000))
    end

    ui.drawImage("./assets/cluster/cluster" .. math.floor(rpmframe) .. ".jpg",
        rpmposition,
        vec2(rpmposition.x+rpmvideosize.x, rpmposition.y+rpmvideosize.y)
    )

    -- SPEED
    display.text {
        text = math.round(car.speedKmh * 0.6213711922, 0),
        pos = vec2(448, 669),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*1.3,
        font = "/fonts/arial_hd",
        width = 200,
        spacing = -10,
        alignment = 0.5
    }

    -- GEAR
    display.text {
        text = car.gear == -1 and "R" or car.gear == 0 and "N" or car.gear,
        pos = vec2(892,667),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*1.7,
        font = "/fonts/arial_hd",
        width = 200,
        alignment = 0.5
    }

    -- TYRE PRESSURE
    display.text { text = math.round(car.wheels[0].tyrePressure,0), pos = vec2(1313, 649.1), color = rgbm(1,1,1,1), letter = vec2(50,200)*0.6, font="/fonts/arial_hd", width=200, alignment=1 }
    display.text { text = math.round(car.wheels[1].tyrePressure,0), pos = vec2(1647, 649.1), color = rgbm(1,1,1,1), letter = vec2(50,200)*0.6, font="/fonts/arial_hd", width=200, alignment=0 }
    display.text { text = math.round(car.wheels[2].tyrePressure,0), pos = vec2(1313, 755.1), color = rgbm(1,1,1,1), letter = vec2(50,200)*0.6, font="/fonts/arial_hd", width=200, alignment=1 }
    display.text { text = math.round(car.wheels[3].tyrePressure,0), pos = vec2(1647, 755.1), color = rgbm(1,1,1,1), letter = vec2(50,200)*0.6, font="/fonts/arial_hd", width=200, alignment=0 }

    -- TYRE TEMP
    display.text { text = math.round((car.wheels[0].tyreCoreTemperature * 9/5)+32,0), pos = vec2(1313, 689.1), color = rgbm(1,1,1,1), letter = vec2(50,200)*0.6, font="/fonts/arial_hd", width=200, alignment=1 }
    display.text { text = math.round((car.wheels[1].tyreCoreTemperature * 9/5)+32,0), pos = vec2(1647, 689.1), color = rgbm(1,1,1,1), letter = vec2(50,200)*0.6, font="/fonts/arial_hd", width=200, alignment=0 }
    display.text { text = math.round((car.wheels[2].tyreCoreTemperature * 9/5)+32,0), pos = vec2(1313, 800.1), color = rgbm(1,1,1,1), letter = vec2(50,200)*0.6, font="/fonts/arial_hd", width=200, alignment=1 }
    display.text { text = math.round((car.wheels[3].tyreCoreTemperature * 9/5)+32,0), pos = vec2(1647, 800.1), color = rgbm(1,1,1,1), letter = vec2(50,200)*0.6, font="/fonts/arial_hd", width=200, alignment=0 }

    -- TIME
    display.text {
        text = time,
        pos = vec2(70,245),
        color = rgbm(1,1,1,1),
        letter = vec2(0,0),
        font = "bmw",
        width = 100,
        alignment = 1
    }

    -- HEADLIGHTS
    if not car.lowBeams and car.headlightsActive then
        local pos = vec2(200,910)
        ui.drawImage("/assets/icons/headlight.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(0.0,0.3,1,1))
    elseif car.lowBeams and car.headlightsActive then
        local pos = vec2(200,910)
      ui.drawImage("/assets/icons/low_beam_headlight.png", pos,vec2(pos.x+50, pos.y+50)  , rgbm(0.1,1,0.1,1))
    end

    -- TC
       if not lasttrueTC then
        lasttrueTC = 0
    end
    if car.tractionControlInAction then
        setInterval(function() tractionControlInAction = not tractionControlInAction end, 0.15, "tractionControlInAction")
        lasttrueTC = os.clock()
	elseif (os.clock() - lasttrueTC > 0.5) then 
        clearInterval("tractionControlInAction")
		tractionControlInAction = false
        
    end
    if car.tractionControlMode == 0 or tractionControlInAction then
        local pos = vec2(300,915)
        ui.drawImage("/assets/icons/traction_controle.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(1,0.5,0,1))
    end
 	

 	
    if car.turningLeftLights or car.turningRightLights then
		setInterval(function() indicatorActive = not indicatorActive end, 0.334, "indicatorActive")
	else
		clearInterval("indicatorActive")
		indicatorActive = false
	end

    -- INDICATORS VISUAL
	if car.turningLeftOnly and indicatorActive then 
		local pos = vec2(720,925)
		ui.beginRotation()
        ui.drawImage("/assets/icons/turn_signal.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(0.1,1,0.1,1))
		ui.endRotation(-90)
    elseif car.turningRightOnly and indicatorActive then 
		local pos = vec2(1220,925)
        ui.drawImage("/assets/icons/turn_signal.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(0.1,1,0.1,1))
    elseif car.hazardLights and indicatorActive then
		local pos1 = vec2(1220,925)
        ui.drawImage("/assets/icons/turn_signal.png", pos1,vec2(pos1.x+50, pos1.y+50) , rgbm(0.1,1,0.1,1))
		local pos2 = vec2(720,925)
		ui.beginRotation()
        ui.drawImage("/assets/icons/turn_signal.png", pos2,vec2(pos2.x+50, pos2.y+50) , rgbm(0.1,1,0.1,1))
		ui.endRotation(-90)
	end

local size = vec2(387.75, 80.625)

-- positions
local posA = vec2(1410, 910)   -- your custom position for EXTRA A
local posB = vec2(387.75, 910)   -- keep original for EXTRA B



-- EXTRA A (only if B is NOT active)
if car.extraA then
  ui.drawImage(
    "/assets/icons/active_aero.png",
    posA,
    vec2(posA.x + size.x, posA.y + size.y),
    rgbm(1, 1, 1, 1)
  )
end


    -- SOUND
    if ac.isInteriorView() then
        indicator_1:setVolume(ac.getAudioVolume("main"))
        indicator_2:setVolume(ac.getAudioVolume("main"))
    else
        indicator_1:setVolume(0)
        indicator_2:setVolume(0)
    end

    if car.turningLeftOnly or car.turningRightOnly or car.hazardLights then
        indicator_2:play()
    else
        indicator_2:pause()
    end
end