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(1,1,1)
local off=rgb(0,0,0)
local Gear=0
local speed=0
local rpm=0
local lap=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 lapfuel=0
local fuelused=0
local exhausttemp=0
local lastlaptime=0
local predtime=0
local fuellevel=0
local brakebias=50
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
    

    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
    lapfuel=car.fuelPerLap
    fuelused=car.fuel
    fuelmap=car.fuelMap
    limit=car.speedLimiter
    rpm=car.rpm
    speed=car.speedKmh

    -- ac.debug('physics', acCarState.physicsAvailable)

    tyrepresslf=car.wheels[0].tyrePressure -- tyrepresslf=car.wheels[0].tyrePressure*0.06894757
    tyrepressrf=car.wheels[1].tyrePressure
    tyrepresslr=car.wheels[2].tyrePressure
    tyrepressrr=car.wheels[3].tyrePressure

    ui.pushDWriteFont('CPMono_v07 Plain:../fonts/CPMono_v07_Plain.ttf; Weight=bold; Stretch=Expanded')
    -- ui.pushDWriteFont('Shentox Regular 14:../fonts/shentox.ttf; Weight=bold; Stretch=Expanded')

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




function update(dt)
    common()

    ui.setCursor(vec2(410,261))
    ui.dwriteTextAligned(Gear, 167, ui.Alignment.Center, ui.Alignment.Center, vec2(204,160), false, green)

    ui.setCursor(vec2(359,421))
    ui.dwriteTextAligned(math.floor(speed), 147, ui.Alignment.Center, ui.Alignment.Center, vec2(306,144), false, white)

    ui.setCursor(vec2(359,540))
    ui.dwriteTextAligned(math.floor(rpm), 70, ui.Alignment.Center, ui.Alignment.Center, vec2(306,144), false, white)

    ui.setCursor(vec2(736,370))
    
    ui.dwriteTextAligned(string.format('%02.2f', perf), 50, ui.Alignment.Center, ui.Alignment.Center, vec2(272,70), false, perfcolor)

    ui.setCursor(vec2(736,487))
    ui.dwriteTextAligned(string.format('%02.0f:%02.0f.%01.0f', math.floor(lastlaptime/60000)%60,math.floor(lastlaptime/1000)%60,math.floor(lastlaptime/10+0.5)%100), 50, ui.Alignment.Center, ui.Alignment.Center, vec2(272,70), false, white)

    ui.setCursor(vec2(736,603))
    ui.dwriteTextAligned(string.format('%02.0f:%02.0f.%01.0f', math.floor(bestlaptime/60000)%60,math.floor(lastlaptime/1000)%60,math.floor(lastlaptime/10+0.5)%100), 50, ui.Alignment.Center, ui.Alignment.Center, vec2(272,70), false, white)

    ui.setCursor(vec2(616,250))
    ui.dwriteTextAligned('AIR', 46, ui.Alignment.Center, ui.Alignment.Center, vec2(95,70), false, black)

    ui.setCursor(vec2(717,250))
    ui.dwriteTextAligned(fuelmap, 50, ui.Alignment.Center, ui.Alignment.Center, vec2(95,70), false, white)

    ui.setCursor(vec2(818,250))
    ui.dwriteTextAligned(tc, 50, ui.Alignment.Center, ui.Alignment.Center, vec2(95,70), false, white)

    ui.setCursor(vec2(919,250))
    ui.dwriteTextAligned(abs, 50, ui.Alignment.Center, ui.Alignment.Center, vec2(95,70), false, white)

    ui.setCursor(vec2(396,729))
    ui.dwriteTextAligned(compound, 50, ui.Alignment.Center, ui.Alignment.Center, vec2(247,71), false, white)

    ui.setCursor(vec2(652,729))
    ui.dwriteTextAligned(string.format('%.1f', brakebias*100), 50, ui.Alignment.Center, ui.Alignment.Center, vec2(147,71), false, white)

    ui.setCursor(vec2(808,729))
    ui.dwriteTextAligned(lap, 50, ui.Alignment.Center, ui.Alignment.Center, vec2(96,71), false, white)

    ui.setCursor(vec2(913,729))
    ui.dwriteTextAligned(limit, 50, ui.Alignment.Center, ui.Alignment.Center, vec2(96,71), false, white)

    ui.setCursor(vec2(9,730))
    ui.dwriteTextAligned(string.format('%.0f', watertemp), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(96,64), false, white)
   
    ui.setCursor(vec2(114,730))
    ui.dwriteTextAligned(string.format('%.0f', oiltemp), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(96,64), false, white)

    ui.setCursor(vec2(218,730))
    ui.dwriteTextAligned(string.format('%.0f', geartemp), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(96,64), false, white)

    ui.setCursor(vec2(9,513))
    ui.dwriteTextAligned(string.format('%.02f', lapfuel), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(222,64), false, white)

    ui.setCursor(vec2(9,621))
    ui.dwriteTextAligned(string.format('%.01f', fuelused), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(222,64), false, white)

    ui.setCursor(vec2(9,258))
    ui.dwriteTextAligned(string.format('%.1f',tyrepresslf), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(155,105), false, green)
    ui.setCursor(vec2(9,362))
    ui.dwriteTextAligned(string.format('%.1f',tyrepresslr), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(155,105), false, green)
    ui.setCursor(vec2(164,258))
    ui.dwriteTextAligned(string.format('%.1f',tyrepressrf), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(155,105), false, green)
    ui.setCursor(vec2(164,362))
    ui.dwriteTextAligned(string.format('%.1f',tyrepressrr), 46, ui.Alignment.Center, ui.Alignment.Center, vec2(155,105), false, green)
   
end