sim = ac.getSim()



local gearsize = vec2(660, 580)
local gearpos = vec2(190, 493)
local gearsize2 = vec2(544, 464)
local gearpos2 = vec2(1440, 920)
local gearpos3 = vec2(70, 760)
local gearsize3 = vec2(435, 371)
local iconsize = vec2(165, 160)
local p1 = vec2(0, 0)
local s1 = vec2(0, 0)
local p2 = vec2(0, 0)
local s2 = vec2(0, 0)
local width1 = 46
local fontstandard = "assets/din_next_bold"
local fontstbold = "assets/din_next"
local rpmbar2 = "assets/rpm_bar_2.dds"
local battery = "assets/battery.dds"
local value = 0.0
local white = rgbm(1,1,1,1)
local green = rgbm(0,1,0,1)
local blue = rgbm(0,0,1,1)
local red = rgbm(1,0,0,1)
local black = rgbm(0,0,0,1)
local orange = rgbm(1,0.5,0,1)
local colfuel = rgbm(0.94, 0.62, 0.08, 1)
local carRPM = car.rpm
local rpmcolor = green
local gearcolor = black
local alpha = 0.5



-- page 1 - racing display
local function modeA(dt)

    display.rect {
        pos = vec2(9, 405),
        size = vec2(2000, 1200),
        color = rgbm(1, 1, 1, 1)
    }
    -- RPM gauge

    value = math.saturate(car.rpm / 8000)
    if car.rpm < 6800 then
        rpmcolor = green
    elseif car.rpm < 6900 then
        rpmcolor = orange
    else
        rpmcolor = red
    end

    p1.x=40 p1.y=620
    s1.x=1988 * value s1.y=978
    p2.x=0 p2.y=0
    s2.x=value s2.y=1
    display.image {
        image = rpmbar2,
        pos = p1,
        size = s1,
        uvStart = p2,
        uvEnd = s2,
        color = rpmcolor
        }


        p1.x=0 p1.y=-1
    s1.x=2048 s1.y=2048
    display.image {
        image = "assets/page1.dds",
        pos = p1, -- coordinates of top left corner
        size = s1
    }

  -- gear display

  local gearText = tostring(car.gear) -- needs to be converted so that neutral and reverse display correctly (-1 = R, 0 = N)
  if car.gear == -1 then
      gearText = "R"
  end
  if car.gear == 0 then
      gearText = "N"
  end

  if car.rpm < 6900 then
      gearcolor = black
     else
      gearcolor = red
  end

  display.text {
      text = gearText,
      pos = vec2(820, 1000),
      letter = vec2(400, 750),
      font = fontstandard,
      width = 46,
      alignment = 0.5,
      spacing = 0,
      color = gearcolor
  }

    --speed text

    p1.x=50 p1.y=420
    s1.x=100 s1.y=210
    display.text {
        text = string.format("%3d", math.floor(car.speedKmh)),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = 1,
        alignment = 0,
        spacing = 0
    }

    -- oil pressure display text

    p1.x=390 p1.y=1490
    s1.x=80 s1.y=160
    display.text {
        text = string.format("%2.2f", car.oilPressure*1.1),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -15,
        color = black
    }

    -- water temp display text

    p1.x=520 p1.y=1280
    s1.x=80 s1.y=160
    display.text {
        text = string.format("%2.0f", car.waterTemperature),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -15,
        color = black
    }

      -- lap counter

    p1.x=1780 p1.y=1440
    s1.x=120 s1.y=220
    display.text {
        text = string.format('%2d', car.lapCount),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 0.5,
        spacing = 0,
        color = black
    }

    -- last lap text

    local time = car.previousLapTimeMs
    local formattedTime =
    string.format(
      "%01d:%02d.%02d",
      math.floor((time / (1000 * 60))) % 60,
      math.floor((time / 1000)) % 60,
      math.floor((time % 1000) / 10)
    )
    p1.x=1500 p1.y=1220
    s1.x=70 s1.y=140
    display.text {
        text = formattedTime,
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -5,
        color = black
    }

    -- delta performance

    local perfMeter = car.performanceMeter
    local formattedTime = string.format('%+.3f', perfMeter)
    p1.x=1390 p1.y=955
    s1.x=120 s1.y=230
    display.text {
        text = formattedTime,
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = 46,
        alignment = 1,
        spacing = -30,
        color = black
    }

    -- time text

        local time = car.lapTimeMs
        local formattedTime = string.format(
            "%02d.%02d",

            math.floor((time / 1000)) % 60,
            math.floor((time % 1000) / 10)
        )
        p1.x=440 p1.y=480
        s1.x=120 s1.y=250
        display.text {
            text = formattedTime,
            pos = p1,
            letter = s1,
            font = fontstandard,
            width = 40,
            alignment = 1,
            spacing = -20
        }
    

