local Timer=0
local perf=0
local fontcolor=rgbm(1,1,1,1)
local white=rgbm(1,1,1,1)
local black=rgbm(0,0,0,1)
local green=rgbm(0,.8,0,1)
local red=rgbm(0.84,0.08,0.13,1)
local cyan=rgbm(0.0,0.60,0.70,1) 
local blue=rgbm(0,0.44,1,1)
local purple=rgbm(0.6,0.58,0.92,1)
local on=rgb(2,2,2)
local off=rgb(1.0,1.0,1.0)
local Gear=0
local speed=0
local rpm=0
local lap=0
local laps=0
local abs=0
local tc=0
local oiltemp=0
local fuelmap=0
local limit=80
local oilpress=0
local tyrepresslf=0
local tyrepressrf=0
local tyrepresslr=0
local tyrepressrr=0
local watertemp=0
local geartemp=0
local difftemp=0
local lapfuel=0
local fuelused=0
local exhausttemp=0
local lastlaptime=0
local laptimedisplay=0
local laptime=0
local predtime=0
local fuellevel=0
local timehours=0
local timeminutes=0
local brakebias=50
local volts=12
local DSC=ON
local perfcolor=white
local lastbrakebias=50
local compound='DRY'
local POPTimer = 10
local POPBrakebias = false
local POPABS = false
local POPTC = false
local i = false
local calculatedwaterPressure
local lastABS = 0
local lastTC = 0
local icons = ui.atlasIcons('icons.png', 4, 4, {
    LowBeams = {1, 1},
    HighBeams = {1, 2},
    Blinker_L = {1, 3},
    Blinker_R = {1, 4},
    WipersSpd1 = {2, 1},
    WipersSpd2 = {2, 2},
    WipersSpd3 = {2, 3},
    WipersSpd4 = {2, 4},
    WipersOff = {3, 1},
    ExtraA = {3, 2},
    ExtraB = {3, 3},
    ExtraC = {3, 4},
    ExtraD = {4, 1},
    ExtraE = {4, 2},
    ExtraF = {4, 3},
    ExtraG = {4, 4},
  })

  local backlite = ac.findNodes("carRoot:" .. car.index):findMeshes("DISPLAY"):ensureUniqueMaterials()

function common(dt)

    

    Gear = car.gear
    if Gear==-1 then
        Gear='R'
    end
    if Gear==0 then
        Gear='N'
    end
    
    if car.compoundIndex==0 then
        compound='DRY'
    else
        compound='WET'
    end

    if perf<0 then
        perfcolor = green
    elseif perf>0 then
        perfcolor = red
    else
        perfcolor = white
    end
    
    laptime=car.lapTimeMs
    lastlaptime=car.previousLapTimeMs
    bestlaptime=car.bestLapTimeMs
    perf=car.performanceMeter
    abs=car.absMode
    tc=car.tractionControlMode
    brakebias = car.brakeBias
    lap=car.lapCount+1
    watertemp=car.waterTemperature
    oiltemp=car.oilTemperature
    geartemp=(car.oilTemperature*1.1)-10
    difftemp=(car.oilTemperature*1.1)-4
    lapfuel=car.fuelPerLap
    fuelused=car.fuel
    fuelmap=car.fuelMap
    limit=car.speedLimiter
    rpm=car.rpm
    speed=car.speedKmh
    volts=car.batteryVoltage
    fuellevel=car.fuel
    timehours=sim.timeHours
    timeminutes=sim.timeMinutes
    DSC=car.tractionControlMode
    if DSC==0 then DSC='OFF' end
    

    -- ac.debug('physics', acCarState.physicsAvailable)
    
    tyrepresslf=car.wheels[0].tyrePressure*0.06894757
    tyrepressrf=car.wheels[1].tyrePressure*0.06894757
    tyrepresslr=car.wheels[2].tyrePressure*0.06894757
    tyrepressrr=car.wheels[3].tyrePressure*0.06894757

    if car.lapCount>laps then
        Timer=0
    end

    

    if Timer<5 then
        laptimedisplay=string.format('%.0f:%02.0f.%02.0f', math.floor(lastlaptime/60000)%60,math.floor(lastlaptime/1000)%60,math.floor(lastlaptime/10+0.5)%100)
    else
        laptimedisplay=string.format('%.0f:%02.0f.%02.0f', math.floor(laptime/60000)%60,math.floor(laptime/1000)%60,math.floor(laptime/10+0.5)%100)
    end

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


