local Timer = 0
local white=rgbm(1,1,1,1)
local laptime=0
local lastlaptime=0
local backlite = ac.findNodes("carRoot:" .. car.index):findMeshes("RACELOGIC_DISPLAY"):ensureUniqueMaterials()
local on=rgb(3,3,3)
local off=rgb(1.5,1.5,1.5)
local laps=0


function common(dt)

    laptime=car.lapTimeMs
    lastlaptime=car.previousLapTimeMs
    
        ui.pushDWriteFont('LImpact:../fonts/impact.ttf; Weight=bold; Stretch=Expanded')
end




function update(dt)
    common()

    Timer = Timer > 1000 and 0 or Timer + dt
    
    ac.debug('laps', laps)
       
    if car.lapCount>laps then
        Timer=0
    end
    if Timer<5 then
        racelogicdisplay=string.format('%.0f:%02.0f.%03.0f', math.floor(lastlaptime/60000)%60,math.floor(lastlaptime/1000)%60,math.floor(lastlaptime/10+0.5)%100)
    else
        racelogicdisplay=string.format('%.0f:%02.0f.%03.0f', math.floor(laptime/60000)%60,math.floor(laptime/1000)%60,math.floor(laptime/10+0.5)%100)
    end
    ac.debug('timer', Timer)


    ui.setCursor(vec2(0,0))
    ui.dwriteTextAligned(racelogicdisplay, 90, ui.Alignment.End, ui.Alignment.Center, vec2(400,128), false, white)

    ui.setCursor(vec2(426,58))
    ui.dwriteTextAligned(car.lapCount+1, 60, ui.Alignment.Center, ui.Alignment.Center, vec2(80,44), false, white)

    if car.headlightsActive==true then
        backlite:setMaterialProperty("ksEmissive",on)
        else
        backlite:setMaterialProperty("ksEmissive",off)
    end
    laps = car.lapCount
end