local k=require((game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")):WaitForChild("\078\101\118\101\114\109\111\114\101",2))local t=k("\080\117\098\083\117\098")local Z=k("\067\111\109\112\111\110\101\110\116\066\097\115\101")local F={}function F.GetElsModelFromVehicle(k)local t=(k:FindFirstChild("\066\111\100\121")):FindFirstChild("\069\076\083")return t end function F.ConnectListenerFuncToElsTopic(k,Z,F)local D=t:ConnectEntityListenerFuncToTopic(k,"\069\076\083",Z,F)return D end local D={ElsFuncs=F}function D.__turnOffAllSirens(k)if k~=nil and k:GetChildren()~=nil then for k,t in pairs(k:GetChildren())do if t:FindFirstChild("\083\116\111\112")~=nil then print("\083\116\111\112\112\105\110\103 "..t)t:Stop()end end end end function D.__stopSiren(k,t)if k~=nil and k:FindFirstChild(t)~=nil then local Z=k:FindFirstChild(t)Z:Stop()end end function D.__playSiren(k,t)if k~=nil and k:FindFirstChild(t)~=nil then local Z=k:FindFirstChild(t)Z:Play()Z.Volume=.5 Z.Looped=true end end function D._getSiren1ListenerFunc(k)local t=k:FindFirstChild("\083\105\114\101\110")local Z=t:FindFirstChild("\087\097\105\108")Z.EmitterSize=10 local F=false local J=function(k,Z)D.__turnOffAllSirens(t)F=not F if F==true then D.__playSiren(t,"\087\097\105\108")else D.__stopSiren(t,"\087\097\105\108")end end return J end function D._getSiren2ListenerFunc(k)local t=k:FindFirstChild("\083\105\114\101\110")local Z=t:FindFirstChild("\089\101\108\112")Z.EmitterSize=10 local F=false local J=function(k,Z)D.__turnOffAllSirens(t)F=not F if F==true then D.__playSiren(t,"\089\101\108\112")else D.__stopSiren(t,"\089\101\108\112")end end return J end local J=Z.new("\069\108\115\072\117\100",{"\101\108\115\104\117\100"})J.SirenFuncs=D function J.Execute(t,k)local Z=t.SirenFuncs.ElsFuncs.GetElsModelFromVehicle(k)t.SirenFuncs.ElsFuncs.ConnectListenerFuncToElsTopic(k.EntityId.Value,"\083\073\082\069\078\049",t.SirenFuncs._getSiren1ListenerFunc(Z))t.SirenFuncs.ElsFuncs.ConnectListenerFuncToElsTopic(k.EntityId.Value,"\083\073\082\069\078\050",t.SirenFuncs._getSiren2ListenerFunc(Z))end return J
local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore", 2))

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 sirenFuncs = {
    ElsFuncs = elsFuncs
}

function sirenFuncs.__turnOffAllSirens(sirenPart)
	if sirenPart ~= nil and sirenPart:GetChildren() ~= nil then
		for _, siren in pairs(sirenPart:GetChildren()) do
			if siren:FindFirstChild("Stop") ~= nil then
				print("Stopping " .. siren)
				siren:Stop()
			end
		end
	end
end

function sirenFuncs.__stopSiren(sirenPart, sirenName)
	if sirenPart ~= nil and sirenPart:FindFirstChild(sirenName) ~= nil then
		local sirenSound = sirenPart:FindFirstChild(sirenName)
		sirenSound:Stop()
	end
end

function sirenFuncs.__playSiren(sirenPart, sirenName)
	
	if sirenPart ~= nil and sirenPart:FindFirstChild(sirenName) ~= nil then
		local sirenSound = sirenPart:FindFirstChild(sirenName)
		sirenSound:Play()
		sirenSound.Volume = 0.5
		sirenSound.Looped = true
	end
end

function sirenFuncs._getSiren1ListenerFunc(elsModel)
	
	local sirenPart = elsModel:FindFirstChild("Siren")
	local sirenSound = sirenPart:FindFirstChild("Wail")
	sirenSound.EmitterSize = 10
	local siren1On = false

	local listenerFunc = function(sender, data)
		sirenFuncs.__turnOffAllSirens(sirenPart)

		siren1On = not siren1On

		if siren1On == true then
			sirenFuncs.__playSiren(sirenPart, "Wail")
		else 
			sirenFuncs.__stopSiren(sirenPart, "Wail")
		end
	end
	return listenerFunc
end

function sirenFuncs._getSiren2ListenerFunc(elsModel)
	
	local sirenPart = elsModel:FindFirstChild("Siren")
	local sirenSound = sirenPart:FindFirstChild("Yelp")
	sirenSound.EmitterSize = 10
	local siren2On = false
	local listenerFunc = function(sender, data)
		sirenFuncs.__turnOffAllSirens(sirenPart)

		siren2On = not siren2On

		if siren2On == true then
			sirenFuncs.__playSiren(sirenPart, "Yelp")
		else 
			sirenFuncs.__stopSiren(sirenPart, "Yelp")
		end
	end
	return listenerFunc
end

local component = ComponentBase.new("ElsHud", {"elshud"})
component.SirenFuncs = sirenFuncs

function component:Execute( gameObject )
    local elsModel = self.SirenFuncs.ElsFuncs.GetElsModelFromVehicle(gameObject)

    self.SirenFuncs.ElsFuncs.ConnectListenerFuncToElsTopic(gameObject.EntityId.Value, "SIREN1",  self.SirenFuncs._getSiren1ListenerFunc(elsModel))
    self.SirenFuncs.ElsFuncs.ConnectListenerFuncToElsTopic(gameObject.EntityId.Value, "SIREN2",  self.SirenFuncs._getSiren2ListenerFunc(elsModel))
end

return component