--https://www.geeksforgeeks.org/how-to-change-an-element-color-based-on-value-of-the-color-picker-value-using-onclick/
--sorry for this attrocious code
local M = {}
M.type = "auxiliary"
M.relevantDevice = nil

local htmlTexture = require("htmlTexture")

local gaugesScreenName = nil
local htmlPath = nil

local updateTimer = 0
local updateFPS = 10
local invFPS = 1 / updateFPS

local rpm = 0
local fuel = 0
local oiltemp = 0
local watertemp = 0
local speedMin = 0.1 
local boost = 0
local airspeed = 0
local throttle_input= 0
local throttle= 0
local updateUnderscoreTimer = 0
local updateTopspeed = 0
local convertedAirspeed = 0
local afr = 0
local device = nil
local torque = 0
local AFRupdate = 0
local timerAcceleration = 0
local speedValue = -69
local speedValuemet = -69
local accelValue = "--"
local underscoreValue = "_"
local timerStarted = false

local function toggleUnderscore()
  if underscoreValue == "_" then
	underscoreValue = " "
  else
	underscoreValue = "_"
  end
  updateUnderscoreTimer = 0
end

local function saveAcceleration()

  if accelValue == "--" then
	accelValue = timerAcceleration
	
  elseif accelValue >= timerAcceleration then
	accelValue = timerAcceleration
  end
	timerStarted = false
	timerAcceleration = 0
end

local function saveTopspeed()
  convertedAirspeed = airspeed * 3.6

  if speedValue == -69 and convertedAirspeed >= 120 then
	speedValuemet = convertedAirspeed
	speedValue = airspeed * 2.23694

  elseif speedValuemet <= convertedAirspeed then
	speedValuemet = convertedAirspeed
	speedValue = airspeed * 2.23694
  end
	updateTopspeed = 0
end

local function updateGFX(dt)

    local data = {}

 	data.topspeedLaptop = speedValue
	data.topspeedmetLaptop = speedValuemet
	data.accelerationLaptop = accelValue
	data.underscoreLaptop = underscoreValue
	
  AFRupdate = AFRupdate + dt		--for afr calculration
  updateTimer = updateTimer + dt
--I hate this code
  if electrics.values["rpm"] == nil then
	rpm = "--"
  else
	rpm = electrics.values["rpm"]
  end

  if electrics.values["fuel"] == nil then
	fuel = "--"
  else
	fuel = electrics.values["fuel"]
  end

  if electrics.values["oiltemp"] == nil then
	oiltemp = "--"
  else
	oiltemp = electrics.values["oiltemp"]
  end
  
  if electrics.values["watertemp"] == nil then
	watertemp = "--"
  else
	watertemp = electrics.values["watertemp"]
  end

  if electrics.values["turboBoost"] == nil then
	boost = "--"
  else
	boost = electrics.values["turboBoost"]
  end

  if electrics.values["airspeed"] == nil then
	airspeed = "--"
  else
	airspeed = electrics.values["airspeed"]
  end
  
   updateUnderscoreTimer = updateUnderscoreTimer + dt	--toggle the underscore in the console becuz why not
  if updateUnderscoreTimer >= 0.7 then
	toggleUnderscore()
  end
  
 	--acceleration timer logic
	
	throttle_input= electrics.values["throttle_input"]

  if throttle_input>= 0.8 and airspeed > speedMin and airspeed < 0.5 then
	timerStarted = true
  elseif throttle_input< 0.8 then
	timerStarted = false
  end

  if timerStarted == true then
	timerAcceleration = timerAcceleration + dt
	if airspeed >= 27.7778 then
		saveAcceleration()
	end
  else
	timerAcceleration = 0
  end
  
	--top speed data logic

	updateTopspeed = updateTopspeed + dt

  if updateTopspeed >= 3 then
	saveTopspeed()
  end
  
  if updateTimer > invFPS then
  
	data.rpmLaptop = rpm	--self explanatory
	
	data.oiltempmetLaptop = oiltemp				--°C

	data.watertempmetLaptop = watertemp				--°C
	
	data.turboboostLaptop = boost					--psi

  if fuel == "--" then			--stuff just keeps erroring so I guess I have to check a million times to be extra extra sure that nothing is nil...
	data.fuelLaptop = "--"
  else
	data.fuelLaptop = fuel * 100	--fuel in %
  end
  
  if oiltemp == "--" then
	data.oiltempLaptop = "--"
  else
	data.oiltempLaptop = (oiltemp * 1.8 + 32) --to wrong temperature	
  end

 if watertemp == "--" then
	data.watertempLaptop = "--"
  else
	data.watertempLaptop = (watertemp * 1.8 + 32) --to wrong temperature
  end

 if airspeed == "--" then
	data.airspeedmetLaptop = "--"
  else
	data.airspeedmetLaptop = airspeed * 3.6 	--to km/h, correct units
  end

 if airspeed == "--" then
	data.airspeedLaptop = "--"
  else
	data.airspeedLaptop = airspeed * 2.23694	--to mph
  end

 if boost == "--" then
	data.turboboostmetLaptop = "--"
  else
	data.turboboostmetLaptop = boost * 0.0689476	--psi to BAR
  end
  
	--AFR logic
  
   if device == nil or device.maxTorqueRating == nil or electrics.values["engineRunning"] == 0 then
	afr = "--"
	data.afrLaptop = afr
    htmlTexture.call(gaugesScreenName, "update", data)
    updateTimer = 0
   return end -- prevent lua error when engine removed, prevent afr from being generated when there is no combustion engine / engine is off
   
	torque = device.combustionTorque
  
 	throttle = electrics.values["throttle"]

  
  if AFRupdate >= 0.5 then
	if throttle <= 0.1 then throttle = 0.1 end
	if torque <= 100 then torque = 100 end
	if torque >= 1000 then torque = 1000 end	--limit torque and throttle input
	if rpm >= 20000 then rpm = 20000 end 
	if rpm <= 500 then rpm = 500 end 

	afr = 14.7 - (throttle / 0.25) - (torque / 200)  + ( math.random (-2, 4) / (10 + (rpm / 500)))
    
	AFRupdate = 0
  end
  
  	data.afrLaptop = afr
  
    --dump(data)
    htmlTexture.call(gaugesScreenName, "update", data)
    updateTimer = 0
  end
end

local function init(jbeamData)
	gaugesScreenName = jbeamData.materialName
	htmlPath = jbeamData.htmlPath
	local width = jbeamData.textureWidth or 1024
	local height = jbeamData.textureHeight or 1024

    device = powertrain.getDevice("mainEngine")

    htmlTexture.create(gaugesScreenName, htmlPath, width, height, updateFPS, "automatic")
    htmlTexture.call(gaugesScreenName, "init")
end

local function reset() 
    speedValue = -69
    speedValuemet = -69
    accelValue = "--"
    underscoreValue = "_"
	
	timerStarted = false
	AFRupdate = 0
	updateTopspeed = 0
	timerAcceleration = 0
	updateUnderscoreTimer = 0
end

M.reset = reset
M.init = init
M.updateGFX = updateGFX

return M
