local N={}N.__index=N local function H()local N=Instance.new("\070\114\097\109\101")N.Selectable=false N.Size=UDim2.new(1,0,1,0)N.BackgroundTransparency=1 local H=Instance.new("\083\099\114\111\108\108\105\110\103\070\114\097\109\101")H.Selectable=ChatSettings.GamepadNavigationEnabled H.Name="\083\099\114\111\108\108\101\114"H.BackgroundTransparency=1 H.BorderSizePixel=0 H.Position=UDim2.new(0,0,0,3)H.Size=UDim2.new(1,-4,1,-6)H.CanvasSize=UDim2.new(0,0,0,0)H.ScrollBarThickness=module.ScrollBarThickness H.Active=true H.Parent=N local W=Instance.new("\085\073\076\105\115\116\076\097\121\111\117\116")W.SortOrder=Enum.SortOrder.LayoutOrder W.Parent=H return N,H,W end function N.Destroy(N)N.GuiObject:Destroy()N.Destroyed=true end function N.SetActive(H,N)H.GuiObject.Visible=N end function N.UpdateMessageFiltered(H,N)local W=nil local r=1 local a=H.MessageObjectLog while#a>=r do local H=a[r]if H.ID==N.ID then W=H break end r=r+1 end if W then if UserFlagRemoveMessageOnTextFilterFailures then if N.Message==""then H:RemoveMessageAtIndex(r)else W.UpdateTextFunction(N)H:PositionMessageLabelInWindow(W,r)end else W.UpdateTextFunction(N)H:PositionMessageLabelInWindow(W,r)end end end function N.AddMessage(H,N)H:WaitUntilParentedCorrectly()local W=MessageLabelCreator:CreateMessageLabel(N,H.CurrentChannelName)if W==nil then return end table.insert(H.MessageObjectLog,W)H:PositionMessageLabelInWindow(W,#H.MessageObjectLog)end function N.RemoveMessageAtIndex(H,N)H:WaitUntilParentedCorrectly()local W=H.MessageObjectLog[N]if W then W:Destroy()table.remove(H.MessageObjectLog,N)end end function N.AddMessageAtIndex(W,N,H)local r=MessageLabelCreator:CreateMessageLabel(N,W.CurrentChannelName)if r==nil then return end table.insert(W.MessageObjectLog,H,r)W:PositionMessageLabelInWindow(r,H)end function N.RemoveLastMessage(N)N:WaitUntilParentedCorrectly()local H=N.MessageObjectLog[1]H:Destroy()table.remove(N.MessageObjectLog,1)end function N.IsScrolledDown(N)local H=N.Scroller.CanvasSize.Y.Offset local W=N.Scroller.AbsoluteWindowSize.Y local r=N.Scroller.CanvasPosition.Y return H<W or H-r<=W+5 end function N.UpdateMessageTextHeight(H,N)local W=N.BaseFrame for r=1,10,1 do if N.BaseMessage.TextFits then break end local a=H.Scroller.AbsoluteSize.X-r W.Size=UDim2.new(1,0,0,N.GetHeightFunction(a))end end function N.PositionMessageLabelInWindow(W,N,H)W:WaitUntilParentedCorrectly()local r=W:IsScrolledDown()local a=N.BaseFrame local g=1 if W.MessageObjectLog[H-1]then if H==#W.MessageObjectLog then g=W.MessageObjectLog[H-1].BaseFrame.LayoutOrder+1 else g=W.MessageObjectLog[H-1].BaseFrame.LayoutOrder end end a.LayoutOrder=g a.Size=UDim2.new(1,0,0,N.GetHeightFunction(W.Scroller.AbsoluteSize.X))a.Parent=W.Scroller if N.BaseMessage then W:UpdateMessageTextHeight(N)end if r then W.Scroller.CanvasPosition=Vector2.new(0,math.max(0,W.Scroller.CanvasSize.Y.Offset-W.Scroller.AbsoluteSize.Y))end end function N.ReorderAllMessages(N)N:WaitUntilParentedCorrectly()if N.GuiObject.AbsoluteSize.Y<1 then return end local H=N.Scroller.CanvasPosition local W=N:IsScrolledDown()for H,W in pairs(N.MessageObjectLog)do N:UpdateMessageTextHeight(W)end if not W then N.Scroller.CanvasPosition=H else N.Scroller.CanvasPosition=Vector2.new(0,math.max(0,N.Scroller.CanvasSize.Y.Offset-N.Scroller.AbsoluteSize.Y))end end function N.Clear(N)for N,H in pairs(N.MessageObjectLog)do H:Destroy()end N.MessageObjectLog={}end function N.SetCurrentChannelName(H,N)H.CurrentChannelName=N end function N.FadeOutBackground(H,N) end function N.FadeInBackground(H,N) end function N.FadeOutText(H,N)for W=1,#H.MessageObjectLog,1 do if H.MessageObjectLog[W].FadeOutFunction then H.MessageObjectLog[W].FadeOutFunction(N,CurveUtil)end end end function N.FadeInText(H,N)for W=1,#H.MessageObjectLog,1 do if H.MessageObjectLog[W].FadeInFunction then H.MessageObjectLog[W].FadeInFunction(N,CurveUtil)end end end function N.Update(H,N)for W=1,#H.MessageObjectLog,1 do if H.MessageObjectLog[W].UpdateAnimFunction then H.MessageObjectLog[W].UpdateAnimFunction(N,CurveUtil)end end end

