local Z=game.ReplicatedStorage:WaitForChild("\080\097\099\107\097\103\101\115")local X=require(Z.TeleportQueue)local v=require(Z.Component)local o=require(script.QueueSurfaceGui)local h=newproxy()local g=newproxy()local b=newproxy()local S=20 local i=v.new({Tag="\084\101\108\101\112\111\114\116\081\117\101\117\101\067\111\109\112\111\110\101\110\116"})function i.Construct(Z)local v=Instance.new("\084\101\108\101\112\111\114\116\079\112\116\105\111\110\115")v.ShouldReserveServer=Z.Instance:GetAttribute("\083\104\111\117\108\100\082\101\115\101\114\118\101\083\101\114\118\101\114")Z[h]=X.new({Id=tostring(newproxy());PlaceId=Z.Instance:GetAttribute("\080\108\097\099\101\073\100"),TeleportOptions=v})local g=o()Z.SurfaceGui=g.SurfaceGui Z.SubtitleLabel=g.SubtitleLabel Z.PlayersLabel=g.PlayersLabel Z.TimerLabel=g.TimerLabel Z.SurfaceGui.Parent=Z.Instance end function i.UpdateSurfaceGui(Z)Z.PlayersLabel.Text=#Z[h]:GetPlayers()Z.SubtitleLabel.Text=Z.Status Z.TimerLabel.Text=Z.Timer end function i.Start(Z)do Z[b]=task.spawn(function()while true do Z.Status="\082\101\097\100\121"Z.Timer=S Z:UpdateSurfaceGui()for X=S,0,-1 do task.wait(1)Z.Timer=X Z:UpdateSurfaceGui()end Z.Status="\084\101\108\101\112\111\114\116\105\110\103"Z:UpdateSurfaceGui()Z[h]:Flush()Z:UpdateSurfaceGui()task.wait(4)end end)end do Z[h]:Observe(function()Z:UpdateSurfaceGui()end)Z[g]=Z.Instance.Touched:Connect(function(X)local v=game.Players:GetPlayerFromCharacter(X.Parent)if v then Z[h]:Add(v)end end)end end function i.Stop(Z)Z[h]:Destroy()if Z[g]then Z[g]:Disconnect()Z[g]=nil end if Z[b]then task.cancel(Z[b])Z[b]=nil end Z.SurfaceGui:Destroy()end return i
local Packages = game.ReplicatedStorage:WaitForChild("Packages")
local TeleportQueue = require(Packages.TeleportQueue)
local Component = require(Packages.Component)

local QueueSurfaceGui = require(script.QueueSurfaceGui)

local TELEPORT_QUEUE_SYMBOL = newproxy()
local TOUCH_CONNECTION_SYMBOL = newproxy()
local TIMER_LOOP_THREAD = newproxy()

local COUNT_DOWN_TIME = 20

local TeleportQueueComponent = Component.new({
	Tag = "TeleportQueueComponent"
})

function TeleportQueueComponent:Construct()
	local queueTeleportOptions = Instance.new("TeleportOptions")
	queueTeleportOptions.ShouldReserveServer = self.Instance:GetAttribute("ShouldReserveServer")

	self[TELEPORT_QUEUE_SYMBOL] = TeleportQueue.new({
		Id = tostring(newproxy()),
		PlaceId = self.Instance:GetAttribute("PlaceId"),
		TeleportOptions = queueTeleportOptions
	})

	local newSurfaceGuiInstances = QueueSurfaceGui()
	self.SurfaceGui = newSurfaceGuiInstances.SurfaceGui
	self.SubtitleLabel = newSurfaceGuiInstances.SubtitleLabel
	self.PlayersLabel = newSurfaceGuiInstances.PlayersLabel
	self.TimerLabel = newSurfaceGuiInstances.TimerLabel

	self.SurfaceGui.Parent = self.Instance
end

function TeleportQueueComponent:UpdateSurfaceGui()
	self.PlayersLabel.Text = #self[TELEPORT_QUEUE_SYMBOL]:GetPlayers()
	self.SubtitleLabel.Text = self.Status
	self.TimerLabel.Text = self.Timer
end

function TeleportQueueComponent:Start()
	do --handles the component's loop
		self[TIMER_LOOP_THREAD] = task.spawn(function()
			while true do
				self.Status = "Ready"
				self.Timer = COUNT_DOWN_TIME
				self:UpdateSurfaceGui()

				for timeLeft = COUNT_DOWN_TIME,0,-1 do
					task.wait(1)
					self.Timer = timeLeft
					self:UpdateSurfaceGui()
				end
				
				self.Status = "Teleporting"
				self:UpdateSurfaceGui()

				self[TELEPORT_QUEUE_SYMBOL]:Flush()
				self:UpdateSurfaceGui()

				task.wait(4)
			end
		end)
	end
	do --handles queue updates
		self[TELEPORT_QUEUE_SYMBOL]:Observe(function()
			self:UpdateSurfaceGui()
		end)
	
		self[TOUCH_CONNECTION_SYMBOL] = self.Instance.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				self[TELEPORT_QUEUE_SYMBOL]:Add(player)
			end
		end)
	end
end

function TeleportQueueComponent:Stop()
	self[TELEPORT_QUEUE_SYMBOL]:Destroy()
	if self[TOUCH_CONNECTION_SYMBOL] then
		self[TOUCH_CONNECTION_SYMBOL]:Disconnect()
		self[TOUCH_CONNECTION_SYMBOL] = nil
	end
	if self[TIMER_LOOP_THREAD] then
		task.cancel(self[TIMER_LOOP_THREAD])
		self[TIMER_LOOP_THREAD] = nil
	end
	self.SurfaceGui:Destroy()
end

return TeleportQueueComponent