------------------------------------------------


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

