function Lobby.resetSelectedObject(k)if UserInputService.GamepadEnabled then GuiService.SelectedObject=k.navigationsRefs[1]:getValue()end end function Lobby.startMatchmaking(f,k)f:setState({status="\099\111\110\110\101\099\116\105\110\103"})local v={type="\074\079\073\078",queue_selection=k}f.joinEvent:FireServer(v)f:resetSelectedObject()end function Lobby.stopMatchmaking(k)k:setState({status="\114\101\097\100\121"})local f={type="\067\065\078\067\069\076"}k.joinEvent:FireServer(f)k:resetSelectedObject()end function Lobby.init(k)local f=Util.playerIsBot(Players.LocalPlayer)k:setState({isBot=f})local v={}for k,f in ipairs(Conf.ui_mode_list)do table.insert(v,Roact.createRef())end k.navigationsRefs=v end function Lobby.didMount(k)k.statusEvent=ReplicatedStorage:WaitForChild("\083\116\097\116\117\115\069\118\101\110\116")k.joinEvent=ReplicatedStorage:WaitForChild("\074\111\105\110\069\118\101\110\116")k.connection=k.statusEvent.OnClientEvent:Connect(function(f)k:setState({status=f.canceled and"\114\101\097\100\121";statusText=f.statusText})end)if k.state.isBot then k:startMatchmaking()end k:resetSelectedObject()end function Lobby.willUnmount(k)k.connection:Disconnect()end function Lobby.render(k)local f={}local v=k.state.status or"\114\101\097\100\121"if k.state.isBot then f.botLabel=Roact.createElement("\084\101\120\116\076\097\098\101\108",{Text="\084\104\105\115 \112\108\097\121\101\114 \104\097\115 \098\101\101\110 \102\108\097\103\103\101\100 \097\115 \097 \116\101\115\116 \098\111\116";Position=UDim2.new(.5,0,.5,0);Size=UDim2.new(0,500,0,26)})else if v=="\114\101\097\100\121"then f.Layout=Roact.createElement("\085\073\076\105\115\116\076\097\121\111\117\116",{SortOrder=Enum.SortOrder.LayoutOrder,FillDirection=Enum.FillDirection.Vertical,Padding=UDim.new(0,20)})for v,w in ipairs(Conf.ui_mode_list)do local b=Conf.modes[w]f["\112\108\097\121\066\117\116\116\111\110\046"..w]=Roact.createElement("\084\101\120\116\066\117\116\116\111\110",{[Roact.Ref]=k.navigationsRefs[v];NextSelectionUp=k.navigationsRefs[v-1];NextSelectionDown=k.navigationsRefs[v+1],Size=UDim2.new(0,300,0,50),Font=Enum.Font.GothamBlack,TextSize=32;BorderSizePixel=3,Text=b.title;BackgroundColor3=Color3.fromRGB(255,239,39);[Roact.Event.Activated]=function()k:startMatchmaking(w)end;LayoutOrder=v})end else f.cancelButton=Roact.createElement("\084\101\120\116\066\117\116\116\111\110",{[Roact.Ref]=k.navigationsRefs[1];Size=UDim2.new(0,300,0,50),Font=Enum.Font.GothamBlack,TextSize=32;BorderSizePixel=3,Text="\067\065\078\067\069\076",BackgroundColor3=Color3.fromRGB(255,200,39);[Roact.Event.Activated]=function()k:stopMatchmaking()end})end end if k.state.statusText then f.joinStatus=Roact.createElement("\084\101\120\116\076\097\098\101\108",{Text=k.state.statusText})end return Roact.createElement("\083\099\114\101\101\110\071\117\105",{IgnoreGuiInset=true,ResetOnSpawn=false,ZIndexBehavior=Enum.ZIndexBehavior.Sibling},{Roact.createElement("\070\114\097\109\101",{Transparency=1;AnchorPoint=Vector2.new(1,1),Size=UDim2.new(0,400,0,280);Position=UDim2.new(1,0,1,0)},f)})end


