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.0,0.0,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(3,3,3)
local off=rgb(1.5,1.5,1.5)
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 laptime=0
local laptimedisplay=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 Timer = 0
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)

    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


    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 -- tyrepresslf=car.wheels[0].tyrePressure*0.06894757
    tyrepressrf=car.wheels[1].tyrePressure
    tyrepresslr=car.wheels[2].tyrePressure
    tyrepressrr=car.wheels[3].tyrePressure

    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('CobeExtraBold:../fonts/CobeExtraBold.ttf; Stretch=Condensed')

    ui.setCursor(vec2(383,157))
    ui.dwriteTextAligned(Gear, 320, ui.Alignment.Center, ui.Alignment.Center, vec2(256,370), false, white)

    ui.pushDWriteFont('FederationBold:../fonts/F_BOLD.TTF; Weight=normal; Stretch=Condensed')

    ui.setCursor(vec2(0,157))
    ui.dwriteTextAligned(tc, 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)
    
    ui.setCursor(vec2(192,157))
    ui.dwriteTextAligned(compound, 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)

    ui.setCursor(vec2(0,296))
    ui.dwriteTextAligned(string.format('%.1f',tyrepresslf), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)

    ui.setCursor(vec2(192,296))
    ui.dwriteTextAligned(string.format('%.1f',tyrepressrf), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)


    
    ui.setCursor(vec2(0,430))
    ui.dwriteTextAligned(string.format('%.1f',tyrepresslr), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)

    -- ui.dwriteTextAligned('0.0', 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)

    ui.setCursor(vec2(192,430))
    ui.dwriteTextAligned(string.format('%.1f',tyrepressrr), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)

    


    
    ui.setCursor(vec2(645,157))
    ui.dwriteTextAligned(abs, 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)

    

    ui.setCursor(vec2(836,157))
    ui.dwriteTextAligned(string.format('%.1f', brakebias*100), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)
    -- ui.dwriteTextAligned('OFF', 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)
    
   
    ui.setCursor(vec2(645,296))
    ui.dwriteTextAligned(string.format('%.0f', oiltemp), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)
    

    ui.setCursor(vec2(836,296))
    ui.dwriteTextAligned(string.format('%.0f', watertemp), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)
    
    ui.setCursor(vec2(645,430))
    ui.dwriteTextAligned(string.format('%.0f', geartemp), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)
    
    ui.setCursor(vec2(836,430))
    ui.dwriteTextAligned(string.format('%.f', fuellevel), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,73), false, white)
    

    
    
    ui.setCursor(vec2(10,562))
    ui.dwriteTextAligned(lap, 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,61), false, white)

    ui.setCursor(vec2(384,562))
    ui.dwriteTextAligned(string.format('%.2f', perf), 50, ui.Alignment.End, ui.Alignment.Center, vec2(220,61), false, perfcolor)
    

    ui.setCursor(vec2(810,562))
    


    ui.setCursor(vec2(0,672))
    ui.dwriteTextAligned(math.floor(rpm), 44, ui.Alignment.End, ui.Alignment.Center, vec2(178,61), false, white)
    
    ui.setCursor(vec2(220,668))
    ui.dwriteTextAligned(laptimedisplay, 50, ui.Alignment.End, ui.Alignment.Center, vec2(416,82), false, white) 

    ui.setCursor(vec2(836,672))
    ui.dwriteTextAligned(string.format('%.f',speed), 50, ui.Alignment.End, ui.Alignment.Center, vec2(178,61), false, white)
    
    ui.setCursor(vec2(0,800))
    ui.dwriteTextAligned('GPS: GOOD', 27, ui.Alignment.End, ui.Alignment.Center, vec2(210,60), false, white)

    ui.setCursor(vec2(394,800))
    ui.dwriteTextAligned('Track', 27, ui.Alignment.Center, ui.Alignment.Center, vec2(256,60), false, white)

    if car.isInPitlane or car.manualPitsSpeedLimiterEnabled then

        ui.drawRectFilled(vec2(0, 800), vec2(1024, 860), red, 6, ui.CornerFlags.All)
        ui.setCursor(vec2(0,800))
        ui.dwriteTextAligned('SPEED ASSIST', 50, ui.Alignment.Center, ui.Alignment.Center, vec2(1024,60), false, white)

    
    end


    ui.setCursor(vec2(836,800))
    ui.dwriteTextAligned(string.format('%.f:%02.f',math.floor(timehours)%24,math.floor(timeminutes)%60), 27, ui.Alignment.End, ui.Alignment.Center, vec2(178,60), false, white)
    
    ac.debug('Timer', Timer)
    laps = car.lapCount
end