local methods = {}
methods.__index = methods


local function CreateGuiObjects()
	local BaseFrame = Instance.new("Frame")
	BaseFrame.Selectable = false
	BaseFrame.Size = UDim2.new(1, 0, 1, 0)
	BaseFrame.BackgroundTransparency = 1

	local Scroller = Instance.new("ScrollingFrame")
	Scroller.Selectable = ChatSettings.GamepadNavigationEnabled
	Scroller.Name = "Scroller"
	Scroller.BackgroundTransparency = 1
	Scroller.BorderSizePixel = 0
	Scroller.Position = UDim2.new(0, 0, 0, 3)
	Scroller.Size = UDim2.new(1, -4, 1, -6)
	Scroller.CanvasSize = UDim2.new(0, 0, 0, 0)
	Scroller.ScrollBarThickness = module.ScrollBarThickness
	Scroller.Active = true
	Scroller.Parent = BaseFrame

	local Layout = Instance.new("UIListLayout")
	Layout.SortOrder = Enum.SortOrder.LayoutOrder
	Layout.Parent = Scroller

	return BaseFrame, Scroller, Layout
end

function methods:Destroy()
	self.GuiObject:Destroy()
	self.Destroyed = true
end

function methods:SetActive(active)
	self.GuiObject.Visible = active
end

