local lrequire = require -- Remove before squishing
require = function(modname) return lrequire('content/cars/urd_darche_992_23/extension/Display/' .. modname) end -- Remove before squishing

-- Import classes
Utils = require('modules/utils')
G = require('modules/globals')
local popup = require('modules/popup')
local eventListener = require('modules/eventListener')
local delay = require('modules/delay')

INF = 1e308
Legacy = ac.getPatchVersionCode() <= 1709
if Legacy then ac.log('Running Legacy Mode!') end
---@diagnostic disable-next-line: deprecated

local simObject = Legacy and ac.getSimState() or ac.getSim();
local lastIdx = sim.currentSessionIndex
local sessionObject = ac.getSession(sim.currentSessionIndex)
ac.debug('this only gets called at game initialization')
-- this is called when session changes
math.randomseed(os.time())
math.random()
math.random()
math.random()
math.random()
math.random()

eventListener:new(function() return simObject.raceSessionType; end, function(_, newVal)
-- this is called when session changes
  if (newVal == 2) then
    -- this gets called on race session changed to qualify here you want to set any state variables you are using to appropriate qualify values
    ac.debug('qualify newVal', newVal)
    return
  end
  if (newVal == 3) then
    -- this gets called on race session changed to race here you want to set any state variables you are using to appropriate race values
    ac.debug('race newVal', newVal)
    return
  end
  return -- this return prevents memory leak when starting in practise 
end)

-- UPDATE LOOP--
---@diagnostic disable-next-line: lowercase-global
function update(dt)
  eventListener:update(); -- this actually calls the event listeners 

  if lastIdx ~= sim.currentSessionIndex then  
    sessionObject = ac.getSession(sim.currentSessionIndex)
  end
  lastIdx = sim.currentSessionIndex -- the above 4 lines update session to current session and saves it as sessionObject
  ac.debug('this gets called every 1 ms')
  G.firstrun = false;  
end 

