local M = {}

local pattern = 0
local timer = 0
local cycleComplete = 0
local cycleStarting = 0
local nextPatternName = "Flashing"

local function toggleState()
	electrics.values.RGB = 1 - (electrics.values.RGB or 0)
	if electrics.values.RGB == 1 then
		guihooks.message("Starlights enabled! Now showing: RGB mode", 4, "vehicle.ekhv36Starlights.StarlightsState", "filter_none") 
	else
		guihooks.message("Starlights disabled", 4, "vehicle.ekhv36Starlights.StarlightsState", "filter_none") 
	end
end

local function nextPattern()	

	if electrics.values["RGB"] == 1 and pattern == 2 then
		guihooks.message("Starlights pattern changed! Now showing: " .. nextPatternName .. " (" .. pattern+1 .. "/" .. 3 .. ")", 4, "vehicle.ekhv36Starlights.StarlightsPattern", "filter_none") 
		
		pattern = 0
		timer = 0
	
	elseif electrics.values["RGB"] == 1 then
		guihooks.message("Starlights pattern changed! Now showing: '" .. nextPatternName .. "' (" .. pattern+1 .. "/" .. 3 .. ")", 4, "vehicle.ekhv36Starlights.StarlightsPattern", "filter_none") 

		pattern = pattern + 1
		timer = 0
	--To fully reset the lights before switching pattern cycle
	electrics.values["ekhv36_RGB_1"] = 0
    electrics.values["ekhv36_RGB_2"] = 0
    electrics.values["ekhv36_RGB_3"] = 0
    electrics.values["ekhv36_RGB_4"] = 0
    electrics.values["ekhv36_RGB_5"] = 0
    electrics.values["ekhv36_RGB_6"] = 0
    electrics.values["ekhv36_RGB_7"] = 0
    electrics.values["ekhv36_RGB_8"] = 0
    electrics.values["ekhv36_RGB_9"] = 0
    electrics.values["ekhv36_RGB_10"] = 0

	else
		guihooks.message("Turn on the Starlights first.", 4, "vehicle.ekhv36Starlights.StarlightsPattern", "filter_none") 
	end
end

local function onInit(jbeamData)
	electrics.values["ekhv36_RGB_1"] = 0
    electrics.values["ekhv36_RGB_2"] = 0
    electrics.values["ekhv36_RGB_3"] = 0
    electrics.values["ekhv36_RGB_4"] = 0
    electrics.values["ekhv36_RGB_5"] = 0
    electrics.values["ekhv36_RGB_6"] = 0
    electrics.values["ekhv36_RGB_7"] = 0
    electrics.values["ekhv36_RGB_8"] = 0
    electrics.values["ekhv36_RGB_9"] = 0
    electrics.values["ekhv36_RGB_10"] = 0
	
	nextPatternName = "Flashing"
	timer2 = 0
	timer = 0
	timer3 = 0
	cycleStarting = 0
	cycleComplete = 0
end