function methods:UpdateMessageFiltered(messageData)
	local messageObject = nil
	local searchIndex = 1
	local searchTable = self.MessageObjectLog

	while (#searchTable >= searchIndex) do
		local obj = searchTable[searchIndex]

		if obj.ID == messageData.ID then
			messageObject = obj
			break
		end

		searchIndex = searchIndex + 1
	end

	if messageObject then
		if UserFlagRemoveMessageOnTextFilterFailures then
			if messageData.Message == "" then
				self:RemoveMessageAtIndex(searchIndex)
			else
				messageObject.UpdateTextFunction(messageData)
				self:PositionMessageLabelInWindow(messageObject, searchIndex)
			end
		else
			messageObject.UpdateTextFunction(messageData)
			self:PositionMessageLabelInWindow(messageObject, searchIndex)
		end
	end
end

function methods:AddMessage(messageData)
	self:WaitUntilParentedCorrectly()

	local messageObject = MessageLabelCreator:CreateMessageLabel(messageData, self.CurrentChannelName)
	if messageObject == nil then
		return
	end

	table.insert(self.MessageObjectLog, messageObject)
	self:PositionMessageLabelInWindow(messageObject, #self.MessageObjectLog)
end

function methods:RemoveMessageAtIndex(index)
	self:WaitUntilParentedCorrectly()

	local removeThis = self.MessageObjectLog[index]

	if removeThis then
		removeThis:Destroy()
		table.remove(self.MessageObjectLog, index)
	end
end

function methods:AddMessageAtIndex(messageData, index)
	local messageObject = MessageLabelCreator:CreateMessageLabel(messageData, self.CurrentChannelName)
	if messageObject == nil then
		return
	end

	table.insert(self.MessageObjectLog, index, messageObject)

	self:PositionMessageLabelInWindow(messageObject, index)
end

function methods:RemoveLastMessage()
	self:WaitUntilParentedCorrectly()

	local lastMessage = self.MessageObjectLog[1]

	lastMessage:Destroy()
	table.remove(self.MessageObjectLog, 1)
end

function methods:IsScrolledDown()
	local yCanvasSize = self.Scroller.CanvasSize.Y.Offset
	local yContainerSize = self.Scroller.AbsoluteWindowSize.Y
	local yScrolledPosition = self.Scroller.CanvasPosition.Y

	return (yCanvasSize < yContainerSize or
		yCanvasSize - yScrolledPosition <= yContainerSize + 5)
end

function methods:UpdateMessageTextHeight(messageObject)
	local baseFrame = messageObject.BaseFrame
	for i = 1, 10 do
		if messageObject.BaseMessage.TextFits then
			break
		end

		local trySize = self.Scroller.AbsoluteSize.X - i
		baseFrame.Size = UDim2.new(1, 0, 0, messageObject.GetHeightFunction(trySize))
	end
end

function methods:PositionMessageLabelInWindow(messageObject, index)
	self:WaitUntilParentedCorrectly()

	local wasScrolledDown = self:IsScrolledDown()

	local baseFrame = messageObject.BaseFrame

	local layoutOrder = 1
	if self.MessageObjectLog[index - 1] then
		if index == #self.MessageObjectLog then
			layoutOrder = self.MessageObjectLog[index - 1].BaseFrame.LayoutOrder + 1
		else
			layoutOrder = self.MessageObjectLog[index - 1].BaseFrame.LayoutOrder
		end
	end
	baseFrame.LayoutOrder = layoutOrder

	baseFrame.Size = UDim2.new(1, 0, 0, messageObject.GetHeightFunction(self.Scroller.AbsoluteSize.X))
	baseFrame.Parent = self.Scroller

	if messageObject.BaseMessage then
		self:UpdateMessageTextHeight(messageObject)
	end

	if wasScrolledDown then
		self.Scroller.CanvasPosition = Vector2.new(
			0, math.max(0, self.Scroller.CanvasSize.Y.Offset - self.Scroller.AbsoluteSize.Y))
	end
end

function methods:ReorderAllMessages()
	self:WaitUntilParentedCorrectly()

	--// Reordering / reparenting with a size less than 1 causes weird glitches to happen
	-- with scrolling as repositioning happens.
	if self.GuiObject.AbsoluteSize.Y < 1 then return end

	local oldCanvasPositon = self.Scroller.CanvasPosition
	local wasScrolledDown = self:IsScrolledDown()

	for _, messageObject in pairs(self.MessageObjectLog) do
		self:UpdateMessageTextHeight(messageObject)
	end

	if not wasScrolledDown then
		self.Scroller.CanvasPosition = oldCanvasPositon
	else
		self.Scroller.CanvasPosition = Vector2.new(
			0, math.max(0, self.Scroller.CanvasSize.Y.Offset - self.Scroller.AbsoluteSize.Y))
	end
end

function methods:Clear()
	for _, v in pairs(self.MessageObjectLog) do
		v:Destroy()
	end
	self.MessageObjectLog = {}
end

function methods:SetCurrentChannelName(name)
	self.CurrentChannelName = name
end

function methods:FadeOutBackground(duration)
	--// Do nothing
end

function methods:FadeInBackground(duration)
	--// Do nothing
end

function methods:FadeOutText(duration)
	for i = 1, #self.MessageObjectLog do
		if self.MessageObjectLog[i].FadeOutFunction then
			self.MessageObjectLog[i].FadeOutFunction(duration, CurveUtil)
		end
	end
end

function methods:FadeInText(duration)
	for i = 1, #self.MessageObjectLog do
		if self.MessageObjectLog[i].FadeInFunction then
			self.MessageObjectLog[i].FadeInFunction(duration, CurveUtil)
		end
	end
end

function methods:Update(dtScale)
	for i = 1, #self.MessageObjectLog do
		if self.MessageObjectLog[i].UpdateAnimFunction then
			self.MessageObjectLog[i].UpdateAnimFunction(dtScale, CurveUtil)
		end
	end
end
