local k=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local t=require(k:WaitForChild("\078\101\118\101\114\109\111\114\101",2))local Z=(t("\083\116\100")).rquery local F=t("\080\117\098\083\117\098")local D=t("\067\111\109\112\111\110\101\110\116\066\097\115\101")local J={}function J.GetElsModelFromVehicle(k)local t=(k:FindFirstChild("\066\111\100\121")):FindFirstChild("\069\076\083")return t end function J.ConnectListenerFuncToElsTopic(k,t,Z)local D=F:ConnectEntityListenerFuncToTopic(k,"\069\076\083",t,Z)return D end local u={ElsFuncs=J}function u.GetLightGroup(k,t)if k~=nil and k:FindFirstChild(t)then local Z=k:FindFirstChild(t)return Z:GetChildren()else warn(k.Name..(" \105\115 \110\105\108 \111\114 \109\105\115\115\105\110\103 "..t))end end function u.TurnOnLightGroup(k)for k,t in pairs(k)do u._turnOnLight(t)end end function u.TurnOffLightGroup(k)for k,t in pairs(k)do u._turnOffLight(t)end end function u._turnOnLight(k)if k:FindFirstChild("\080\111\105\110\116")~=nil then k.Point.Enabled=true end if k:FindFirstChild("\076\105\103\104\116\111")~=nil then k.Lighto.Enabled=true end if k.Name=="\114\101\100"or k.Name=="\119\104\105\116\101"or k.Name=="\098\108\117\101"or k.Name=="\121\101\108\108\111\119"then k.Material=Enum.Material.Neon k.Transparency=0 end for k,t in pairs(k:GetChildren())do if t:IsA("\083\112\111\116\076\105\103\104\116")or t:IsA("\083\117\114\102\097\099\101\076\105\103\104\116")or t:IsA("\080\111\105\110\116\076\105\103\104\116")then t.Enabled=true end end end function u._turnOffLight(k)if k:FindFirstChild("\080\111\105\110\116")~=nil then k.Point.Enabled=false end if k:FindFirstChild("\076\105\103\104\116\111")~=nil then k.Lighto.Enabled=false end if(k.Name=="\114\101\100"or k.Name=="\119\104\105\116\101"or k.Name=="\098\108\117\101"or k.Name=="\121\101\108\108\111\119")and k.Material==Enum.Material.Neon then k.Material=Enum.Material.SmoothPlastic k.Transparency=.6 end for k,t in pairs(k:GetChildren())do if t:IsA("\083\112\111\116\076\105\103\104\116")or t:IsA("\083\117\114\102\097\099\101\076\105\103\104\116")then t.Enabled=false end end end function u.TurnOffAllLights(k)local t=u.GetLightGroup(k,"\071\049")local Z=u.GetLightGroup(k,"\071\050")u.TurnOffLightGroup(t)u.TurnOffLightGroup(Z)end function u.ConnectLights(k,t,Z,F)print("\099\111\110\110\101\099\116\105\110\103 \108\105\103\104\116\115\046\046\046 ")local D=u.ElsFuncs.ConnectListenerFuncToElsTopic(k,t,F)print(D.Name.." \072\065\083 \082\069\067\069\073\086\069\068 \065 \083\069\082\086\069\082-\083\073\068\069 \076\073\083\084\069\078\069\082\033\064\033\033\033\033")return true end function u.ExecuteStrobeWigWagPattern(k,t)for t=1,6,1 do wait(.05)u.TurnOnLightGroup(k)wait(.05)u.TurnOffLightGroup(k)end for k=1,6,1 do wait(.05)u.TurnOnLightGroup(t)wait(.05)u.TurnOffLightGroup(t)end end function u.ExecuteWigWagPattern(k,t)wait(.2)u.TurnOnLightGroup(k)u.TurnOffLightGroup(t)wait(.2)u.TurnOffLightGroup(k)u.TurnOnLightGroup(t)end function u.ExecuteBlinkBlinkPattern(k,t)u.TurnOffLightGroup(k)u.TurnOffLightGroup(t)for Z=1,2,1 do wait(.2)u.TurnOnLightGroup(k)u.TurnOnLightGroup(t)wait(.2)u.TurnOffLightGroup(k)u.TurnOffLightGroup(t)end end local m=D.new("\076\105\103\104\116\098\097\114\049",{"\101\108\115\104\117\100";"\108\105\103\104\116\098\097\114\049"})m.LightsFuncs=u function m._getLights1ListenerFunc(k)local t=k:FindFirstChild("\108\105\103\104\116\098\097\114\049")if t:FindFirstChild("\111\110")==nil then local k=Instance.new("\066\111\111\108\086\097\108\117\101")k.Value=false k.Name="\111\110"k.Parent=t end m.LightsFuncs.TurnOffAllLights(t)t.on.Value=false local Z=false local F=function(k,F)print("\067\117\114\114\101\110\116 \076\105\103\104\116\115\079\110\058"..tostring(Z))Z=not Z t.on.Value=Z local D=m.LightsFuncs.GetLightGroup(t,"\071\049")local J=m.LightsFuncs.GetLightGroup(t,"\071\050")if t.on.Value then while t.on.Value do for k=1,4,1 do if t.on.Value then m.LightsFuncs.ExecuteStrobeWigWagPattern(D,J)end end for k=1,2,1 do if t.on.Value then m.LightsFuncs.ExecuteWigWagPattern(D,J)end end end end m.LightsFuncs.TurnOffAllLights(t)end return F end function m.Execute(t,k)local Z=t.LightsFuncs.ElsFuncs.GetElsModelFromVehicle(k)t.LightsFuncs.ConnectLights(k.EntityId.Value,"\076\073\071\072\084\083\049",Z,t._getLights1ListenerFunc(Z))end local a=D.new("\076\105\103\104\116\098\097\114\050",{"\101\108\115\104\117\100";"\108\105\103\104\116\098\097\114\050"})a.LightsFuncs=u function a._getLights2ListenerFunc(k)local t=k:FindFirstChild("\108\105\103\104\116\098\097\114\050")if t:FindFirstChild("\111\110")==nil then local k=Instance.new("\066\111\111\108\086\097\108\117\101")k.Value=false k.Name="\111\110"k.Parent=t end a.LightsFuncs.TurnOffAllLights(t)t.on.Value=false local Z=false local F=function(k,F)print("\067\117\114\114\101\110\116 \076\105\103\104\116\115\079\110\058"..tostring(Z))Z=not Z t.on.Value=Z local D=a.LightsFuncs.GetLightGroup(t,"\071\049")local J=a.LightsFuncs.GetLightGroup(t,"\071\050")if t.on.Value then while t.on.Value do a.LightsFuncs.TurnOnLightGroup(D)wait(.5)end else a.LightsFuncs.TurnOffAllLights(t)end end return F end function a.Execute(t,k)local Z=t.LightsFuncs.ElsFuncs.GetElsModelFromVehicle(k)t.LightsFuncs.ConnectLights(k.EntityId.Value,"\076\073\071\072\084\083\050",Z,t._getLights2ListenerFunc(Z))end local A=D.new("\069\108\115\076\105\103\104\116\115",{"\101\108\115\104\117\100"})A.InnerComponents={m;a}function A.Execute(t,k)for t,Z in pairs(t.InnerComponents)do Z:TryExecute(k)end end return A
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local require = require(ReplicatedStorage:WaitForChild("Nevermore", 2))

