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 displayGreen = true -- sets initial state of green flag display to true
local simObject = Legacy and ac.getSimState() or ac.getSim();
local tempRaceFlagType = simObject.raceFlagType
local isInRace = (simObject.raceSessionType == 3)
local isOnFinalLap = false
local p1 = vec2(0, 0)
local p2 = vec2(260, 260)
local leader = nil
local lastIdx = sim.currentSessionIndex
local sessionObject = ac.getSession(sim.currentSessionIndex)

-- 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
    isInRace = false;
    displayGreen = false;
    isOnFinalLap = false;
    return
  end
  if (newVal == 3) then
    isInRace = true;
    displayGreen = true;
    return
  end
  return
end)

-- UPDATE LOOP--
---@diagnostic disable-next-line: lowercase-global
function update(dt)
  eventListener:update();

  if lastIdx ~= sim.currentSessionIndex then  
    sessionObject = ac.getSession(sim.currentSessionIndex)
  end
  lastIdx = sim.currentSessionIndex

  if simObject.raceSessionType == 3 then
    isInRace = true
  end

  carcount=simObject.carsCount-1
  for i=0,carcount do
    aCar=ac.getCar(i)
    if aCar.racePosition == 1 then
      leader = aCar
    end
  end

  if isInRace == true and isOnFinalLap == false then
    isOnFinalLap = (sessionObject.laps - leader.lapCount == 1)
  end

  if isOnFinalLap == true and tempRaceFlagType == 13 then
    isOnFinalLap = false
  end

  if car.lapCount == 0 and car.lapTimeMs > 20000 then
    displayGreen = false
  end
  
  if tempRaceFlagType ~= simObject.raceFlagType then
    tempRaceFlagType = simObject.raceFlagType
  end

  if tempRaceFlagType == 0 then  
      display.image({image = 'Display/texDisp.zip::flags/0.dds', pos = p1, size = p2})
  end

  if tempRaceFlagType == 1 then
    if displayGreen == true then
      display.image({image = 'Display/texDisp.zip::flags/1.dds', pos = p1, size = p2})
    end 
    if displayGreen == false then
      display.image({image = 'Display/texDisp.zip::flags/0.dds', pos = p1, size = p2})
    end
  end 

  if tempRaceFlagType == 2 then
      display.image({image = 'Display/texDisp.zip::flags/2.dds', pos = p1, size = p2})
  end

  if tempRaceFlagType == 3 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.Slippery then flag, color = '/extension/textures/flags/slippery.png', rgbm.colors.white
  end

  if tempRaceFlagType == 4 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.PitLaneClosed then return
  end

  if tempRaceFlagType == 5 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.Stop then flag, color = '/content/gui/flags/blackFlag_small.png', rgbm.colors.white
  end

  if tempRaceFlagType == 6 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.SlowVehicle then flag, color = '/content/gui/flags/whiteFlag.png', rgbm(0.8, 0.8, 0.8, 1)
  end

  if tempRaceFlagType == 7 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.Ambulance then flag, color = '/extension/textures/flags/ambulance.png', rgbm.colors.white
  end

  if tempRaceFlagType == 8 then 
    display.image({image = 'Display/texDisp.zip::flags/8.dds', pos = p1, size = p2})
  end

  if tempRaceFlagType == 9 then ac.debug('flag', tempRaceFlagType)
     -- ac.FlagType.MechanicalFailure then flag, color = '/extension/textures/flags/mechanical_failure.png', rgbm.colors.white
  end

  if tempRaceFlagType == 10 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.Unsportsmanlike then flag, color = '/extension/textures/flags/unsportsmanlike.png', rgbm.colors.white
  end

  if tempRaceFlagType == 11 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.StopCancel then return
  end

  if tempRaceFlagType == 12 then 
      display.image({image = 'Display/texDisp.zip::flags/12.dds', pos = p1, size = p2})
  end

  if tempRaceFlagType == 13 then
      display.image({image = 'Display/texDisp.zip::flags/13.dds', pos = p1, size = p2})
  end

  if tempRaceFlagType == 14 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.OneLapLeft then flag, color = '/content/gui/flags/whiteFlag.png', rgbm.colors.white
  end

  if tempRaceFlagType == 15 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.SessionSuspended then flag, color = '/content/gui/flags/whiteFlag.png', rgbm.colors.red
  end

  if tempRaceFlagType == 16 then ac.debug('flag', tempRaceFlagType)
    -- ac.FlagType.Code60 then flag, color = '/extension/textures/flags/code60.png', rgbm.colors.white
  end

  if isOnFinalLap == true and tempRaceFlagType ~= 12 then
    display.image({image = 'Display/texDisp.zip::flags/14.dds', pos = p1, size = p2}) 
  end
  G.firstrun = false;  
end 

