local M = {}
local on_timerL = 0
local on_timerR = 0
local off_timerL = 0
local off_timerR = 0
local cornerlightL_active = false
local cornerlightR_active = false

local function smoothlights(dt)
    -- left cornerlight
	if electrics.values.cornerlight_L == 1 then
        off_timerL = 0
        cornerlightL_active = true
        on_timerL = on_timerL + dt
        if on_timerL >= 0.35 then
            electrics.values.cornerlight_L6 = 1
            electrics.values.cornerlight_L5 = 0
            electrics.values.cornerlight_L4 = 0
            electrics.values.cornerlight_L3 = 0
            electrics.values.cornerlight_L2 = 0
            electrics.values.cornerlight_L1 = 0
        elseif on_timerL >= 0.28 then
            electrics.values.cornerlight_L5 = 1
            electrics.values.cornerlight_L6 = 0
            electrics.values.cornerlight_L4 = 0
            electrics.values.cornerlight_L3 = 0
            electrics.values.cornerlight_L2 = 0
            electrics.values.cornerlight_L1 = 0
        elseif on_timerL >= 0.21 then
            electrics.values.cornerlight_L4 = 1
            electrics.values.cornerlight_L6 = 0
            electrics.values.cornerlight_L5 = 0
            electrics.values.cornerlight_L3 = 0
            electrics.values.cornerlight_L2 = 0
            electrics.values.cornerlight_L1 = 0
        elseif on_timerL >= 0.14 then
            electrics.values.cornerlight_L3 = 1
            electrics.values.cornerlight_L6 = 0
            electrics.values.cornerlight_L5 = 0
            electrics.values.cornerlight_L4 = 0
            electrics.values.cornerlight_L2 = 0
            electrics.values.cornerlight_L1 = 0
        elseif on_timerL >= 0.07 then
            electrics.values.cornerlight_L2 = 1
            electrics.values.cornerlight_L6 = 0
            electrics.values.cornerlight_L5 = 0
            electrics.values.cornerlight_L4 = 0
            electrics.values.cornerlight_L3 = 0
            electrics.values.cornerlight_L1 = 0
        elseif on_timerL >= 0 then
            electrics.values.cornerlight_L1 = 1
            electrics.values.cornerlight_L6 = 0
            electrics.values.cornerlight_L5 = 0
            electrics.values.cornerlight_L4 = 0
            electrics.values.cornerlight_L3 = 0
            electrics.values.cornerlight_L2 = 0
        end
    else
        on_timerL = 0
        if cornerlightL_active then
            off_timerL = off_timerL + dt
            if off_timerL >= 0.35 then
				electrics.values.cornerlight_L1 = 0
				electrics.values.cornerlight_L6 = 0
				electrics.values.cornerlight_L5 = 0
				electrics.values.cornerlight_L4 = 0
				electrics.values.cornerlight_L3 = 0
				electrics.values.cornerlight_L2 = 0
            elseif off_timerL >= 0.28 then
				electrics.values.cornerlight_L2 = 1
				electrics.values.cornerlight_L6 = 0
				electrics.values.cornerlight_L5 = 0
				electrics.values.cornerlight_L4 = 0
				electrics.values.cornerlight_L3 = 0
				electrics.values.cornerlight_L1 = 0
            elseif off_timerL >= 0.21 then
				electrics.values.cornerlight_L3 = 1
				electrics.values.cornerlight_L6 = 0
				electrics.values.cornerlight_L5 = 0
				electrics.values.cornerlight_L4 = 0
				electrics.values.cornerlight_L2 = 0
				electrics.values.cornerlight_L1 = 0
            elseif off_timerL >= 0.14 then
				electrics.values.cornerlight_L4 = 1
				electrics.values.cornerlight_L6 = 0
				electrics.values.cornerlight_L5 = 0
				electrics.values.cornerlight_L3 = 0
				electrics.values.cornerlight_L2 = 0
				electrics.values.cornerlight_L1 = 0
            elseif off_timerL >= 0.07 then
				electrics.values.cornerlight_L5 = 1
				electrics.values.cornerlight_L6 = 0
				electrics.values.cornerlight_L4 = 0
				electrics.values.cornerlight_L3 = 0
				electrics.values.cornerlight_L2 = 0
				electrics.values.cornerlight_L1 = 0
            elseif off_timerL >= 0 then
				electrics.values.cornerlight_L6 = 1
				electrics.values.cornerlight_L5 = 0
				electrics.values.cornerlight_L4 = 0
				electrics.values.cornerlight_L3 = 0
				electrics.values.cornerlight_L2 = 0
				electrics.values.cornerlight_L1 = 0
                if electrics.values.cornerlight_L1 == 0 and electrics.values.cornerlight_L2 == 0 and electrics.values.cornerlight_L3 == 0 and electrics.values.cornerlight_L4 == 0 and electrics.values.cornerlight_L5 == 0 and electrics.values.cornerlight_L6 == 0 then
                    cornerlightL_active = false
                    off_timerL = 0
                end
            end
        else
            electrics.values.cornerlight_L1 = 0
            electrics.values.cornerlight_L2 = 0
            electrics.values.cornerlight_L3 = 0
            electrics.values.cornerlight_L4 = 0
            electrics.values.cornerlight_L5 = 0
            electrics.values.cornerlight_L6 = 0
        end
    end
	-- right cornerlight
	if electrics.values.cornerlight_R == 1 then
        off_timerR = 0
        cornerlightR_active = true
        on_timerR = on_timerR + dt
        if on_timerR >= 0.35 then
            electrics.values.cornerlight_R6 = 1
            electrics.values.cornerlight_R5 = 0
            electrics.values.cornerlight_R4 = 0
            electrics.values.cornerlight_R3 = 0
            electrics.values.cornerlight_R2 = 0
            electrics.values.cornerlight_R1 = 0
        elseif on_timerR >= 0.28 then
            electrics.values.cornerlight_R5 = 1
            electrics.values.cornerlight_R6 = 0
            electrics.values.cornerlight_R4 = 0
            electrics.values.cornerlight_R3 = 0
            electrics.values.cornerlight_R2 = 0
            electrics.values.cornerlight_R1 = 0
        elseif on_timerR >= 0.21 then
            electrics.values.cornerlight_R4 = 1
            electrics.values.cornerlight_R6 = 0
            electrics.values.cornerlight_R5 = 0
            electrics.values.cornerlight_R3 = 0
            electrics.values.cornerlight_R2 = 0
            electrics.values.cornerlight_R1 = 0
        elseif on_timerR >= 0.14 then
            electrics.values.cornerlight_R3 = 1
            electrics.values.cornerlight_R6 = 0
            electrics.values.cornerlight_R5 = 0
            electrics.values.cornerlight_R4 = 0
            electrics.values.cornerlight_R2 = 0
            electrics.values.cornerlight_R1 = 0
        elseif on_timerR >= 0.07 then
            electrics.values.cornerlight_R2 = 1
            electrics.values.cornerlight_R6 = 0
            electrics.values.cornerlight_R5 = 0
            electrics.values.cornerlight_R4 = 0
            electrics.values.cornerlight_R3 = 0
            electrics.values.cornerlight_R1 = 0
        elseif on_timerR >= 0 then
            electrics.values.cornerlight_R1 = 1
            electrics.values.cornerlight_R6 = 0
            electrics.values.cornerlight_R5 = 0
            electrics.values.cornerlight_R4 = 0
            electrics.values.cornerlight_R3 = 0
            electrics.values.cornerlight_R2 = 0
        end
    else
        on_timerR = 0
        if cornerlightR_active then
            off_timerR = off_timerR + dt
            if off_timerR >= 0.35 then
				electrics.values.cornerlight_R1 = 0
				electrics.values.cornerlight_R6 = 0
				electrics.values.cornerlight_R5 = 0
				electrics.values.cornerlight_R4 = 0
				electrics.values.cornerlight_R3 = 0
				electrics.values.cornerlight_R2 = 0
            elseif off_timerR >= 0.28 then
				electrics.values.cornerlight_R2 = 1
				electrics.values.cornerlight_R6 = 0
				electrics.values.cornerlight_R5 = 0
				electrics.values.cornerlight_R4 = 0
				electrics.values.cornerlight_R3 = 0
				electrics.values.cornerlight_R1 = 0
            elseif off_timerR >= 0.21 then
				electrics.values.cornerlight_R3 = 1
				electrics.values.cornerlight_R6 = 0
				electrics.values.cornerlight_R5 = 0
				electrics.values.cornerlight_R4 = 0
				electrics.values.cornerlight_R2 = 0
				electrics.values.cornerlight_R1 = 0
            elseif off_timerR >= 0.14 then
				electrics.values.cornerlight_R4 = 1
				electrics.values.cornerlight_R6 = 0
				electrics.values.cornerlight_R5 = 0
				electrics.values.cornerlight_R3 = 0
				electrics.values.cornerlight_R2 = 0
				electrics.values.cornerlight_R1 = 0
            elseif off_timerR >= 0.07 then
				electrics.values.cornerlight_R5 = 1
				electrics.values.cornerlight_R6 = 0
				electrics.values.cornerlight_R4 = 0
				electrics.values.cornerlight_R3 = 0
				electrics.values.cornerlight_R2 = 0
				electrics.values.cornerlight_R1 = 0
            elseif off_timerR >= 0 then
				electrics.values.cornerlight_R6 = 1
				electrics.values.cornerlight_R5 = 0
				electrics.values.cornerlight_R4 = 0
				electrics.values.cornerlight_R3 = 0
				electrics.values.cornerlight_R2 = 0
				electrics.values.cornerlight_R1 = 0
                if electrics.values.cornerlight_R1 == 0 and electrics.values.cornerlight_R2 == 0 and electrics.values.cornerlight_R3 == 0 and electrics.values.cornerlight_R4 == 0 and electrics.values.cornerlight_R5 == 0 and electrics.values.cornerlight_R6 == 0 then
                    cornerlightR_active = false
                    off_timerR = 0
                end
            end
        else
            electrics.values.cornerlight_R1 = 0
            electrics.values.cornerlight_R2 = 0
            electrics.values.cornerlight_R3 = 0
            electrics.values.cornerlight_R4 = 0
            electrics.values.cornerlight_R5 = 0
            electrics.values.cornerlight_R6 = 0
        end
    end
end

local function updateGFX(dt)
    if electrics.values.ignitionLevel <= 1 then
        electrics.values.cornerlight_L = 0
        electrics.values.cornerlight_R = 0
    elseif electrics.values.fog == 1 then
        electrics.values.cornerlight_L = 1
        electrics.values.cornerlight_R = 1
    elseif electrics.values.wheelspeed > 60/3.6 then
        electrics.values.cornerlight_L = 0
        electrics.values.cornerlight_R = 0
    elseif electrics.values.steering < -75 or (electrics.values.wheelspeed < 30/3.6 and electrics.values.signal_right_input == 1 and electrics.values.hazard_enabled == 0) then
        electrics.values.cornerlight_R = 1
    elseif electrics.values.steering > 75 or (electrics.values.wheelspeed < 30/3.6 and electrics.values.signal_left_input == 1 and electrics.values.hazard_enabled == 0) then
        electrics.values.cornerlight_L = 1
    else
        electrics.values.cornerlight_L = 0
        electrics.values.cornerlight_R = 0
    end

    smoothlights(dt)
end

-- public interface
M.updateGFX = updateGFX
M.smoothlights = smoothlights
return M
