local perf=0
local Timer=0
local fontcolor=rgbm(1,1,1,1)
local white=rgbm(1,1,1,1)
local yellow=rgbm(1,0.95,0,1)
-- local darkyellow=rgbm(0.9,0.8,0,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(3,3,3)
local off=rgb(1,1,1)
local gear=0
local gearmode='M'
local speed=0
local rpm=0
local lap=0
local laps=0
local abs=0
local esp='OFF'
local tc=0
local oiltemp=0
local oilpress=0
local tyrepresslf=0
local tyrepressrf=0
local tyrepresslr=0
local tyrepressrr=0
local watertemp=0
local laptime=0
local lastlaptime=0
local predtime=0
local fuellevel=0
local brakebias=50
local pitbias=65
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 scale = ui.atlasIconID('scale.png', vec2(0,0), vec2(939, 37))
local brakebiasgrid = ui.atlasIconID('brakebias_grid.png', vec2(0,0), vec2(93, 322))
local backlite = ac.findNodes("carRoot:" .. car.index):findMeshes("DISPLAY"):ensureUniqueMaterials()


function popup(dt)

    if POPBrakebias then
        POPTimer = POPTimer + dt
        
        ui.drawRectFilled(vec2(924, 124), vec2(1017, 446), black, 1, ui.CornerFlags.All)
        local yvalue=286-brakebias*100*3.16
        if brakebias>0 then
            
            ui.drawRectFilled(vec2(926, yvalue), vec2(1015, 286), green, 1, ui.CornerFlags.All)    
        else
            
            ui.drawRectFilled(vec2(926, 286), vec2(1015, yvalue), green, 1, ui.CornerFlags.All)    
        end
        
        ui.setCursor(vec2(924,124))
        ui.image(brakebiasgrid, vec2(93, 322))
        if lastbrakebias < car.brakeBias or i==true then --arrow up
            ui.drawRectFilled(vec2(958,182),vec2(984,215), white)
            ui.drawTriangleFilled(vec2(947,182),vec2(971,150),vec2(995,182))
            i=true
        end
        if lastbrakebias > car.brakeBias or i==false then --arrow down
            ui.drawRectFilled(vec2(958,355),vec2(984,388), white)
            ui.drawTriangleFilled(vec2(947,388),vec2(971,420),vec2(995,388))
            i=false
        end

        ui.drawRectFilled(vec2(802, 367), vec2(900, 401), purple, 1, ui.CornerFlags.All)
        
        lastbrakebias = car.brakeBias
    end

    if POPTimer > 1.5 then
        POPBrakebias = false
        i=false
    end
    if lastbrakebias ~= car.brakeBias then
        POPBrakebias = true
        POPTimer = 0
    end
    
    if POPABS then
        POPTimer = POPTimer + dt
        if POPTimer > 1.5 then
            POPABS = false
        end
        ui.drawRectFilled(vec2(10, 127), vec2(121, 171), blue, 1, ui.CornerFlags.All)
        ui.setCursor(vec2(9,127))
        ui.dwriteTextAligned('ABS', 30, ui.Alignment.Center, ui.Alignment.Center, vec2(73,44), false, white)
    end

    if lastABS ~= car.absMode then
        POPABS = true
        POPTimer = 0
    end

    if POPTC then
        POPTimer = POPTimer + dt
        if POPTimer > 1.5 then
            POPTC = false
        end
        ui.drawRectFilled(vec2(10, 177), vec2(121, 220), red, 1, ui.CornerFlags.All)
        ui.setCursor(vec2(8,176))
        ui.dwriteTextAligned('TC', 30, ui.Alignment.Center, ui.Alignment.Center, vec2(73,44), false, white)
    end

    if lastTC ~= car.tractionControlMode then
        POPTC = true
        POPTimer = 0
    end

end

function common(dt)

    if car.lapCount>laps then
        Timer=0
    end

    if Timer<5 then
        laptimedisplay=string.format('%02.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('%02.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'
        gearmode='R'
    end
    if gear==0 then
        gear='N'
        gearmode='P'
    end
    if car.gear>0 then
        gearmode='M'
    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

    fontcolor=white
    perf=car.performanceMeter


    speed=math.floor(car.speedKmh)
    rpm=math.floor(car.rpm)
    lap=car.lapCount+1
    abs=car.absMode
    tc=car.tractionControlMode
    oiltemp=car.oilTemperature
    oilpress=car.oilPressure
    watertemp=car.waterTemperature
    laptime=car.lapTimeMs
    predtime=car.bestLapTimeMs+perf*1000
    brakebias=car.brakeBias
    fuellevel=car.fuel
    lastlaptime=car.previousLapTimeMs
    tyrepresslf=car.wheels[0].tyrePressure
    tyrepressrf=car.wheels[1].tyrePressure
    tyrepresslr=car.wheels[2].tyrePressure
    tyrepressrr=car.wheels[3].tyrePressure

    if car.tractionControlMode == 0 then
        esp='OFF'
    else
        esp='ON'
    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


calculatedwaterPressure = 1.1+(0.0001 * car.rpm) - (0.001 * car.waterTemperature)

    ui.pushDWriteFont('Verdana:../fonts/verdana.ttf; Weight=bold; Stretch=Expanded')
    ui.setCursor(vec2(0,0))

if car.isInPitlane or car.manualPitsSpeedLimiterEnabled then

    if speed > (ac.getSim().pitsSpeedLimit+1) then
        ui.drawRectFilled(vec2(14, 161), vec2(1005, 281), red)
    else
        ui.drawRectFilled(vec2(14, 161), vec2(1010, 286), blue)
    end

    -- fontcolor=black
    ui.drawRect(vec2(19, 166), vec2(1005, 281), white, 0, 0, 3)

    
    ui.setCursor(vec2(48,161))
    ui.dwriteTextAligned('PITSPEED ACTIVE', 67, ui.Alignment.Start, ui.Alignment.Center, vec2(700,120), false, white)
    ui.pushDWriteFont('Verdana:../fonts/verdanab.ttf; Weight=bold; Stretch=Expanded')

    -- ui.setCursor(vec2(630,161))
    -- ui.dwriteTextAligned(speed, 72, ui.Alignment.End, ui.Alignment.Center, vec2(170,120), false, white)
    ui.setCursor(vec2(810,161))
    ui.dwriteTextAligned(speed, 72, ui.Alignment.Center, ui.Alignment.Center, vec2(170,120), false, white)

    -- ui.setCursor(vec2(726,300))
    -- ui.dwriteTextAligned('Gear', 50, ui.Alignment.Center, ui.Alignment.Center, vec2(202,86), false, black)
    -- ui.setCursor(vec2(726,370))
    -- ui.dwriteTextAligned(gear, 180, ui.Alignment.Center, ui.Alignment.Center, vec2(202,180), false, black)

else
    
    ui.drawRectFilled(vec2(43, 225), vec2(((rpm*0.117)+43), 262), blue)
    ui.setCursor(vec2(42,225))
    ui.image(scale, vec2(926, 37))

    -- ui.setCursor(vec2(728,76))
    -- ui.dwriteTextAligned(lap, 32, ui.Alignment.Center, ui.Alignment.Center, vec2(74,34), false, white)
    -- if perf<0 then
    --     ui.drawRectFilled(vec2(635, 255), vec2(773, 318), green, 1, ui.CornerFlags.All)
    -- end


end
ui.pushDWriteFont('Verdana:../fonts/verdanab.ttf; Weight=bold; Stretch=Expanded')

ui.setCursor(vec2(0,0))

ui.setCursor(vec2(21,330))
ui.dwriteTextAligned(laptimedisplay, 62, ui.Alignment.Center, ui.Alignment.Center, vec2(332,128), false, white)

ui.setCursor(vec2(21,496))
ui.dwriteTextAligned(string.format('%.1f',tyrepresslf), 58, ui.Alignment.Center, ui.Alignment.Center, vec2(155,128), false, white)
ui.setCursor(vec2(21,661))
ui.dwriteTextAligned(string.format('%.1f',tyrepresslr), 58, ui.Alignment.Center, ui.Alignment.Center, vec2(155,128), false, white)
ui.setCursor(vec2(198,496))
ui.dwriteTextAligned(string.format('%.1f',tyrepressrf), 58, ui.Alignment.Center, ui.Alignment.Center, vec2(155,128), false, white)
ui.setCursor(vec2(198,661))
ui.dwriteTextAligned(string.format('%.1f',tyrepressrr), 58, ui.Alignment.Center, ui.Alignment.Center, vec2(155,128), false, white)



ui.setCursor(vec2(408,330))
ui.dwriteTextAligned(string.format('%.2f', perf), 67, ui.Alignment.Center, ui.Alignment.Center, vec2(208,128), false, perfcolor)

ui.setCursor(vec2(408,481))
ui.dwriteTextAligned(gear, 230, ui.Alignment.Center, ui.Alignment.Center, vec2(208,304), false, white)


ui.setCursor(vec2(672,501))
ui.dwriteTextAligned(abs, 58, ui.Alignment.Center, ui.Alignment.Center, vec2(97,128), false, white)

ui.setCursor(vec2(789,501))
ui.dwriteTextAligned(tc, 58, ui.Alignment.Center, ui.Alignment.Center, vec2(97,128), false, white)

ui.setCursor(vec2(906,501))
ui.dwriteTextAligned(lap, 58, ui.Alignment.Center, ui.Alignment.Center, vec2(97,128), false, white)


ui.pushDWriteFont('Verdana:../fonts/verdana.ttf; Weight=bold; Stretch=Expanded')

ui.setCursor(vec2(672,330))
ui.dwriteTextAligned(speed, 90, ui.Alignment.Center, ui.Alignment.Center, vec2(332,128), false, white)

ui.setCursor(vec2(672,661))
ui.dwriteTextAligned(string.format("%.1f",fuellevel), 90, ui.Alignment.Center, ui.Alignment.Center, vec2(332,128), false, white)





-- ui.setCursor(vec2(203,298))
-- ui.dwriteTextAligned(string.format("%.1f",oiltemp), 50, ui.Alignment.End, ui.Alignment.Center, vec2(143,63), false, white)
-- ui.setCursor(vec2(203,361))
-- ui.dwriteTextAligned(string.format("%.2f",oilpress), 50, ui.Alignment.End, ui.Alignment.Center, vec2(143,63), false, white)
-- ui.setCursor(vec2(203,424))
-- ui.dwriteTextAligned(string.format("%.1f",watertemp), 50, ui.Alignment.End, ui.Alignment.Center, vec2(143,63), false, white)







-- popup(dt)



-- ui.setCursor(vec2(182,244))
-- ui.dwriteTextAligned(compound, 38, ui.Alignment.Center, ui.Alignment.Center, vec2(87,52), false, white)





-- ui.setCursor(vec2(203,550))
-- ui.dwriteTextAligned(string.format('%.1f', brakebias*100), 50, ui.Alignment.End, ui.Alignment.Center, vec2(143,63), false, white)





-- ui.setCursor(vec2(852,710))
-- ui.dwriteTextAligned(esp, 32, ui.Alignment.Center, ui.Alignment.Center, vec2(62,86), false, white)

-- ui.setCursor(vec2(389,710))
-- ui.dwriteTextAligned(rpm, 67, ui.Alignment.Center, ui.Alignment.Center, vec2(246,86), false, white)

-- ui.setCursor(vec2(929,715))
-- ui.dwriteTextAligned(gearmode, 44, ui.Alignment.Center, ui.Alignment.Center, vec2(57,69), false, white)

-- ui.setCursor(vec2(307,236))
-- if car.headlightsActive==true then
--         ui.image(icons.HighBeams, vec2(54, 54), rgbm(0.15,0.2,1,1))
--     else
--         ui.image(icons.LowBeams, vec2(54,54), rgbm(1,1,1,.8))
-- end
-- local bbpos = 198
-- bbpos=((car.brakeBias-0.65)*1100)+198
-- ui.drawRectFilled(vec2(bbpos,608), vec2((bbpos+11),672), white)

laps = car.lapCount

ac.debug('brakebias', car.brakeBias)

end