local rq = require("Std").rquery
local pubSub = require("PubSub")
local ComponentBase = require("ComponentBase")

local elsFuncs = {}

function elsFuncs.GetElsModelFromVehicle(vehicleModel)
	local elsModel = vehicleModel:FindFirstChild("Body"):FindFirstChild("ELS")
	return elsModel
end
function elsFuncs.ConnectListenerFuncToElsTopic(entityId, topic, listenerFunc)
	local elsTopic = pubSub:ConnectEntityListenerFuncToTopic(entityId, "ELS", topic, listenerFunc)
	return elsTopic
end

local lightsFuncs = {
	ElsFuncs = elsFuncs
}
function lightsFuncs.GetLightGroup(lightbar, groupName)
	if lightbar ~= nil and 
		lightbar:FindFirstChild(groupName)
	then
		local lightsGroupModel = lightbar:FindFirstChild(groupName)
		return lightsGroupModel:GetChildren()
	else 
		warn(lightbar.Name .. " is nil or missing " .. groupName)
	end
end

function lightsFuncs.TurnOnLightGroup(lightgroup)
	for _, value in pairs(lightgroup) do
		lightsFuncs._turnOnLight(value)
	end
end

function lightsFuncs.TurnOffLightGroup(lightgroup)
	for _, value in pairs(lightgroup) do
		lightsFuncs._turnOffLight(value)
	end