end
---------------------------------------
-- page 2 - technical screen
--------------------------------------

local function modeB(dt)

    display.rect {
        pos = vec2(9, 405),
        size = vec2(2000, 1200),
        color = rgbm(0.5, 0.5, 0.5, 1)
    }

    -- exhaust temp gauge masked

    local value = math.saturate(car.exhaustTemperature /1000)
    if value < 0.5 then
        color = blue
    else
        color = white
    end
    p1.x=140 p1.y=1560
    s1.x=422 s1.y=-value*560
    p2.x=0 p2.y=0
    s2.x=1 s2.y=1
    display.rect {
    pos = p1,
    size = s1,
    color = color,
    uvStart = p2,
    uvEnd = s2
    }

-- oil pressure gauge masked

    local value = math.saturate(car.oilPressure/12)
    if value < 0.35 then
        color = red
    else
        color = white
    end
    p1.x=1490 p1.y=1560
    s1.x=360 s1.y=-value*560
    p2.x=0 p2.y=0
    s2.x=1 s2.y=1
    display.rect {
    pos = p1,
    size = s1,
    color = color,
    uvStart = p2,
    uvEnd = s2
    }

     -- rotating rpm gauge
    local rpmPercentage = (car.rpm / 8000 * 100) -- conversion to %


    local amountOfSquares = math.ceil(rpmPercentage/6.25) -- only render the squares the user will actually see, for performance.

    local color = rgbm(1, 1, 0.9, 1) -- normal colour of the displayed rectangle
    if 85 < (car.rpm / car.rpmLimiter * 100) then -- if rpms exceed Configured Value colour switches
        color = rgbm(255, 0, 0, 255)
    end
    local rpmPos = vec2(620, 1270)
    local rpmSize = vec2(250, 250)
    local rpmPivot = vec2(1028, 1028)

    for i = 1, amountOfSquares do
        local thisRotation = (-rpmPercentage) * 2.7 -- "-" turns rotation counter clockwise
        ui.beginRotation()
        ui.beginRotation()
        display.rect {
            -- draws rectangle
            pos = rpmPos,
            size = rpmSize,
            color = color
        }
        ui.endRotation(28)
        if rpmPercentage > (100 / 16 * i) then
            thisRotation = -(100 / 16 * i) * 2.7
        end
        ui.endPivotRotation(thisRotation + 107, rpmPivot)
    end
    
 -- water temp gauge masked

        local value = math.saturate(car.waterTemperature /120)
        p1.x=158 p1.y=1000
        s1.x=300 s1.y=-value*560
        p2.x=0 p2.y=0
        s2.x=1 s2.y=1
        display.rect {
        pos = p1,
        size = s1,
        color = white,
        uvStart = p2,
        uvEnd = s2
        }

    -- oil temp gauge masked

        local value = math.saturate(car.oilTemperature /120)
        p1.x=1560 p1.y=1000
        s1.x=350 s1.y=-value*560
        p2.x=0 p2.y=0
        s2.x=1 s2.y=1
        display.rect {
        pos = p1,
        size = s1,
        color = white,
        uvStart = p2,
        uvEnd = s2
        }

        -- page 2 image

        p1.x=0 p1.y=-1
        s1.x=2048 s1.y=2048
        display.image {
            image = "assets/page2.dds",
            pos = p1,
            size = s1
        }

        -- water temp display text

        p1.x=40 p1.y=490
        s1.x=60 s1.y=120
        display.text {
            text = string.format("%2.1f", car.waterTemperature),
            pos = p1,
            letter = s1,
            font = fontstandard,
            width = width1,
            alignment = 1,
            spacing = -15,
            color = white
        }

        -- exhaust temp display text

        p1.x=40 p1.y=1480
        s1.x=60 s1.y=120

        if car.exhaustTemperature > 500 then
            color = white
        else
            color = blue
        end

        display.text {
            text = string.format("%2.1f", car.exhaustTemperature),
            pos = p1,
            letter = s1,
            font = fontstandard,
            width = width1,
            alignment = 1,
            spacing = -15,
            color = color
         
            
        }

    -- oil temp display text

    p1.x=1840 p1.y=490
    s1.x=60 s1.y=120
    display.text {
        text = string.format("%2.1f", car.oilTemperature),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -15,
        color = white
    }

    -- oil pressure display text

    p1.x=1840 p1.y=1480
    s1.x=60 s1.y=120

    if car.oilPressure > 4 then
        color = white
    else
        color = red
    end

    display.text {
        text = string.format("%2.1f", car.oilPressure*1.1),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -15,
        color = color
    }
    -- gear display

    local gearText = tostring(car.gear)
    if car.gear == -1 then
        gearText = "R"
    end
    if car.gear == 0 then
        gearText = "N"
    end

    if car.rpm < 6900 then
        gearcolor = black
       else
        gearcolor = red
    end

    display.text {
        text = gearText,
        pos = vec2(820, 730),
        letter = vec2(400, 750),
        font = fontstandard,
        width = 46,
        alignment = 0.5,
        spacing = 0,
        color = gearcolor
    }

