local flagstatus='NONE'

local backlite = ac.findNodes("carRoot:" .. car.index):findMeshes("SAS_DISPLAY"):ensureUniqueMaterials()
local on=rgb(2,2,2)
local off=rgb(0,0,0)

function update(dt)

    if flagstatus=='NONE' then
        backlite:setMaterialProperty("ksEmissive",off)
        else
        backlite:setMaterialProperty("ksEmissive",on)
    end

    -- if car.headlightsActive==true then
    --     backlite:setMaterialProperty("ksEmissive",on)
    --     else
    --     backlite:setMaterialProperty("ksEmissive",off)
    -- end

    if ac.getSim().raceFlagType==2 then
        flagstatus='YELLOW'
    elseif ac.getSim().raceFlagType==12 then
        flagstatus='BLUE'
    elseif ac.getSim().raceFlagType==1 and ac.getSim().timeToSessionStart>-5000 then
        flagstatus='GREEN'
    elseif ac.getSim().raceFlagType==5 then
        flagstatus='BLACK'
    elseif ac.getSim().raceFlagType==8 then
        flagstatus='PITS'
    elseif ac.getSim().raceFlagType==14 then
        flagstatus='WHITE'
    elseif ac.getSim().raceFlagType==13 then
        flagstatus='FINISH'
    elseif ac.getSim().raceFlagType==0 then
        flagstatus='NONE'
    else
        flagstatus='NONE'
    end
    if flagstatus=='NONE' then
        backlite:setMaterialProperty("ksEmissive",off)
        else
        backlite:setMaterialProperty("ksEmissive",on)
    end
    
    
    ui.pushDWriteFont('Segoe UI:../fonts/impact.ttf; Weight=bold; Stretch=Expanded')
    ui.setCursor(vec2(0,0))
    ac.debug('flagstatus', ac.getSim().raceFlagType)
    ac.debug('time', ac.getSim().timeToSessionStart)

if flagstatus=='YELLOW' then
    ui.drawRectFilled(vec2(0,0), vec2(404,298), rgbm(1,1,0,1))
    ui.dwriteTextAligned(flagstatus, 90, ui.Alignment.Center, ui.Alignment.Center, vec2(404,298), false, rgbm(0,0,0,1))
elseif flagstatus=='BLUE' then
    ui.drawRectFilled(vec2(0,0), vec2(404,298), rgbm(0,0,1,1))
    ui.dwriteTextAligned(flagstatus, 90, ui.Alignment.Center, ui.Alignment.Center, vec2(404,298), false, rgbm(1,1,1,1))
elseif flagstatus=='GREEN' then
    ui.drawRectFilled(vec2(0,0), vec2(404,298), rgbm(0,1,0,1))
    ui.dwriteTextAligned(flagstatus, 90, ui.Alignment.Center, ui.Alignment.Center, vec2(404,298), false, rgbm(0,0,0,1))
elseif flagstatus=='BLACK' then
    ui.drawRectFilled(vec2(0,0), vec2(404,298), rgbm(0,0,0,1))
    ui.dwriteTextAligned(flagstatus, 90, ui.Alignment.Center, ui.Alignment.Center, vec2(404,298), false, rgbm(1,1,1,1))
elseif flagstatus=='PITS' then
    ui.drawRectFilled(vec2(0,0), vec2(404,298), rgbm(1,1,1,1))
    ui.dwriteTextAligned(flagstatus, 90, ui.Alignment.Center, ui.Alignment.Center, vec2(404,298), false, rgbm(0,0,0,1))
elseif flagstatus=='WHITE' then
    ui.drawRectFilled(vec2(0,0), vec2(404,298), rgbm(1,1,1,1))
    ui.dwriteTextAligned(flagstatus, 90, ui.Alignment.Center, ui.Alignment.Center, vec2(404,298), false, rgbm(0,0,0,1))
elseif flagstatus=='FINISH' then
    ui.drawRectFilled(vec2(0,0), vec2(404,298), rgbm(0,0,0,1))

    ui.drawRectFilled(vec2(0,0), vec2(67,74), rgbm(1,1,1,1))
    ui.drawRectFilled(vec2(134,0), vec2(199,74), rgbm(1,1,1,1))
    ui.drawRectFilled(vec2(268,0), vec2(335,74), rgbm(1,1,1,1))
    
    ui.drawRectFilled(vec2(67,74), vec2(134,149), rgbm(1,1,1,1))
    ui.drawRectFilled(vec2(199,74), vec2(268,149), rgbm(1,1,1,1))
    ui.drawRectFilled(vec2(335,74), vec2(402,149), rgbm(1,1,1,1))
    
    ui.drawRectFilled(vec2(0,149), vec2(67,223), rgbm(1,1,1,1))
    ui.drawRectFilled(vec2(134,149), vec2(199,223), rgbm(1,1,1,1))
    ui.drawRectFilled(vec2(268,149), vec2(335,223), rgbm(1,1,1,1))
    
    ui.drawRectFilled(vec2(67,223), vec2(134,298), rgbm(1,1,1,1))
    ui.drawRectFilled(vec2(199,223), vec2(268,298), rgbm(1,1,1,1))
    ui.drawRectFilled(vec2(335,233), vec2(402,298), rgbm(1,1,1,1))
    


end
    


    -- display.text{ width=512, pos=vec2(0,70) , letter=vec2(50, 100), text="YELLOW", font="", alignment=0.5, spacing = 15, color = rgbm(1, 1, 1, 1) }
end