end


function lightsFuncs._turnOnLight(lightPart)
	if lightPart:FindFirstChild("Point") ~= nil then
		lightPart.Point.Enabled = true
	end
	if lightPart:FindFirstChild("Lighto") ~= nil then
		lightPart.Lighto.Enabled = true
	end
	if lightPart.Name == "red" 
	or lightPart.Name == "white" or 
		lightPart.Name == "blue" or 
		lightPart.Name == "yellow" then
		lightPart.Material = Enum.Material.Neon
		lightPart.Transparency = 0

	end
	for idx, obj in pairs(lightPart:GetChildren()) do
		if obj:IsA("SpotLight") or obj:IsA("SurfaceLight") or obj:IsA("PointLight") then
			obj.Enabled = true
		end
	end
end

function lightsFuncs._turnOffLight(lightPart)
	if lightPart:FindFirstChild("Point") ~= nil then
		lightPart.Point.Enabled = false
	end
	if lightPart:FindFirstChild("Lighto") ~= nil then
		lightPart.Lighto.Enabled = false
	end
	if (lightPart.Name == "red" or
		lightPart.Name == "white" or 
		lightPart.Name == "blue" or 
		lightPart.Name == "yellow") and lightPart.Material == Enum.Material.Neon then
		lightPart.Material = Enum.Material.SmoothPlastic
		lightPart.Transparency = 0.6
	end
	for idx, obj in pairs(lightPart:GetChildren()) do
		if obj:IsA("SpotLight") or obj:IsA("SurfaceLight") then
			obj.Enabled = false
		end
	end
end

function lightsFuncs.TurnOffAllLights(lightbar)
	local group1 = lightsFuncs.GetLightGroup(lightbar, "G1")
	local group2 = lightsFuncs.GetLightGroup(lightbar, "G2")
	lightsFuncs.TurnOffLightGroup(group1)
	lightsFuncs.TurnOffLightGroup(group2)
end

function lightsFuncs.ConnectLights(entityId, lightsTopic, elsModel, listenerFunc)
	print("connecting lights... ")

	local elsTopicLights1 = lightsFuncs.ElsFuncs.ConnectListenerFuncToElsTopic(entityId, lightsTopic, listenerFunc)
	print(elsTopicLights1.Name .. " HAS RECEIVED A SERVER-SIDE LISTENER!@!!!!")

	return true
end

function lightsFuncs.ExecuteStrobeWigWagPattern(lightGroup1, lightGroup2)
	
	for i=1, 6 do
		wait(0.05)
		lightsFuncs.TurnOnLightGroup(lightGroup1)
		wait(0.05)
		lightsFuncs.TurnOffLightGroup(lightGroup1)
	end

	for i=1, 6 do
		wait(0.05)
		lightsFuncs.TurnOnLightGroup(lightGroup2)
		
		wait(0.05)
		lightsFuncs.TurnOffLightGroup(lightGroup2)
	end
	
end
	 
function lightsFuncs.ExecuteWigWagPattern(lightGroup1, lightGroup2)
	wait(0.2)
	lightsFuncs.TurnOnLightGroup(lightGroup1)
	lightsFuncs.TurnOffLightGroup(lightGroup2)
	wait(0.2)
	lightsFuncs.TurnOffLightGroup(lightGroup1)
	lightsFuncs.TurnOnLightGroup(lightGroup2)
end

	 
function lightsFuncs.ExecuteBlinkBlinkPattern(lightGroup1, lightGroup2)
	lightsFuncs.TurnOffLightGroup(lightGroup1)
	lightsFuncs.TurnOffLightGroup(lightGroup2)
	for i=1, 2 do
		wait(0.2)
		lightsFuncs.TurnOnLightGroup(lightGroup1)
		lightsFuncs.TurnOnLightGroup(lightGroup2)
		wait(0.2)
		lightsFuncs.TurnOffLightGroup(lightGroup1)
		lightsFuncs.TurnOffLightGroup(lightGroup2)
	end
