local mode = 1
local updateDelay = 0
local overlayText = ""

local Nm = 0
local GAS = 0
local RPM = 0
local TEMP = 0
local BOOST = 0
local logo = 0
local logo1 = 0

local shiftWarnOn = true
local shiftWarnCycle = 2
local shiftPercentage = 0

local timer = 2.0
local carSounds = ui.MediaPlayer()

function update(dt)

ui.setAsynchronousImagesLoading(true)

    if ((ac.getCar().turningLeftLights == true or ac.getCar().turningRightLights == true) and ac.isInteriorView() ==
        true) then
        -- if(carSounds:playing() ~= true) then
        carSounds:setSource('./assets/turn_signals.mp3'):setAutoPlay(true)
        carSounds:setVolume(ac.getAudioVolume('main') * 0.2)
        carSounds:play()
        -- carSounds:setLooping(true)
        -- end
    else
        carSounds:pause()
        carSounds:setCurrentTime(0)
    end
	
    if timer >= 0 then
        timer = timer  - 0.025 - dt 
	else
	timer = 2 - dt
    end

  display.rect 
		{
        pos = vec2(0,0),
		size = vec2(2048, 2048),
        color = rgbm(0,0,0,2),
        } 
		

		
 display.image {
     image = "assets/BOOST_BG.dds",
     pos = vec2(1585,301),
	 size = vec2(256,170),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

 display.image {
     image = "assets/LONG_BAR.dds",
     pos = vec2(1500 , 337),
	 size = vec2(220,12),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

    BOOST = math.applyLag(BOOST,math.floor(car.turboBoost * 130), 0.2, dt)
   
 display.image {
     image = "assets/LONG_BAR.dds",
     pos = vec2(1390 + BOOST , 429),
	 size = vec2(220,12),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
		
  display.rect 
		{
        pos = vec2(1538.1, 335.9),
		size = vec2(15.2, 15.9),
        color = rgbm(0,0,0,2),
        } 
		
  display.rect 
		{
        pos = vec2(1535.1, 423.6),
		size = vec2(14.8, 21),
        color = rgbm(0,0,0,2),
        } 

 display.image {
     image = "assets/RPM_BG.dds",
	 pos = vec2(494,125),
     size = vec2(1062,543),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

	
    RPM = math.applyLag(RPM, car.rpm / 8000 * 100, 0.2, dt)

 display.image {
     image = "assets/RPM_BAR.dds",
	 pos = vec2(469,580 - RPM * 5),
     size = vec2(280,653),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

 display.image {
     image = "assets/RPM_BAR.dds",
	 pos = vec2(1301,580 - RPM * 5),
     size = vec2(280,653),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

 display.image {
     image = "assets/POWER_BG.dds",
     pos = vec2(195,311),
	 size = vec2(260,185),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
 
 Nm = math.applyLag(GAS,car.drivetrainTorque * car.gas, 0.5, dt)
 

 display.image {
     image = "assets/LONG_BAR.dds",
     pos = vec2(10 + Nm / 3.25 ,342),
	 size = vec2(220,12),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}



 GAS = math.applyLag(GAS,car.drivetrainPower * car.gas, 0.5, dt)

 display.image {
     image = "assets/LONG_BAR.dds",
     pos = vec2(10 + GAS / 3.25, 434),
	 size = vec2(220,12),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}



 --display.image {
--     image = "assets/FUEL_BG.dds",
--	 pos = vec2(79,516),
--     size = vec2(163,160),
--     uvStart = vec2(0,0),
 --    uvEnd = vec2(1,1)
--}

 display.image {
     image = "assets/FUEL_HIGHLITED.dds",
	 pos = vec2(79,526),
     size = vec2(163 ,149),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

	local value = math.floor(car.fuel * 2.3 )
	
        ui.beginRotation()
        ui.beginRotation()
  display.rect 
		{
	 pos = vec2(60,515 - value),
     size = vec2(130,158),
	 color = rgbm(0,0,0,2),
        } 
        ui.endRotation(10)
		
 display.image {
     image = "assets/FUEL_LINE.dds",
	 pos = vec2(100,640 - value),
     size = vec2(126 ,40),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

 
 display.image {
     image = "assets/CLUSTER_DIGI.dds",
	 pos = vec2(0, 0),
     size = vec2(2048,2048),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

 display.image {
     image = "assets/FUEL_OVR.dds",
     pos = vec2(53,488),
	 size = vec2(176,218),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

 display.image {
     image = "assets/C.dds",
	 pos = vec2(1702, 712),
     size = vec2(38,31),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

if car.tractionControlMode == 0 then

 display.image {
     image = "assets/DSC_OFF.dds",
	 pos = vec2(1328, 693),
     size = vec2(117,23),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

if timer > 1 and car.turningLeftLights == true then

	display.image {
        image = "assets/TRUN_L.dds",
        pos = vec2(742,158),
        size = vec2(41,34),
        uvStart = vec2(0, 0),
        uvEnd = vec2(1, 1)
    }
end

if timer > 1 and car.turningRightLights == true then

	display.image {
        image = "assets/TRUN_R.dds",
        pos = vec2(1260,158),
        size = vec2(41,34),
        uvStart = vec2(0, 0),
        uvEnd = vec2(1, 1)
    }
end

if car.absInAction == true  then

	display.image {
        image = "assets/ABS.dds",
        pos = vec2(42,353),
        size = vec2(74,58),
        uvStart = vec2(0, 0),
        uvEnd = vec2(1, 1)
    }
end

	local temperature = ac.getSimState().ambientTemperature

	display.text{
	text = string.format(math.round(temperature)),
	pos = temperature >= 10 and vec2(1620, 695) or vec2(1650, 695) ,
    letter = vec2(40, 80),
    font = "e92_big",
	color = rgbm(5,5,5,1), 
	width = 0.5,
    spacing = -5.0,
    alignment = 0.2
	}
	
   display.text{ 
    text = string.format("%02d:%02d", sim.timeHours, sim.timeMinutes),
	pos = vec2(349, 704),
    letter = vec2(20, 35),
    font = "e92_big",
	color = rgbm(34,34,34,2), 
	width = 0,
	spacing = -2,
	alignment = -1.0
    }

local rpmPercentage = (car.rpm / car.rpmLimiter * 100)


if shiftWarnCycle < 1 and 95 < rpmPercentage then
	shiftWarnCycle = shiftWarnCycle + 1
else
		shiftWarnCycle = 0
end

	if rpmPercentage < 65 then
 display.image {
     image = "assets/M4_LOGO.dds",
	 pos = vec2(955, 194),
     size = vec2(138,29),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 65 and rpmPercentage <= 70 then
 display.image {
     image = "assets/SHIFT/SHIFT_1.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 70 and rpmPercentage <= 75 then
 display.image {
     image = "assets/SHIFT/SHIFT_2.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 75 and rpmPercentage <= 80 then
 display.image {
     image = "assets/SHIFT/SHIFT_3.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 80 and rpmPercentage <= 85 then
 display.image {
     image = "assets/SHIFT/SHIFT_4.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 85 and rpmPercentage <= 87 then
 display.image {
     image = "assets/SHIFT/SHIFT_5.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 87 and rpmPercentage <= 90 then
 display.image {
     image = "assets/SHIFT/SHIFT_6.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 90 and rpmPercentage <= 92.5 then
 display.image {
     image = "assets/SHIFT/SHIFT_7.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 92.5 and rpmPercentage <= 93.5 then
 display.image {
     image = "assets/SHIFT/SHIFT_8.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 93.5 and rpmPercentage < 95  then
 display.image {
     image = "assets/SHIFT/SHIFT_9.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

	if rpmPercentage > 95 and shiftWarnCycle < 1 then
 display.image {
     image = "assets/SHIFT/SHIFT_10.dds",
	 pos = vec2(739, 163),
     size = vec2(569,64),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
end

  if rpmPercentage > 0 then ac.boostFrameRate() end


 display.image {
     image = "assets/around_rpm.dds",
	 pos = vec2(468,98),
     size = vec2(1110,587),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}

	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(930, 310),
    letter = vec2(180, 180),
    font = "G82_v2",
	color = rgbm(5,5,5,1), 
	width = 0.5,
    spacing = 0,
    alignment = -1.0
	}
	
 display.image {
     image = "assets/DIRVELOGIC_STUFF.dds",
     pos = vec2(1100, 380),
	 size = vec2(90,65),
     uvStart = vec2(0,0),
     uvEnd = vec2(1,1)
}
	
	    digitCoords = {
        -- define your coords here
        vec2(960 - 90, 470),
        vec2(960, 470),
        vec2(960 + 90, 470),
        vec2(960- 45, 470),
        vec2(960 + 45, 470),
    }
	
if updateDelay <= 0 then
    updateDelay = 0.45
	
	overlayText = tostring(math.floor(car.speedKmh))

  else
    updateDelay = updateDelay - dt
  end
  
    local displayspeed = tostring(math.floor(overlayText)) -- math.floor rounds to the next full number
    local speedTable = {}
    for i = 1, string.len(displayspeed) do
        speedTable[i] = displayspeed:sub(i, i)
    end
	
	    if string.len(displayspeed) == 1 then
        display.text {

            text = speedTable[1],
            pos = digitCoords[2],
			letter = vec2(120, 120),
			font = "G82_v2",
			color = rgbm(5,5,5,1), 
			width = 0.5,
			spacing = 0,
			alignment = -1.0
        }

    elseif string.len(displayspeed) == 2 then
        display.text {

            text = speedTable[2],
            pos = digitCoords[5],
			letter = vec2(120, 120),
			font = "G82_v2",
			color = rgbm(5,5,5,1), 
			width = 0.5,
			spacing = 0,
			alignment = -1.0
        }
        display.text {

            text = speedTable[1],
            pos = digitCoords[4],
			letter = vec2(120, 120),
			font = "G82_v2",
			color = rgbm(5,5,5,1), 
			width = 0.5,
			spacing = 0,
			alignment = -1.0
        }
    elseif string.len(displayspeed) == 3 then
        display.text {

            text = speedTable[1],
            pos = digitCoords[1],
			letter = vec2(120, 120),
			font = "G82_v2",
			color = rgbm(5,5,5,1), 
			width = 0.5,
			spacing = 0,
			alignment = -1.0
        }
        display.text {

            text = speedTable[2],
            pos = digitCoords[2],
			letter = vec2(120, 120),
			font = "G82_v2",
			color = rgbm(5,5,5,1), 
			width = 0.5,
			spacing = 0,
			alignment = -1.0
        }
        display.text {

            text = speedTable[3],
            pos = digitCoords[3],
			letter = vec2(120, 120),
			font = "G82_v2",
			color = rgbm(5,5,5,1), 
			width = 0.5,
			spacing = 0,
			alignment = -1.0
        }
end



end