-- << MESSAGING SERVICE >>

spawn(function()
	--testSub:Disconnect()
	for _, topicName in pairs(topics) do
		main.messagingService:SubscribeAsync(topicName, function(message)
			local data = message.Data
			
			if topicName == "BeginPoll" then
				local newData, scores = main:GetModule("cf"):BeginPoll(data)
				main.messagingService:PublishAsync(data.UniquePollTopic, scores)
				
			elseif topicName == "DisplayPollResultsToServer" then
				for i, plr in pairs(main.players:GetChildren()) do
					main:GetModule("cf"):DisplayPollResults(plr, data)
				end	
			
			elseif topicName == "GetAmountOfServers" then
				local topicName = data
				main.messagingService:PublishAsync(topicName)
			
			elseif topicName == "GlobalAlert" then
				for i,plr in pairs(main.players:GetChildren()) do
					main.signals.CreateAlert:FireClient(plr, {data.Title, data.Message})
				end
			
			elseif topicName == "GlobalCommands" then
				local data = message.Data
				-------------------------------------------
				if data.Name == "globalPlace" then
					local placeId = data.PlaceId
					local placeName = data.PlaceName
					for i, plr in pairs(main.players:GetChildren()) do
						local notice = {
							"Global Teleport",
							"Click to teleport to "..placeName.." ["..placeId.."]",
							{"Teleport", placeId},
						}
						main.signals.Notice:FireClient(plr, notice)
					end
				end
				
				
				-------------------------------------------
				if data.Name == "globalForcePlace" then
					local placeId = data.PlaceId
					local placeName = data.PlaceName
					for i, plr in pairs(main.players:GetChildren()) do
						main.teleportService:Teleport(placeId, plr)
					end
				end
				
				
				-------------------------------------------
			end--]]
		end)
	end
end)



return module
