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