local function updateGFX(dt) 
  if pattern == 0 and electrics.values["RGB"] == 1 then
	electrics.values["ekhv36_RGB_1"] = 1
    electrics.values["ekhv36_RGB_2"] = 1
    electrics.values["ekhv36_RGB_3"] = 1
    electrics.values["ekhv36_RGB_4"] = 1
    electrics.values["ekhv36_RGB_5"] = 1
    electrics.values["ekhv36_RGB_6"] = 1
    electrics.values["ekhv36_RGB_7"] = 1
    electrics.values["ekhv36_RGB_8"] = 1
    electrics.values["ekhv36_RGB_9"] = 1
    electrics.values["ekhv36_RGB_10"] = 1
	nextPatternName = "Show"
	
  elseif pattern == 1 and electrics.values["RGB"] == 1 then
	timer = timer + dt
	nextPatternName = "Flashing"
	if timer <= 0.1 then
	--toggle off
	electrics.values["ekhv36_RGB_1"] = 1
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 1
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 0
	electrics.values["ekhv36_RGB_6"] = 1
	electrics.values["ekhv36_RGB_7"] = 1
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 0
	electrics.values["ekhv36_RGB_10"] = 0
	
	elseif timer >= 0.1 and timer <= 0.2 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 0
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 0
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 0
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 0
	electrics.values["ekhv36_RGB_8"] = 1
	electrics.values["ekhv36_RGB_9"] = 1
	electrics.values["ekhv36_RGB_10"] = 1

	elseif timer >= 0.2 and timer <= 0.3 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 0
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 0
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 1
	electrics.values["ekhv36_RGB_6"] = 1
	electrics.values["ekhv36_RGB_7"] = 1
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 0
	electrics.values["ekhv36_RGB_10"] = 1
	
	elseif timer >= 0.3 and timer <= 0.4 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 1
	electrics.values["ekhv36_RGB_2"] = 1
	electrics.values["ekhv36_RGB_3"] = 1
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 0
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 1
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 0
	electrics.values["ekhv36_RGB_10"] = 0
	
	elseif timer >= 0.4 and timer <= 0.5 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 1
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 1
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 0
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 1
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 1
	electrics.values["ekhv36_RGB_10"] = 1
	
	elseif timer >= 0.5 and timer <= 0.6 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 0
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 1
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 0
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 1
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 0
	electrics.values["ekhv36_RGB_10"] = 0
	
	elseif timer >= 0.6 and timer <= 0.7 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 0
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 0
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 1
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 1
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 1
	electrics.values["ekhv36_RGB_10"] = 0
	
	elseif timer >= 0.7 and timer <= 0.8 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 0
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 1
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 0
	electrics.values["ekhv36_RGB_6"] = 1
	electrics.values["ekhv36_RGB_7"] = 0
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 1
	electrics.values["ekhv36_RGB_10"] = 1

	elseif timer >= 0.8 and timer <= 0.9 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 1
	electrics.values["ekhv36_RGB_2"] = 1
	electrics.values["ekhv36_RGB_3"] = 0
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 0
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 1
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 0
	electrics.values["ekhv36_RGB_10"] = 1

	elseif timer >= 0.9 and timer <= 1.0 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 1
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 0
	electrics.values["ekhv36_RGB_4"] = 1
	electrics.values["ekhv36_RGB_5"] = 0
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 0
	electrics.values["ekhv36_RGB_8"] = 1
	electrics.values["ekhv36_RGB_9"] = 0
	electrics.values["ekhv36_RGB_10"] = 0

	elseif timer >= 1.0 and timer <= 1.1 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 1
	electrics.values["ekhv36_RGB_2"] = 0
	electrics.values["ekhv36_RGB_3"] = 1
	electrics.values["ekhv36_RGB_4"] = 1
	electrics.values["ekhv36_RGB_5"] = 1
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 0
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 1
	
	elseif timer >= 1.1 and timer <= 1.2 then
	--toggling old
	electrics.values["ekhv36_RGB_1"] = 0
	electrics.values["ekhv36_RGB_2"] = 1
	electrics.values["ekhv36_RGB_3"] = 0
	electrics.values["ekhv36_RGB_4"] = 0
	electrics.values["ekhv36_RGB_5"] = 1
	electrics.values["ekhv36_RGB_6"] = 0
	electrics.values["ekhv36_RGB_7"] = 1
	electrics.values["ekhv36_RGB_8"] = 0
	electrics.values["ekhv36_RGB_9"] = 0
	electrics.values["ekhv36_RGB_10"] = 1
	
	else timer = 0
	
	end
	
  elseif pattern == 2 and electrics.values["RGB"] == 1 then
	timer = timer + dt
	nextPatternName = "No Pattern"
	if timer <= 0.4 then
	electrics.values["ekhv36_RGB_1"] = 0
    electrics.values["ekhv36_RGB_2"] = 0
    electrics.values["ekhv36_RGB_3"] = 0
    electrics.values["ekhv36_RGB_4"] = 0
    electrics.values["ekhv36_RGB_5"] = 0
    electrics.values["ekhv36_RGB_6"] = 0
    electrics.values["ekhv36_RGB_7"] = 0
    electrics.values["ekhv36_RGB_8"] = 0
    electrics.values["ekhv36_RGB_9"] = 0
    electrics.values["ekhv36_RGB_10"] = 0
	
	elseif timer >= 0.4 and timer <= 0.8 then
	electrics.values["ekhv36_RGB_1"] = 1
    electrics.values["ekhv36_RGB_2"] = 1
    electrics.values["ekhv36_RGB_3"] = 1
    electrics.values["ekhv36_RGB_4"] = 1
    electrics.values["ekhv36_RGB_5"] = 1
    electrics.values["ekhv36_RGB_6"] = 1
    electrics.values["ekhv36_RGB_7"] = 1
    electrics.values["ekhv36_RGB_8"] = 1
    electrics.values["ekhv36_RGB_9"] = 1
    electrics.values["ekhv36_RGB_10"] = 1
	
	else timer = 0

	end
	
  else
	electrics.values["ekhv36_RGB_1"] = 0
    electrics.values["ekhv36_RGB_2"] = 0
    electrics.values["ekhv36_RGB_3"] = 0
    electrics.values["ekhv36_RGB_4"] = 0
    electrics.values["ekhv36_RGB_5"] = 0
    electrics.values["ekhv36_RGB_6"] = 0
    electrics.values["ekhv36_RGB_7"] = 0
    electrics.values["ekhv36_RGB_8"] = 0
    electrics.values["ekhv36_RGB_9"] = 0
    electrics.values["ekhv36_RGB_10"] = 0
	timer = 0	
  end
end


M.updateGFX = updateGFX
M.onInit = onInit
M.nextPattern = nextPattern
M.toggleState = toggleState
return M