local modeSelect = 0
local modeMaxIndex = 4
local screen = "GEO_INT_Racelogic_Screen"
local vMax = 0

function rotateToggle(input,maxVal)
	if(input > maxVal) then
		return 0
	else
		return input
	end
end

function clickCheck()
	if ac.isMeshClicked(screen,vec2(0,0), vec2(1024,256), inCarPos, inCarRadius, inCarLocalPos) then
		modeSelect = modeSelect + 1
	end
end	

function logMaxSpeed()
	if car.speedKmh > vMax then
		vMax = car.speedKmh
	end
end
function padDelta(inputDelta)
	if inputDelta < 0 then
		if math.abs(inputDelta) >= 10 then
			return string.format("%.2f",inputDelta)
		else
			return string.format(" %.2f",inputDelta)
		end
	else
		if math.abs(inputDelta) >= 10 then
			return string.format(" %.2f",inputDelta)
		else
			return string.format("  %.2f",inputDelta)
		end
	end
end
function padSpeed(speed)
	if speed > 99 then
		return string.format("%d",speed)
	elseif speed > 9 then
		return string.format(" %d",speed)
	else
		return string.format("  %d",speed)
	end
end
--current speed
function screen0()
	display.text{
		text = "km/h",
		pos = vec2(825,170),
		letter = vec2(40,80),
		font = "vbox",
		width = 1,
		alignment = 0
	}
	display.text{
		text = padSpeed(car.speedKmh),
		pos = vec2(140,-10),
		letter = vec2(170,300),
		font = "vbox",
		width = 1,
		alignment = 10,
		spacing = -5
	}
end
--maximum speed in session
function screen1()
	display.text{
		text = "max",
		pos = vec2(850,10),
		letter = vec2(40,80),
		font = "vbox",
		width = 1,
		alignment = 0,
		spacing = 2
	}
	display.text{
		text = "km/h",
		pos = vec2(825,170),
		letter = vec2(40,80),
		font = "vbox",
		width = 1,
		alignment = 0
	}
	display.text{
		text = padSpeed(vMax),
		pos = vec2(140,-10),
		letter = vec2(170,300),
		font = "vbox",
		width = 1,
		alignment = 10,
		spacing = -5
	}
end
--current laptime
function screen2()
	display.text{
		text = ac.lapTimeToString(car.lapTimeMs, false), 
		pos = vec2(100,30),
		letter = vec2(100,200),
		font = "vbox",
		width = 2,
		alignment = 0,
		spacing = -20
	}
	display.text{
		text = "current", 
		pos = vec2(795,6),
		letter = vec2(25,50),
		font = "vbox",
		width = 2,
		alignment = 0,
		spacing = -3
	}
	display.text{
		text = "lap time", 
		pos = vec2(790,50),
		letter = vec2(26,50),
		font = "vbox",
		width = 2,
		alignment = 0,
		spacing = -1
	}
end
--best laptime
function screen3()
	display.text{
		text = ac.lapTimeToString(car.bestLapTimeMs, false), 
		pos = vec2(100,30),
		letter = vec2(100,200),
		font = "vbox",
		width = 2,
		alignment = 0,
		spacing = -30
	}
	display.text{
		text = "best", 
		pos = vec2(794,5),
		letter = vec2(28,51),
		font = "vbox",
		width = 2,
		alignment = 0,
		spacing = -3
	}
	display.text{
		text = "lap time", 
		pos = vec2(790,50),
		letter = vec2(26,50),
		font = "vbox",
		width = 2,
		alignment = 0,
		spacing = -1
	}
end
--delta
function screen4()
	display.text{
		
		text = padDelta(math.clamp(car.performanceMeterSpeedDifferenceMs,-10,10),1), 
				--text = string.format("%.2f",math.clamp(car.performanceMeterSpeedDifferenceMs,-10,10),1), 
		pos = vec2(280,10),
		letter = vec2(65,140),
		font = "vbox",
		width = 2,
		alignment=0,
		spacing = -2
	}
	display.rect{
		pos = vec2(500,148),
		size = vec2(450*math.clamp(car.performanceMeterSpeedDifferenceMs,-10,10)/10,70),
		color = rgbm(10,10,10,10)
	}
end
--the actual logic
function update(dt)
	clickCheck()
	 modeSelect = rotateToggle(modeSelect,modeMaxIndex)
	 logMaxSpeed()
	--CONVERT IT TO SWITCHCASE YOU MOTHERFUCKER
	if modeSelect == 0 then
		screen0()
	elseif modeSelect == 1 then
		screen1()
	elseif modeSelect == 2 then
		screen2()
	elseif modeSelect == 3 then
		screen3()
	elseif modeSelect == 4 then
		screen4()
	end
end