end

--------------------------------------------
-- page 3 - pit limiter screen
-------------------------------------------

local function modeC(dt)
    display.rect {
        pos = vec2(9, 405),
        size = vec2(2000, 1200),
        color = rgbm(1, 1, 1, 1)
    }
    -- RPM gauge

    value = math.saturate(car.rpm / 8000)
    if car.rpm < 6800 then
        rpmcolor = green
    elseif car.rpm < 6900 then
        rpmcolor = orange
    else
        rpmcolor = red
    end

    p1.x=40 p1.y=620
    s1.x=1988 * value s1.y=978
    p2.x=0 p2.y=0
    s2.x=value s2.y=1
    display.image {
        image = rpmbar2,
        pos = p1,
        size = s1,
        uvStart = p2,
        uvEnd = s2,
        color = rpmcolor
        }


        p1.x=0 p1.y=-1
    s1.x=2048 s1.y=2048
    display.image {
        image = "assets/page3.dds",
        pos = p1, -- coordinates of top left corner
        size = s1
    }

    --speed text

    p1.x=50 p1.y=420
    s1.x=100 s1.y=210
    display.text {
        text = string.format("%3d", math.floor(car.speedKmh)),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = 1,
        alignment = 0,
        spacing = 0,
        color = black
    }

    -- gear display

    local gearText = tostring(car.gear) -- needs to be converted so that neutral and reverse display correctly (-1 = R, 0 = N)
    if car.gear == -1 then
        gearText = "R"
    end
    if car.gear == 0 then
        gearText = "N"
    end
    display.text {
        text = gearText,
        pos = vec2(820, 1000),
        letter = vec2(400, 750),
        font = fontstandard,
        width = 46,
        alignment = 0.5,
        spacing = 0,
        color = black
    }

    -- Pit limiter ON/OFF message

	 if (car.speedLimiter > 0) then
        p1.x=380 p1.y=1500
        s1.x=60 s1.y=120
            display.text {
        text = "OK",
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -5,
        color = black
    }
    else
        p1.x=380 p1.y=1500
        s1.x=60 s1.y=120
            display.text {
        text = "OFF",
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -5,
        color = black
    }
    end

    -- water temp display text

    p1.x=520 p1.y=1280
    s1.x=75 s1.y=150
    display.text {
        text = string.format("%2.0f", car.waterTemperature),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -15,
        color = black
    }

    -- last lap text

    local time = car.bestLapTimeMs
    local formattedTime =
    string.format(
      "%01d:%02d.%02d",
      math.floor((time / (1000 * 60))) % 60,
      math.floor((time / 1000)) % 60,
      math.floor((time % 1000) / 10)
    )
    p1.x=1515 p1.y=1480
    s1.x=70 s1.y=140
    display.text {
        text = formattedTime,
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -5,
        color = black
    }

    -- average consumption

        p1.x=440 p1.y=480
        s1.x=120 s1.y=250
    display.text {
        text = string.format("%2.2f", car.fuelPerLap),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 1,
        spacing = -15,
        color = black
    }
