ui.setAsynchronousImagesLoading(true)
local rpmframe
local rpmmaxframe = 299
local rpmvideosize = vec2(1920, 980)*0.7
local rpmposition = vec2(660,900)
local rpmStartUp = 0
local carRPM = 0

local blinkerstate = 0
local leftstate = 0
local rightstate = 0
local hazardstate = 0

local dashReady = false

local indicator_1 = ui.MediaPlayer() 
local indicator_2 = ui.MediaPlayer()
indicator_1:setSource("./sounds/on.mp3")
indicator_2:setSource("./sounds/indicator.mp3")
 
function update(dt)
	ui.setAsynchronousImagesLoading(true)
    if rpmStartUp < rpmmaxframe then
        rpmStartUp = rpmStartUp + 1
        ui.drawImage("./assets/cluster/cluster" .. rpmStartUp .. ".jpg", rpmposition, vec2(rpmposition.x+rpmvideosize.x, rpmposition.y+rpmvideosize.y))
    end
    local time = string.format("%02d:%02d",sim.timeHours,sim.timeMinutes)
    local temp = "+" .. sim.ambientTemperature .. "C"
    local distance = (car.fuel * sim.trackLengthM ) / car.fuelPerLap .. "km"

    if rpmStartUp == rpmmaxframe then
        carRPM = math.applyLag(carRPM, car.rpm, 0.8, dt)

        normalizedRPM = carRPM / 7000

        -- Apply quadratic easing function
        easedRPM = normalizedRPM ^ 3

        -- Calculate the rpmframe based on the easedRPM value
        rpmframe = math.min(rpmmaxframe, rpmmaxframe * easedRPM)


        --rpmframe = math.min(rpmmaxframe, (rpmmaxframe/9000)*carRPM)
        --rpmframe = math.floor(rpmframe)
    
        ui.drawImage("./assets/cluster/cluster" .. math.min(rpmmaxframe, math.floor(rpmframe)) .. ".jpg", rpmposition, vec2(rpmposition.x+rpmvideosize.x, rpmposition.y+rpmvideosize.y))

    end
    if dashReady == false then
        if rpmStartUp == rpmmaxframe then
            display.text{
            text = "DASH READY",
            pos = vec2(900,200),
            letter = vec2(10, 25):scale(2),
            font = "fonts/340i",
            color = rgbm.colors.green,
            alignment = 0,
            width = 1000,
            spacing = 0
            }
           setTimeout(function () dashReady = true end, 1)            
        else
            display.text{
                text = "    DASH \nINITIALISING",
                pos = vec2(325,170),
                letter = vec2(10, 25):scale(2),
                font = "fonts/340i",
                color = rgbm.colors.white,
                alignment = 1,
                width = 1000,
                spacing = 0
            }
        end
    end
    local amgrpm = 0
   
    if carRPM < 10 then
        amgrpm = '   '..string.sub(math.round(carRPM, 0),1,1)
    elseif carRPM < 100 then
        amgrpm = '  '..string.sub(math.round(carRPM, 0),1,1) .. '0'
    elseif carRPM < 1000 then
        amgrpm = ' '..string.sub(math.round(carRPM, 0),1,2) .. '0'
    elseif carRPM < 10000 then
        amgrpm = string.sub(math.round(carRPM, 0),1,2) .. '00'
    end
    display.text {
        text = string.sub(math.round(amgrpm, 0), -3),
        pos = vec2(1205,1300),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 50)*1,
        font = "./fonts/Microsquare",
        width = 200,
        alignment = 1,
        spacing = -20
    }  
    if carRPM > 999 then
        display.text {
            text = string.sub(math.round(amgrpm, 0),1,1),
            pos = vec2(1118,1288),
            color = rgbm(1,1,1,1),
            letter = vec2(50, 50)*1.25,
            font = "./fonts/Microsquare",
            width = 200,
            alignment = 1,
            spacing = -40
        }  
    end
    display.text {
        text = math.round(car.speedKmh, 0),
        pos = vec2(1230,1000),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 50)*1.75,
        font = "./fonts/Microsquare",
        width = 200,
        alignment = 0.5,
        spacing = -30
    }
    
    if car.gear == -1 then
        display.text {
            text = "R",
			pos = vec2(1230,1210),
			color = rgbm(1,1,1,1),
            letter = vec2(50, 50)*1.5,
            font = "./fonts/Microsquare",
            width = 200,
            alignment = 0.5
        }
    elseif car.gear == 0 then
        display.text {
            text = "N",
			pos = vec2(1230,1210),
			color = rgbm(1,1,1,1),
            letter = vec2(50, 50)*1.5,
            font = "./fonts/Microsquare",
            width = 200,
            alignment = 0.5
        }
    else
        display.text {
            text = car.gear,
			pos = vec2(1230,1210),
			color = rgbm(1,1,1,1),
            letter = vec2(50, 50)*1.5,
            font = "./fonts/Microsquare",
            width = 200,
            alignment = 0.5
        }
    end

    local pos =  vec2(1560,1425)
    ui.drawImage("./assets/icons/icons.png", pos*0.7,vec2(pos.x+734, pos.y+141)*0.7 , rgbm(1,1,1,1))
    
    if not car.lowBeams and car.headlightsActive then
        local pos = vec2(1450,1400)
        ui.drawImage("./assets/icons/headlight.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(0.1,0.1,1,1))
    end
    if car.lowBeams and car.headlightsActive then
        local pos = vec2(1450,1400)
        ui.drawImage("./assets/icons/low_beam_headlight.png", pos,vec2(pos.x+50, pos.y+50)  , rgbm(0.1,1,0.1,1))
    end
    
    if not lasttrueTC then
        lasttrueTC = 0
    end
    if car.tractionControlInAction then
        setInterval(function() tractionControlInAction = not tractionControlInAction end, 0.15, "tractionControlInAction")
        lasttrueTC = os.clock()
	elseif (os.clock() - lasttrueTC > 0.5) then 
        clearInterval("tractionControlInAction")
		tractionControlInAction = false
        
    end
    if car.tractionControlMode == 0 or tractionControlInAction then
        local pos = vec2(1165,1400)
        ui.drawImage("./assets/icons/traction_controle.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(25,2,0,1))
    end
 	
    if car.turningLeftLights or car.turningRightLights then
		setInterval(function() indicatorActive = not indicatorActive end, 0.334, "indicatorActive")
	else
		clearInterval("indicatorActive")
		indicatorActive = false
	end


	if car.turningLeftOnly and indicatorActive then 
		local pos = vec2(1000,1000)
		ui.beginRotation()
        ui.drawImage("./assets/icons/turn_signal.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(0.1,1,0.1,1))
		ui.endRotation(-90)
    elseif car.turningRightOnly and indicatorActive then 
		local pos = vec2(1620,1000)
        ui.drawImage("./assets/icons/turn_signal.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(0.1,1,0.1,1))
    elseif car.hazardLights and indicatorActive then
		local pos1 = vec2(1620,1000)
        ui.drawImage("./assets/icons/turn_signal.png", pos1,vec2(pos1.x+50, pos1.y+50) , rgbm(0.1,1,0.1,1))
		local pos2 = vec2(1000,1000)
		ui.beginRotation()
        ui.drawImage("./assets/icons/turn_signal.png", pos2,vec2(pos2.x+50, pos2.y+50) , rgbm(0.1,1,0.1,1))
		ui.endRotation(-90)
	end
end