end




function update(dt)
    common()
    Timer = Timer > 1000 and 0 or Timer + dt

    ui.pushDWriteFont('Segoe UI:../fonts/segoeui.ttf; Weight=normal; Stretch=Condensed')
    
    ui.setCursor(vec2(416,445))
    ui.dwriteTextAligned(string.format('%.f',speed), 150, ui.Alignment.Center, ui.Alignment.Center, vec2(284,126), false, white)

    ui.pushDWriteFont('Lato:../fonts/LatoWeb-Bold.ttf; Weight=bold Stretch=Condensed')

    ui.setCursor(vec2(416,235))
    ui.dwriteTextAligned(Gear, 200, ui.Alignment.Center, ui.Alignment.Center, vec2(284,166), false, white)

    ui.pushDWriteFont('Segoe UI:../fonts/segoeui.ttf; Weight=bold; Stretch=Condensed')

    ui.setCursor(vec2(676,245))
    ui.dwriteTextAligned(math.floor(rpm), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(183,55), false, white)

    ui.setCursor(vec2(862,258))
    ui.dwriteTextAligned(abs, 60, ui.Alignment.Center, ui.Alignment.Center, vec2(148,63), false, white)

    ui.setCursor(vec2(862,366))
    ui.dwriteTextAligned(tc, 60, ui.Alignment.Center, ui.Alignment.Center, vec2(148,63), false, white)
    
    ui.setCursor(vec2(702,485))
    ui.dwriteTextAligned(string.format('%.2f', perf), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(308,86), false, perfcolor)

    ui.setCursor(vec2(702,604))
    ui.dwriteTextAligned(laptimedisplay, 88, ui.Alignment.End, ui.Alignment.Center, vec2(300,86), false, white)
    
    ui.setCursor(vec2(14,267))
    ui.dwriteTextAligned(string.format('%.2f',tyrepresslf), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(194,78), false, white)
    ui.setCursor(vec2(14,375))
    ui.dwriteTextAligned(string.format('%.2f',tyrepresslr), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(194,78), false, white)
    ui.setCursor(vec2(210,267))
    ui.dwriteTextAligned(string.format('%.2f',tyrepressrf), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(194,78), false, white)
    ui.setCursor(vec2(210,375))
    ui.dwriteTextAligned(string.format('%.2f',tyrepressrr), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(194,78), false, white)

    ui.setCursor(vec2(14,480))
    ui.dwriteTextAligned(string.format('%.1f', brakebias*100), 88, ui.Alignment.Center, ui.Alignment.Center, vec2(390,103), false, white)
    
    ui.setCursor(vec2(14,624))
    ui.dwriteTextAligned(string.format('%.0f', watertemp), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(124,68), false, white)
       
    ui.setCursor(vec2(138,624))
    ui.dwriteTextAligned(string.format('%.0f', oiltemp), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(124,68), false, white)

    ui.setCursor(vec2(262,624))
    ui.dwriteTextAligned(string.format('%.f', geartemp), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(124,68), false, white)

    ui.setCursor(vec2(385,624))
    ui.dwriteTextAligned(string.format('%.1f', lapfuel), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(124,68), false, white)


    ui.setCursor(vec2(531,624))
    ui.dwriteTextAligned(string.format('%.1f', fuellevel), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(152,68), false, white)
    
    ui.setCursor(vec2(722,745))
    ui.dwriteTextAligned(lap, 60, ui.Alignment.Center, ui.Alignment.Center, vec2(151,57), false, white)

    ui.setCursor(vec2(876,745))
    ui.dwriteTextAligned(string.format('%.1f', volts), 60, ui.Alignment.Center, ui.Alignment.Center, vec2(134,57), false, white)

    laps = car.lapCount
end