end

---------------------------------------------
-- page 4 - launch control
---------------------------------------------

local function modeD(dt)

    display.rect {
        pos = vec2(9, 405),
        size = vec2(2048, 500),
        color = white
    }

    display.rect {
        pos = vec2(10, 1000),
        size = vec2(2048, 200),
        color = red
    }

    display.rect {
        pos = vec2(10, 1300),
        size = vec2(2048, 300),
        color = white
    }

    --  rpm gauge at launch control

    value = math.saturate(car.rpm / 8000) -- saturate clamps value between 0 and 1
    if car.rpm < 5000 then
        rpmcolor = green
    elseif car.rpm < 5100 then
        rpmcolor = orange
    else
        rpmcolor = red
    end

    p1.x=10 p1.y=490
    s1.x=2001*value s1.y=354
    p2.x=0 p2.y=0
    s2.x=value s2.y=1
    display.rect {
        pos = p1,
        size = s1,
        uvStart = p2,
        uvEnd = s2,
        color = rpmcolor
    }

    -- turbo pressure gauge

    local value = math.saturate(car.turboBoost / 2.6)
    p1.x=20 p1.y=1032
    s1.x=2000*value s1.y=140
    p2.x=0 p2.y=0
    s2.x=value s2.y=1
    if value < 0.25 then
        rpmcolor = orange
    elseif value < 0.75 then
        rpmcolor = green
        else
        rpmcolor = orange
    end
    display.rect {
        pos = p1,
        size = s1,
        uvStart = p2,
        uvEnd = s2,
        color = rpmcolor
    }
    
    -- throttle bar
    
    local value = math.saturate(car.gas)
    if value < 0.85 then
        rpmcolor = blue
    else
        rpmcolor = green
    end
    p1.x=1342 p1.y=1560
    s1.x=120 s1.y=-value*250
    p2.x=0 p2.y=0
    s2.x=value s2.y=1
    display.rect {
        pos = p1,
        size = s1,
        color = rpmcolor,
        uvStart = p2,
        uvEnd = s2
    }

    -- exhaust gauge

    local value = math.saturate(car.exhaustTemperature /1000)
    p1.x=1727 p1.y=1560
    s1.x=118 s1.y=-value*250
    p2.x=0 p2.y=0
    s2.x=1 s2.y=1
    display.rect {
    pos = p1,
    size = s1,
    color = black,
    uvStart = p2,
    uvEnd = s2
    }

    -- clutch bar
    local value = math.saturate(1 - car.clutch)
    if value < 0.5 then         -- set the bite point!
        rpmcolor = blue
    elseif value < 0.75 then
        rpmcolor = green
    else
        rpmcolor = red
    end 
    p1.x=956 p1.y=1560
    s1.x=120 s1.y=-value*250
    p2.x=0 p2.y=0
    s2.x=1 s2.y=1
    display.rect {
        pos = p1,
        size = s1,
        color = rpmcolor,
        uvStart = p2,
        uvEnd = s2
      }

    -- Gear bar

    local value = tostring(car.gear)
    if value == "1" then
        rpmcolor = green
    else
        rpmcolor = red
    end
    p1.x=572 p1.y=1560
    s1.x=120 s1.y=-42*value
    p2.x=0 p2.y=0
    s2.x=1 s2.y=1
    display.rect {
        pos = p1,
        size = s1,
        color = rpmcolor,
        uvStart = p2,
        uvEnd = s2
      }

    -- Car speed bar

    local value = math.floor(car.speedKmh)
    if value < 37 then
        rpmcolor = black
    else
        rpmcolor = green
    end
    p1.x=188 p1.y=1560
    s1.x=120 s1.y=-value
    p2.x=0 p2.y=0
    s2.x=1 s2.y=1
    display.rect {
        pos = p1,
        size = s1,
        color = rpmcolor,
        uvStart = p2,
        uvEnd = s2
      }

    -- background image

    p1.x=0 p1.y=-1
    s1.x=2048 s1.y=2048
    display.image {
       image = "assets/page4.dds",
       pos = p1, -- coordinates of top left corner
       size = s1
    }

    -- turbo presure text

    p1.x=1895 p1.y=950
    s1.x=40 s1.y=80
    display.text {
        text = string.format("%3d", car.turboBoost * 1000),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 2,
        spacing = -10,
        color = blue
    }
        
    -- pedal perc text
    
    p1.x=1350 p1.y=1575
    s1.x=40 s1.y=80
    display.text {
        text = string.format("%3d", car.gas * 101),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 2,
        spacing = -10,
        color = black
    }

    -- exhaust temp text

    p1.x=1735 p1.y=1575
    s1.x=40 s1.y=80
    display.text {
        text = string.format("%3d", car.exhaustTemperature),
        pos = p1,
        letter = s1,
        font = fontstandard,
        width = width1,
        alignment = 2,
        spacing = -5,
        color = black
    }

    -- clutch bar text

     p1.x=956 p1.y=1575
     s1.x=40 s1.y=80
     display.text {
         text = string.format("%3d", (1 - car.clutch)*100),
         pos = p1,
         letter = s1,
         font = fontstandard,
         width = width1,
         alignment = 2,
         spacing = -5,
         color = black
     }

    -- Gear bar text

     p1.x=572 p1.y=1575
     s1.x=40 s1.y=80
     display.text {
         text = string.format("%3d", car.gear),
         pos = p1,
         letter = s1,
         font = fontstandard,
         width = width1,
         alignment = 2,
         spacing = -5,
         color = black
     }

    -- Car speed text

     p1.x=186 p1.y=1575
     s1.x=40 s1.y=80
     display.text {
         text = string.format("%3d", math.floor(car.speedKmh)),
         pos = p1,
         letter = s1,
         font = fontstandard,
         width = width1,
         alignment = 2,
         spacing = -5,
         color = black
     }
     
     
    end

------------------------------------------------
-- display switch
------------------------------------------------

local listOfModes = {modeA, modeB, modeC, modeD} -- 4 displays cycle
local currentMode = tonumber(ac.loadDisplayValue("displayMode") or "1")
local prevMode    = tonumber("1")
local lastExtraAState = false
local lastExtraFState = false
local lastBrakeBlance = car.brakeBias or 0.0
local lastPitLimiterOn = car.manualPitsSpeedLimiterEnabled or false
local brakebiasTimer = 0.0
local pitLimiterTimer = 0.0
local lcOn = false

function script.update(dt)
    if not currentMode then
        if car.speedKmh > 5 then
            currentMode = 1 -- modeA
        else
            currentMode = 2 -- modeB
        end
    end

    if car.extraF then
        currentMode = 4 -- modeD
    else
        if car.manualPitsSpeedLimiterEnabled then
            currentMode = 3 -- modeC
        else
            if car.speedKmh > 5 then
                currentMode = 1 -- modeA
            else
                currentMode = 2 -- modeB
            end
        end
    end

    listOfModes[currentMode](dt)

    ac.debug("value", math.saturate(car.oilPressure/12))
end