function Lobby:resetSelectedObject()
	if UserInputService.GamepadEnabled then
		GuiService.SelectedObject = self.navigationsRefs[1]:getValue()
	end
end

function Lobby:startMatchmaking(modeSelection)
	self:setState({
		status = 'connecting',
	})

	-- TEMP:  Forcing queue selection
	local joinData = {
		type = "JOIN",
		queue_selection = modeSelection,
	}

	self.joinEvent:FireServer(joinData)

	self:resetSelectedObject()
end

function Lobby:stopMatchmaking()
	self:setState({
		status = 'ready',
	})

	local data = {
		type = "CANCEL"
	}

	self.joinEvent:FireServer(data)

	self:resetSelectedObject()
end

function Lobby:init()
	local isBot = Util.playerIsBot(Players.LocalPlayer)

	self:setState({
		isBot = isBot,
	})

	local refs = {}

	for _, _ in ipairs(Conf.ui_mode_list) do
		table.insert(refs, Roact.createRef())
	end

	self.navigationsRefs = refs
end

function Lobby:didMount()
	self.statusEvent = ReplicatedStorage:WaitForChild("StatusEvent")
	self.joinEvent = ReplicatedStorage:WaitForChild("JoinEvent")

	self.connection = self.statusEvent.OnClientEvent:Connect(function(data)
		self:setState({
			status = data.canceled and 'ready',
			statusText = data.statusText,
		})
	end)

	if self.state.isBot then
		self:startMatchmaking()
	end

	self:resetSelectedObject()
end

function Lobby:willUnmount()
	self.connection:Disconnect()
end

function Lobby:render()

	local children = {}

	local status = self.state.status or 'ready'

	if self.state.isBot then
		children.botLabel = Roact.createElement("TextLabel", {
			Text = "This player has been flagged as a test bot",
			Position = UDim2.new(0.5, 0, 0.5, 0),
			Size = UDim2.new(0, 500, 0, 26),
		})

	else
		if status == 'ready' then
			children.Layout = Roact.createElement("UIListLayout", {
				SortOrder = Enum.SortOrder.LayoutOrder,
				FillDirection = Enum.FillDirection.Vertical,
				Padding = UDim.new(0, 20),
			})

			for i, mode in ipairs(Conf.ui_mode_list) do
				local data = Conf.modes[mode]

				children["playButton." .. mode] = Roact.createElement("TextButton", {
					[Roact.Ref] = self.navigationsRefs[i],
					NextSelectionUp = self.navigationsRefs[i - 1],
					NextSelectionDown = self.navigationsRefs[i + 1],
					Size = UDim2.new(0, 300, 0, 50),
					Font = Enum.Font.GothamBlack,
					TextSize = 32,
					BorderSizePixel = 3,
					Text = data.title,
					BackgroundColor3 = Color3.fromRGB(255, 239, 39),
					[Roact.Event.Activated] = function() self:startMatchmaking(mode) end,
					LayoutOrder = i,
				})
			end

		else
			children.cancelButton = Roact.createElement("TextButton", {
				[Roact.Ref] = self.navigationsRefs[1],
				Size = UDim2.new(0, 300, 0, 50),
				Font = Enum.Font.GothamBlack,
				TextSize = 32,
				BorderSizePixel = 3,
				Text = "CANCEL",
				BackgroundColor3 = Color3.fromRGB(255, 200, 39),
				[Roact.Event.Activated] = function() self:stopMatchmaking() end,
			})
		end

	end

	if self.state.statusText then
		children.joinStatus = Roact.createElement("TextLabel", {
			Text = self.state.statusText
		})
	end

	return Roact.createElement("ScreenGui", {
		IgnoreGuiInset = true,
		ResetOnSpawn = false,
		ZIndexBehavior = Enum.ZIndexBehavior.Sibling,
	}, {
		Roact.createElement("Frame", {
			Transparency = 1,
			AnchorPoint = Vector2.new(1, 1),
			Size = UDim2.new(0, 400, 0, 280),
			Position = UDim2.new(1, 0, 1, 0),
		}, children)
	})
end