end

local lightbar1Component = ComponentBase.new("Lightbar1", { "elshud", "lightbar1" })
lightbar1Component.LightsFuncs = lightsFuncs
function lightbar1Component._getLights1ListenerFunc(elsModel)
	
	local lightbar1 = elsModel:FindFirstChild("lightbar1")
	if lightbar1:FindFirstChild("on") == nil then
		local onValue = Instance.new("BoolValue")
		onValue.Value = false
		onValue.Name = "on"
		onValue.Parent = lightbar1
	end
	lightbar1Component.LightsFuncs.TurnOffAllLights(lightbar1)
	lightbar1.on.Value = false
	local lightsOn = false
	local listenerFunc = function(sender, data)
		print("Current LightsOn:" .. tostring(lightsOn))
		lightsOn = not lightsOn

		lightbar1.on.Value = lightsOn
		local lightGroup1 = lightbar1Component.LightsFuncs.GetLightGroup(lightbar1, "G1")
		local lightGroup2 = lightbar1Component.LightsFuncs.GetLightGroup(lightbar1, "G2")

		if lightbar1.on.Value then
			while lightbar1.on.Value do
				for iter = 1, 4 do
					if (lightbar1.on.Value) then
						lightbar1Component.LightsFuncs.ExecuteStrobeWigWagPattern(lightGroup1, lightGroup2)
					end
				end
				for iter = 1, 2 do
					if (lightbar1.on.Value) then
						lightbar1Component.LightsFuncs.ExecuteWigWagPattern(lightGroup1, lightGroup2)
					end
				end
			end
		end
		lightbar1Component.LightsFuncs.TurnOffAllLights(lightbar1)
	end

	return listenerFunc
end
function lightbar1Component:Execute(gameObject)
	local elsModel = self.LightsFuncs.ElsFuncs.GetElsModelFromVehicle(gameObject)

	self.LightsFuncs.ConnectLights(gameObject.EntityId.Value, "LIGHTS1", elsModel, self._getLights1ListenerFunc(elsModel))
end


local lightbar2Component = ComponentBase.new("Lightbar2", { "elshud", "lightbar2" })
lightbar2Component.LightsFuncs = lightsFuncs

function lightbar2Component._getLights2ListenerFunc(elsModel)
	
	local lightbar2 = elsModel:FindFirstChild("lightbar2")
	if lightbar2:FindFirstChild("on") == nil then
		local onValue = Instance.new("BoolValue")
		onValue.Value = false
		onValue.Name = "on"
		onValue.Parent = lightbar2
	end
	lightbar2Component.LightsFuncs.TurnOffAllLights(lightbar2)
	lightbar2.on.Value = false
	local lightsOn = false
	local listenerFunc = function(sender, data)
		print("Current LightsOn:" .. tostring(lightsOn))
		lightsOn = not lightsOn

		lightbar2.on.Value = lightsOn
		local lightGroup1 = lightbar2Component.LightsFuncs.GetLightGroup(lightbar2, "G1")
		local lightGroup2 = lightbar2Component.LightsFuncs.GetLightGroup(lightbar2, "G2")

		if lightbar2.on.Value then
			while lightbar2.on.Value do
				lightbar2Component.LightsFuncs.TurnOnLightGroup(lightGroup1)
				wait(0.5)
			end
		else
			lightbar2Component.LightsFuncs.TurnOffAllLights(lightbar2)
		end
	end

	return listenerFunc
end
function lightbar2Component:Execute(gameObject)
	local elsModel = self.LightsFuncs.ElsFuncs.GetElsModelFromVehicle(gameObject)

	self.LightsFuncs.ConnectLights(gameObject.EntityId.Value, "LIGHTS2", elsModel, self._getLights2ListenerFunc(elsModel))
end

local component = ComponentBase.new("ElsLights", {"elshud"})

component.InnerComponents = {
	lightbar1Component,
	lightbar2Component
}

function component:Execute(gameObject)
	for _, cmp in pairs(self.InnerComponents) do
		cmp:TryExecute(gameObject)
	end
end

return component