local Timer = 0
local rpm=0
local blue=rgb(0,5,255)
local purple=rgb(255,0,255)
local green=rgb(0,255,0)
local yellow=rgb(255,255,0)
local red=rgbm(255,0,0,0)
local off=rgb(0,0,0)
local limiter
local Slip_lut=ac.DataLUT11.load("Slip_Indicator.lut")
local SlipLED={}
local RPMLED={}
local BlinkerLLED
local BlinkerRLED
local PitLED
local ABSLED
local TCLED
local FuelLED
local LightLED
local PREDPLUSLED
local PREDMINUSLED
local INITIALIZED = false


local function initialize()
    INITIALIZED = true
    for i = 0,3 do
        SlipLED[i] = ac.findNodes("carRoot:" .. car.index):findMeshes("LEDSLIP_"..i):ensureUniqueMaterials()
    end
    
    for i = 0,4 do
        RPMLED[i]=ac.findNodes("carRoot:" .. car.index):findMeshes("RPMLED_"..i):ensureUniqueMaterials()
    end
    BlinkerLLED = ac.findNodes("carRoot:" .. car.index):findMeshes("LEDSLIP_0"):ensureUniqueMaterials()
    BlinkerRLED = ac.findNodes("carRoot:" .. car.index):findMeshes("LEDSLIP_2"):ensureUniqueMaterials()
    FuelLED = ac.findNodes("carRoot:" .. car.index):findMeshes("LEDFUEL"):ensureUniqueMaterials()
    LightLED = ac.findNodes("carRoot:" .. car.index):findMeshes("LEDLIGHT"):ensureUniqueMaterials()
    -- PitLED = ac.findNodes("carRoot:" .. car.index):findMeshes("LEDPIT"):ensureUniqueMaterials()
    -- ABSLED = ac.findNodes("carRoot:" .. car.index):findMeshes("LED_10"):ensureUniqueMaterials()
    -- TCLED = ac.findNodes("carRoot:" .. car.index):findMeshes("LED_11"):ensureUniqueMaterials()
    PREDPLUSLED = ac.findNodes("carRoot:" .. car.index):findMeshes("racelogic_led0"):ensureUniqueMaterials()
    PREDMINUSLED = ac.findNodes("carRoot:" .. car.index):findMeshes("racelogic_led1"):ensureUniqueMaterials()
    
end

function slipindicator()
    local tc=false 
    ac.debug('tc', tc)

    if car.isInPitlane or car.manualPitsSpeedLimiterEnabled then
        for i = 0,3 do
            if (math.floor((Timer*8)%2)) == 0 then
                SlipLED[i]:setMaterialProperty("ksEmissive",purple)
            else
                SlipLED[i]:setMaterialProperty("ksEmissive",off)
            end
        end
    else
        if car.tractionControlInAction==true then
            for i = 0,3 do
                SlipLED[i]:setMaterialProperty("ksEmissive",blue)
            end
        else
            for i = 0,3 do
                SlipLED[i]:setMaterialProperty("ksEmissive",off)
            end 
        end
        if car.brake > 0.05 then
            for i = 0,3 do
                if car.wheels[0].speedDifference > Slip_lut:get(i) then
                    SlipLED[i]:setMaterialProperty("ksEmissive",red)
                end
            end
        end
    end

end

function rpmindicator(dt)
    rpm=car.rpm
    limiter=car.isEngineLimiterOn
    
    if rpm>7200 or limiter==true then
        if (math.floor((Timer*8)%2)) == 0 then
            for i = 0,4 do
                RPMLED[i]:setMaterialProperty("ksEmissive",red)
            end
        else
            for i = 0,4 do
                RPMLED[i]:setMaterialProperty("ksEmissive",off)
            end
        end
    else

        if rpm>6000 then 
            RPMLED[0]:setMaterialProperty("ksEmissive",green)
        else
            RPMLED[0]:setMaterialProperty("ksEmissive",off)
        end
        
        if rpm>6300 then
            RPMLED[1]:setMaterialProperty("ksEmissive",green)
        else
            RPMLED[1]:setMaterialProperty("ksEmissive",off)
        end
        
        if rpm>6600 then
            RPMLED[2]:setMaterialProperty("ksEmissive",yellow)
        else
            RPMLED[2]:setMaterialProperty("ksEmissive",off)
        end
        
        if rpm>6800 then
            RPMLED[3]:setMaterialProperty("ksEmissive",yellow)
        else
            RPMLED[3]:setMaterialProperty("ksEmissive",off)
        end
        
        if rpm>7000 then
            RPMLED[4]:setMaterialProperty("ksEmissive",red)
        else
            RPMLED[4]:setMaterialProperty("ksEmissive",off)
        end
        
        

    end
end

function predindicator()
    if car.performanceMeter<0 then
        PREDMINUSLED:setMaterialProperty("ksEmissive",green)
    else
        PREDMINUSLED:setMaterialProperty("ksEmissive",off)
    end

    if car.performanceMeter>0 then
        PREDPLUSLED:setMaterialProperty("ksEmissive",red)
    else
        PREDPLUSLED:setMaterialProperty("ksEmissive",off)
    end
    
end

function update(dt)
    
    if not INITIALIZED then initialize() end

    Timer = Timer > 1000 and 0 or Timer + dt
    if car.headlightsActive then
        blue=rgb(0,5,20)
        green=rgb(0,20,0)
        yellow=rgb(20,20,0)
        red=rgb(20,0,0)
        purple=rgb(20,0,20)
    else
        blue=rgb(0,5,50)
        green=rgb(0,50,0)
        yellow=rgb(50,50,0)
        red=rgb(50,0,0)
        purple=rgb(50,0,50)
    end

    if car.turningLeftLights==true then
        if car.turningLightsActivePhase==true then
            BlinkerLLED:setMaterialProperty("ksEmissive",green)
        else
            BlinkerLLED:setMaterialProperty("ksEmissive",off)
        end
    else
        BlinkerLLED:setMaterialProperty("ksEmissive",off)
    end

    if car.turningRightLights==true then
        if car.turningLightsActivePhase==true then
            BlinkerRLED:setMaterialProperty("ksEmissive",green)
            else
            BlinkerRLED:setMaterialProperty("ksEmissive",off)
        end
    else
            BlinkerRLED:setMaterialProperty("ksEmissive",off)
    end

    if car.fuel<10 then
        FuelLED:setMaterialProperty("ksEmissive",red)
        else
        FuelLED:setMaterialProperty("ksEmissive",off)
    end

    -- if car.isInPitlane==true or car.manualPitsSpeedLimiterEnabled==true then
    --     PitLED:setMaterialProperty("ksEmissive",purple)
    --     else
    --     PitLED:setMaterialProperty("ksEmissive",off)
    -- end

    if car.headlightsActive==true then
        LightLED:setMaterialProperty("ksEmissive",blue)
        else
        LightLED:setMaterialProperty("ksEmissive",off)
    end

    -- if car.absInAction==true then
    --     ABSLED:setMaterialProperty("ksEmissive",red)
    --     else
    --     ABSLED:setMaterialProperty("ksEmissive",off)
    -- end

    -- if car.tractionControlInAction==true then
    --         TCLED:setMaterialProperty("ksEmissive",blue)
    --     else
    --         TCLED:setMaterialProperty("ksEmissive",off)
    -- end

    predindicator()
    slipindicator()
    rpmindicator()

end