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 eventListener = require('modules/eventListener')
local delay = require('modules/delay')
local carstate = {
  screen = 1 -- defines the first page upon loading
}


math.randomseed(os.time())
math.random()
math.random()
math.random()
math.random()
math.random()

Legacy = ac.getPatchVersionCode() <= 1709
if Legacy then ac.log('Running Legacy Mode!') end
eventListener:new(function() return car.extraB; end, function(diff)
-- this is the button pressed event handler
  if Legacy == false then
    if carstate.screen == 0 then carstate.screen = 1     
    elseif carstate.screen == 1 then carstate.screen = 0  
    end 
  end
end)


-- reimplementing method because of other font
Utils.displayText = function(text, posx, posy, size, font, color, align)
  local lastx = posx

  local digitswidth = 22
  local symbolwidth = 19
  local alphabeticwidth = 32

  local alignmentoffset = (align == 'left' and 0 or
                            (select(2, string.gsub(text, '%d', '')) * digitswidth +
                              select(2, string.gsub(text, '%a', '')) * alphabeticwidth +
                              (string.len(text) - select(2, string.gsub(text, '%d', ''))) * 10) /
                            (align == 'center' and 2 or 1)) * size

  for i = 1, string.len(text) do
    local numeric = string.find(string.sub(text, i, i), '%d') and true or false
    local alphabetic = string.find(string.sub(text, i, i), '%a') and true or false

    display.text({
      text = string.sub(text, i, i),
      pos = vec2((numeric and lastx or
                   (alphabetic and lastx + (size ^ ((1 - size) / 8 + 1) * 10) - 5 or lastx + size ^
                     ((1 - size) / 8 + 0.6) * 10)) - alignmentoffset,
                 numeric and posy or (alphabetic and posy or posy + size * 1)),
      letter = vec2(numeric and 38 * size or (alphabetic and 55 * size or 29 / 1 * size),
                    numeric and 70 * size or (alphabetic and 84 * size or 112 / 1.65 * size)),
      color = rgbm(color[1], color[2], color[3], 1),
      font = font,
      width = 50,
      alignment = 0,
      spacing = -1
    })
    if numeric and string.find(string.sub(text, i + 1, i + 1), '%d') then
      lastx = lastx + size * digitswidth
    elseif alphabetic and string.find(string.sub(text, i + 1, i + 1), '%a') then
      lastx = lastx + size * alphabeticwidth
    else
      lastx = lastx + size * symbolwidth
    end
  end
end

local debugmessge = function(msg, desc) ac.setSystemMessage(msg, desc or math.random()) end

-- UPDATE LOOP--
---@diagnostic disable-next-line: lowercase-global
function update(dt)
    eventListener:update();
    if car.isInPitlane then
      display.image({image = 'Display/texRL.zip::Page_Speed.dds', pos = vec2(0, 0), size = vec2(1024, 256)})
      pcall(function()
        Utils.displayText(Utils.dec(car.speedKmh > 0.1 and car.speedKmh or 0, 2, '.'), 700, 0, 4.3, 'bdp_fox', {1, 1, 1},
                          'right')
      end)
    else
     if carstate.screen == 1 then
            display.image({image = 'Display/texRL.zip::Page_Laptime.dds', pos = vec2(0, 0), size = vec2(1024, 256)})
              pcall(function()
                local textToDisplay = Utils.timeformatZeros(car.lapTimeMs,{':', '.'},2,true)
                Utils.displayText(
                    textToDisplay, 
                    700, 0, 4.3, 'bdp_fox', {1, 1, 1}, 'right')
              end)
        Utils.displayText(car.lapCount, 920, 100, 2, 'bdp_fox', {1, 1, 1}, 'center')
     elseif carstate.screen == 0 then
        display.image({image = 'Display/texRL.zip::Page_Delta.dds', pos = vec2(0, 0), size = vec2(1024, 256)})
        Utils.displayText((car.performanceMeter == 0 and '  ' or (car.performanceMeter >= 0 and '+ ' or '- ')) ..
                            Utils.dec(math.abs(math.clampN(car.performanceMeter, -99, 99)), 3, '.'), 400, -10, 2.4, 'bdp_fox',
        
                          {1, 1, 1}, 'center')
        -- This below is 1 second for each direction, the one below is 2 seconds in either direction.       
        -- display.rect({
        --   pos = vec2(500, 130),
        --   size = vec2(430 * math.clampN(car.performanceMeter, -1, 1), 54),
        --   color = rgb(1, 1, 1)
        -- })
        display.rect(
          {
          pos = vec2(500, 160),
          size = vec2((430/2) * math.clampN(car.performanceMeter, -2, 2), 90),
          color = rgb(1, 1, 1)
          })
      end
  end
end