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

	return BaseFrame, Scroller
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:ReorderAllMessages()
	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)
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)

	local wasScrolledToBottom = self:IsScrolledDown()
	self:ReorderAllMessages()
	if wasScrolledToBottom then
		self.Scroller.CanvasPosition = Vector2.new(0, math.max(0, self.Scroller.CanvasSize.Y.Offset - self.Scroller.AbsoluteSize.Y))
	end
end

function methods:RemoveLastMessage()
	self:WaitUntilParentedCorrectly()

	local lastMessage = self.MessageObjectLog[1]
	local posOffset = UDim2.new(0, 0, 0, lastMessage.BaseFrame.AbsoluteSize.Y)

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

	for i, messageObject in pairs(self.MessageObjectLog) do
		messageObject.BaseFrame.Position = messageObject.BaseFrame.Position - posOffset
	end

	self.Scroller.CanvasSize = self.Scroller.CanvasSize - posOffset
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 min(x, y)
	return x < y and x or y
end

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

	local baseFrame = messageObject.BaseFrame

	baseFrame.Parent = self.Scroller
	baseFrame.Position = UDim2.new(0, 0, 0, self.Scroller.CanvasSize.Y.Offset)

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

	if messageObject.BaseMessage then
		local trySize = self.Scroller.AbsoluteSize.X
		local minTrySize = min(self.Scroller.AbsoluteSize.X - 10, 0)
		while not messageObject.BaseMessage.TextFits do
			trySize = trySize - 1
			if trySize < minTrySize then
				break
			end
			baseFrame.Size = UDim2.new(1, 0, 0, messageObject.GetHeightFunction(trySize))
		end
	end

	local isScrolledDown = self:IsScrolledDown()

	local add = UDim2.new(0, 0, 0, baseFrame.Size.Y.Offset)
	self.Scroller.CanvasSize = self.Scroller.CanvasSize + add

	if isScrolledDown 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()

	self.Scroller.CanvasSize = UDim2.new(0, 0, 0, 0)
	for i, messageObject in pairs(self.MessageObjectLog) do
		self:PositionMessageLabelInWindow(messageObject)
	end

	if not wasScrolledDown then
		self.Scroller.CanvasPosition = oldCanvasPositon
	end
end

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

	self.Scroller.CanvasSize = UDim2.new(0, 0, 0, 0)
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
