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 FuelLED
local LightLED
local INITIALIZED = false

local function initialize()
    INITIALIZED = true
    for i = 0,3 do
        SlipLED[i] = ac.findNodes("carRoot:" .. car.index):findMeshes("SLIP_LED_"..i):ensureUniqueMaterials()
    end
    for i = 0,4 do
        RPMLED[i]=ac.findNodes("carRoot:" .. car.index):findMeshes("LED_"..i):ensureUniqueMaterials()
    end
    FuelLED = ac.findNodes("carRoot:" .. car.index):findMeshes("FUEL_LED"):ensureUniqueMaterials()

    LightLED = ac.findNodes("carRoot:" .. car.index):findMeshes("LIGHT_ON"):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>6865 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>5590 then 
            RPMLED[0]:setMaterialProperty("ksEmissive",green)
        else
            RPMLED[0]:setMaterialProperty("ksEmissive",off)
        end
        
        if rpm>6000 then
            RPMLED[1]:setMaterialProperty("ksEmissive",green)
        else
            RPMLED[1]:setMaterialProperty("ksEmissive",off)
        end
        
        if rpm>6200 then
            RPMLED[2]:setMaterialProperty("ksEmissive",yellow)
        else
            RPMLED[2]:setMaterialProperty("ksEmissive",off)
        end
        
        if rpm>6400 then
            RPMLED[3]:setMaterialProperty("ksEmissive",yellow)
        else
            RPMLED[3]:setMaterialProperty("ksEmissive",off)
        end
        
        if rpm>6600 then
            RPMLED[4]:setMaterialProperty("ksEmissive",red)
        else
            RPMLED[4]:setMaterialProperty("ksEmissive",off)
        end
        
        

    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.fuel<10 then
        FuelLED:setMaterialProperty("ksEmissive",red)
        else
        FuelLED:setMaterialProperty("ksEmissive",off)
    end

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

    slipindicator()
    rpmindicator()

end