ui.setAsynchronousImagesLoading(true)
local rpmframe
local rpmmaxframe = 300
local rpmvideosize = vec2(2000, 1000)*1
local rpmposition = vec2(0,200)
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 == 300 then
        carRPM = math.applyLag(carRPM, car.rpm, 0.8, dt)

        rpmframe = math.min(rpmmaxframe, (rpmmaxframe/9000)*carRPM)
        if carRPM >= 0 and carRPM <= 2000 then
            rpmframe = (35/2000)*carRPM
        elseif carRPM > 2000 and carRPM <= 3000 then
            rpmframe = ((65-35)/(3000-2000))*carRPM+(35-(((65-35)/(3000-2000))*2000))
        elseif carRPM > 3000 and carRPM <= 4000 then
            rpmframe = ((90-65)/(4000-3000))*carRPM+(65-(((90-65)/(4000-3000))*3000))
        elseif carRPM > 4000 and carRPM <= 5000 then
            rpmframe = ((120-90)/(5000-4000))*carRPM+(90-(((120-90)/(5000-4000))*4000))
        elseif carRPM > 5000 and carRPM <= 6000 then
            rpmframe = ((150-120)/(6000-5000))*carRPM+(120-(((150-120)/(6000-5000))*5000))
        elseif carRPM > 6000 and carRPM <= 7000 then
            rpmframe = ((180-150)/(7000-6000))*carRPM+(150-(((180-150)/(7000-6000))*6000))
        elseif carRPM > 7000 and carRPM <= 8000 then
            rpmframe = ((260-180)/(8000-7000))*carRPM+(180-(((260-180)/(8000-7000))*7000))
        elseif carRPM > 8000 and carRPM <= 9000 then
            rpmframe = ((300-260)/(9000-8000))*carRPM+(260-(((300-260)/(9000-8000))*8000))
        end

        --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
    display.text {
        text = math.round(car.speedKmh * 0.6213711922, 0),
        pos = vec2(448, 859),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*1.2,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 0.5
    }
    
    if car.gear == -1 then
        display.text {
            text = "R",
			pos = vec2(890,867),
			color = rgbm(1,1,1,1),
            letter = vec2(50, 200)*1.7,
            font = "./fonts/arial_hd",
            width = 200,
            alignment = 0.5
        }
    elseif car.gear == 0 then
        display.text {
            text = "N",
			pos = vec2(895,867),
			color = rgbm(1,1,1,1),
            letter = vec2(50, 200)*1.7,
            font = "./fonts/arial_hd",
            width = 200,
            alignment = 0.5
        }
    else
        display.text {
            text = car.gear,
			pos = vec2(894,867),
			color = rgbm(1,1,1,1),
            letter = vec2(50, 200)*1.7,
            font = "./fonts/arial_hd",
            width = 200,
            alignment = 0.5
        }
    end
    display.text {
        text = math.round(car.wheels[0].tyrePressure,0),
        pos = vec2(1313, 849.1),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*0.6,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 1
    }
    display.text {
        text = math.round(car.wheels[1].tyrePressure,0),
        pos = vec2(1647, 849.1),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*0.6,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 0
    }
    display.text {
        text = math.round(car.wheels[2].tyrePressure,0),
        pos = vec2(1313, 955.1),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*0.6,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 1
    }
    display.text {
        text = math.round(car.wheels[3].tyrePressure,0),
        pos = vec2(1647, 955.1),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*0.6,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 0
    }

    display.text {
        text = math.round((car.wheels[0].tyreCoreTemperature * 9/5) + 32,0),
        pos = vec2(1313, 889.1),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*0.6,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 1
    }
    display.text {
        text = math.round((car.wheels[1].tyreCoreTemperature * 9/5) + 32,0),
        pos = vec2(1647, 889.1),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*0.6,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 0
    }
    display.text {
        text = math.round((car.wheels[2].tyreCoreTemperature * 9/5) + 32,0),
        pos = vec2(1313, 1000.1),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*0.6,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 1
    }
    display.text {
        text = math.round((car.wheels[3].tyreCoreTemperature * 9/5) + 32,0),
        pos = vec2(1647, 1000.1),
        color = rgbm(1,1,1,1),
        letter = vec2(50, 200)*0.6,
        font = "./fonts/arial_hd",
        width = 200,
        alignment = 0
    }
    

    display.text {
        text = time,
        pos = vec2(70,445),
        color = rgbm(1,1,1,1),
        letter = vec2(0, 0),
        font = "bmw",
        width = 100,
        alignment = 1
    }
    display.text {
        text = math.floor(car.distanceDrivenTotalKm/1.609) .. " mi",
        pos = vec2(1580,1110),
        color = rgbm(1,1,1,1),
        letter = vec2(20, 50)*1.2,
        font = "e92_big",
        width = 100,
        alignment = 0
    }

 
        ----- outside temp ------
        local temp =  sim.ambientTemperature .. "*"
        ui.drawRectFilled(vec2(2900, 1630),vec2(3130, 1695),rgbm(0,0,0,0.5), 20)
        display.text {
            text = temp,
            pos = vec2(700,1121),
            color = rgbm(1,1,1,1),
            letter = vec2(25, 50),
            font = "e92_big",
            width = 100,
            alignment = 1
        }

    if not car.lowBeams and car.headlightsActive then
        local pos = vec2(200,1110)
        ui.drawImage("./assets/icons/headlight.png", pos,vec2(pos.x+50, pos.y+50) , rgbm(0.0,0.3,1,1))
    end
    if car.lowBeams and car.headlightsActive then
        local pos = vec2(200,1110)
        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(300,1115)
        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(720,1118)
		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(1220,1118)
        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(1220,1118)
        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(720,1118)
		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
   
    if (ac.isInteriorView() == false) then
		indicator_1:setVolume(0)
        indicator_2:setVolume(0) 
	else
		indicator_1:setVolume(ac.getAudioVolume("main") * 1)
		indicator_2:setVolume(ac.getAudioVolume("main") * 1)
	end
	if (ac.getCar().turningLeftOnly == true and rightstate == 1 and blinkerstate == 1) then 
			indicator_1:play()
			rightstate = 0
			leftstate = 1
	elseif (ac.getCar().turningRightOnly == true and leftstate == 1 and blinkerstate == 1) then 
			indicator_1:play()
			leftstate = 0
			rightstate = 1
	end
    if (ac.getCar().turningLeftOnly == true) then     
		if (blinkerstate == 0) then
			indicator_1:play()
		end
		indicator_2:play()
		leftstate = 1
		rightstate = 0
		blinkerstate = 1
	elseif (ac.getCar().turningRightOnly == true) then   
		if (blinkerstate == 0) then
			indicator_1:play()
		end	
		indicator_2:play()
		leftstate = 0
		rightstate = 1
        blinkerstate = 1
	elseif (ac.getCar().turningLeftLights == false and ac.getCar().turningRightLights  == false and blinkerstate == 1) then
		indicator_2:pause()
        indicator_2:setCurrentTime(0)
		hazardstate = 0
		blinkerstate = 0
		leftstate = 0
		rightstate = 0
	elseif (ac.getCar().hazardLights == true) then
        indicator_2:play()
        hazardstate = 1
		blinkerstate = 1
    end
end