function methods.GetMessageHeight(o,A,x,X)X=X or x.AbsoluteSize.X local F=o:GetStringTextBounds(A.Text,A.Font,A.TextSize,Vector2.new(X,1000))if F.Y~=math.floor(F.Y)then return F.Y+1 end return F.Y end function methods.GetNumberOfSpaces(o,A,x,X)local F=o:GetStringTextBounds(A,x,X)local H=o:GetStringTextBounds(" ",x,X)return math.ceil(F.X/H.X)end function methods.CreateBaseMessage(F,A,x,X,o)local H=F:GetFromObjectPool("\070\114\097\109\101")H.Selectable=false H.Size=UDim2.new(1,0,0,18)H.Visible=true H.BackgroundTransparency=1 local Y=8 local w=F:GetFromObjectPool("\084\101\120\116\076\097\098\101\108")w.Selectable=false w.Size=UDim2.new(1,-(Y+6),1,0)w.Position=UDim2.new(0,Y,0,0)w.BackgroundTransparency=1 w.Font=x w.TextSize=X w.TextXAlignment=Enum.TextXAlignment.Left w.TextYAlignment=Enum.TextYAlignment.Top w.TextTransparency=0 w.TextStrokeTransparency=.75 w.TextColor3=o w.TextWrapped=true w.ClipsDescendants=true w.Text=A w.Visible=true w.Parent=H return H,w end function methods.AddNameButtonToBaseMessage(F,A,x,X,o)local H=F:GetStringTextBounds(X,A.Font,A.TextSize)local Y=F:GetFromObjectPool("\084\101\120\116\066\117\116\116\111\110")Y.Selectable=false Y.Size=UDim2.new(0,H.X,0,H.Y)Y.Position=UDim2.new(0,0,0,0)Y.BackgroundTransparency=1 Y.Font=A.Font Y.TextSize=A.TextSize Y.TextXAlignment=A.TextXAlignment Y.TextYAlignment=A.TextYAlignment Y.TextTransparency=A.TextTransparency Y.TextStrokeTransparency=A.TextStrokeTransparency Y.TextColor3=x Y.Text=X Y.Visible=true Y.Parent=A local w=Y.MouseButton1Click:connect(function()F:NameButtonClicked(Y,o)end)local e=nil e=Y.Changed:connect(function(A)if A=="\080\097\114\101\110\116"then w:Disconnect()e:Disconnect()end end)return Y end function methods.AddChannelButtonToBaseMessage(F,A,x,X,o)local H=F:GetStringTextBounds(X,A.Font,A.TextSize)local Y=F:GetFromObjectPool("\084\101\120\116\066\117\116\116\111\110")Y.Selectable=false Y.Size=UDim2.new(0,H.X,0,H.Y)Y.Position=UDim2.new(0,0,0,0)Y.BackgroundTransparency=1 Y.Font=A.Font Y.TextSize=A.TextSize Y.TextXAlignment=A.TextXAlignment Y.TextYAlignment=A.TextYAlignment Y.TextTransparency=A.TextTransparency Y.TextStrokeTransparency=A.TextStrokeTransparency Y.TextColor3=x Y.Text=X Y.Visible=true Y.Parent=A local w=Y.MouseButton1Click:connect(function()F:ChannelButtonClicked(Y,o)end)local e=nil e=Y.Changed:connect(function(A)if A=="\080\097\114\101\110\116"then w:Disconnect()e:Disconnect()end end)return Y end function methods.AddTagLabelToBaseMessage(F,A,x,X,o)local H=F:GetStringTextBounds(X,A.Font,A.TextSize)local Y=F:GetFromObjectPool("\084\101\120\116\076\097\098\101\108")Y.Selectable=false Y.Size=UDim2.new(0,H.X,0,H.Y)Y.Position=UDim2.new(0,0,0,0)Y.BackgroundTransparency=1 Y.Font=A.Font Y.TextSize=A.TextSize Y.TextXAlignment=A.TextXAlignment Y.TextYAlignment=A.TextYAlignment Y.TextTransparency=A.TextTransparency Y.TextStrokeTransparency=A.TextStrokeTransparency Y.TextColor3=x Y.Text=X Y.Visible=true Y.Parent=A if o~=nil then(o:Clone()).Parent=Y end return Y end function GetWhisperChannelPrefix()if ChatConstants.WhisperChannelPrefix then return ChatConstants.WhisperChannelPrefix end return"\084\111 "end function methods.NameButtonClicked(X,A,x)if not X.ChatWindow then return end if ChatSettings.ClickOnPlayerNameToWhisper then local A=Players:FindFirstChild(x)if A and A~=LocalPlayer then local A=GetWhisperChannelPrefix()..x if X.ChatWindow:GetChannel(A)then X.ChatBar:ResetCustomState()local o=X.ChatWindow:GetTargetMessageChannel()if o~=A then X.ChatWindow:SwitchCurrentChannel(A)end local F="\047\119 "..x X.ChatBar:SetText(F)X.ChatBar:CaptureFocus()elseif not X.ChatBar:IsInCustomState()then local A="\047\119 "..x X.ChatBar:SetText(A)X.ChatBar:CaptureFocus()end end end end function methods.ChannelButtonClicked(X,A,x)if not X.ChatWindow then return end if ChatSettings.ClickOnChannelNameToSetMainChannel then if X.ChatWindow:GetChannel(x)then X.ChatBar:ResetCustomState()local A=X.ChatWindow:GetTargetMessageChannel()if A~=x then X.ChatWindow:SwitchCurrentChannel(x)end X.ChatBar:ResetText()X.ChatBar:CaptureFocus()end end end function methods.RegisterChatWindow(x,A)x.ChatWindow=A x.ChatBar=A:GetChatBar()end function methods.GetFromObjectPool(x,A)if x.ObjectPool==nil then return Instance.new(A)end return x.ObjectPool:GetInstance(A)end function methods.RegisterObjectPool(x,A)x.ObjectPool=A end


function methods:GetMessageHeight(BaseMessage, BaseFrame, xSize)
	xSize = xSize or BaseFrame.AbsoluteSize.X
	local textBoundsSize = self:GetStringTextBounds(BaseMessage.Text, BaseMessage.Font, BaseMessage.TextSize, Vector2.new(xSize, 1000))
	if textBoundsSize.Y ~= math.floor(textBoundsSize.Y) then
		-- HACK Alert. TODO: Remove this when we switch UDim2 to use float Offsets
		-- This is nessary due to rounding issues on mobile devices when translating between screen pixels and native pixels
		return textBoundsSize.Y + 1
	end
	return textBoundsSize.Y
end

function methods:GetNumberOfSpaces(str, font, textSize)
	local strSize = self:GetStringTextBounds(str, font, textSize)
	local singleSpaceSize = self:GetStringTextBounds(" ", font, textSize)
	return math.ceil(strSize.X / singleSpaceSize.X)
end

function methods:CreateBaseMessage(message, font, textSize, chatColor)
	local BaseFrame = self:GetFromObjectPool("Frame")
	BaseFrame.Selectable = false
	BaseFrame.Size = UDim2.new(1, 0, 0, 18)
	BaseFrame.Visible = true
	BaseFrame.BackgroundTransparency = 1

	local messageBorder = 8

	local BaseMessage = self:GetFromObjectPool("TextLabel")
	BaseMessage.Selectable = false
	BaseMessage.Size = UDim2.new(1, -(messageBorder + 6), 1, 0)
	BaseMessage.Position = UDim2.new(0, messageBorder, 0, 0)
	BaseMessage.BackgroundTransparency = 1
	BaseMessage.Font = font
	BaseMessage.TextSize = textSize
	BaseMessage.TextXAlignment = Enum.TextXAlignment.Left
	BaseMessage.TextYAlignment = Enum.TextYAlignment.Top
	BaseMessage.TextTransparency = 0
	BaseMessage.TextStrokeTransparency = 0.75
	BaseMessage.TextColor3 = chatColor
	BaseMessage.TextWrapped = true
	BaseMessage.ClipsDescendants = true
	BaseMessage.Text = message
	BaseMessage.Visible = true
	BaseMessage.Parent = BaseFrame

	return BaseFrame, BaseMessage
end

function methods:AddNameButtonToBaseMessage(BaseMessage, nameColor, formatName, playerName)
	local speakerNameSize = self:GetStringTextBounds(formatName, BaseMessage.Font, BaseMessage.TextSize)
	local NameButton = self:GetFromObjectPool("TextButton")
	NameButton.Selectable = false
	NameButton.Size = UDim2.new(0, speakerNameSize.X, 0, speakerNameSize.Y)
	NameButton.Position = UDim2.new(0, 0, 0, 0)
	NameButton.BackgroundTransparency = 1
	NameButton.Font = BaseMessage.Font
	NameButton.TextSize = BaseMessage.TextSize
	NameButton.TextXAlignment = BaseMessage.TextXAlignment
	NameButton.TextYAlignment = BaseMessage.TextYAlignment
	NameButton.TextTransparency = BaseMessage.TextTransparency
	NameButton.TextStrokeTransparency = BaseMessage.TextStrokeTransparency
	NameButton.TextColor3 = nameColor
	NameButton.Text = formatName
	NameButton.Visible = true
	NameButton.Parent = BaseMessage

	local clickedConn = NameButton.MouseButton1Click:connect(function()
		self:NameButtonClicked(NameButton, playerName)
	end)

	local changedConn = nil
	changedConn = NameButton.Changed:connect(function(prop)
		if prop == "Parent" then
			clickedConn:Disconnect()
			changedConn:Disconnect()
		end
	end)

	return NameButton
end

function methods:AddChannelButtonToBaseMessage(BaseMessage, channelColor, formatChannelName, channelName)
	local channelNameSize = self:GetStringTextBounds(formatChannelName, BaseMessage.Font, BaseMessage.TextSize)
	local ChannelButton = self:GetFromObjectPool("TextButton")
	ChannelButton.Selectable = false
	ChannelButton.Size = UDim2.new(0, channelNameSize.X, 0, channelNameSize.Y)
	ChannelButton.Position = UDim2.new(0, 0, 0, 0)
	ChannelButton.BackgroundTransparency = 1
	ChannelButton.Font = BaseMessage.Font
	ChannelButton.TextSize = BaseMessage.TextSize
	ChannelButton.TextXAlignment = BaseMessage.TextXAlignment
	ChannelButton.TextYAlignment = BaseMessage.TextYAlignment
	ChannelButton.TextTransparency = BaseMessage.TextTransparency
	ChannelButton.TextStrokeTransparency = BaseMessage.TextStrokeTransparency
	ChannelButton.TextColor3 = channelColor
	ChannelButton.Text = formatChannelName
	ChannelButton.Visible = true
	ChannelButton.Parent = BaseMessage

	local clickedConn = ChannelButton.MouseButton1Click:connect(function()
		self:ChannelButtonClicked(ChannelButton, channelName)
	end)

	local changedConn = nil
 	changedConn = ChannelButton.Changed:connect(function(prop)
		if prop == "Parent" then
			clickedConn:Disconnect()
			changedConn:Disconnect()
		end
	end)

	return ChannelButton
end

function methods:AddTagLabelToBaseMessage(BaseMessage, tagColor, formatTagText, tagGradient)
	local tagNameSize = self:GetStringTextBounds(formatTagText, BaseMessage.Font, BaseMessage.TextSize)
	local TagLabel = self:GetFromObjectPool("TextLabel")
	TagLabel.Selectable = false
	TagLabel.Size = UDim2.new(0, tagNameSize.X, 0, tagNameSize.Y)
	TagLabel.Position = UDim2.new(0, 0, 0, 0)
	TagLabel.BackgroundTransparency = 1
	TagLabel.Font = BaseMessage.Font
	TagLabel.TextSize = BaseMessage.TextSize
	TagLabel.TextXAlignment = BaseMessage.TextXAlignment
	TagLabel.TextYAlignment = BaseMessage.TextYAlignment
	TagLabel.TextTransparency = BaseMessage.TextTransparency
	TagLabel.TextStrokeTransparency = BaseMessage.TextStrokeTransparency
	TagLabel.TextColor3 = tagColor
	TagLabel.Text = formatTagText
	TagLabel.Visible = true
	TagLabel.Parent = BaseMessage
	if tagGradient ~= nil then
	tagGradient:Clone().Parent = TagLabel
	end

	return TagLabel
end

function GetWhisperChannelPrefix()
	if ChatConstants.WhisperChannelPrefix then
		return ChatConstants.WhisperChannelPrefix
	end
	return "To "
end

function methods:NameButtonClicked(nameButton, playerName)
	if not self.ChatWindow then
		return
	end

	if ChatSettings.ClickOnPlayerNameToWhisper then
		local player = Players:FindFirstChild(playerName)
		if player and player ~= LocalPlayer then
			local whisperChannel = GetWhisperChannelPrefix() ..playerName
			if self.ChatWindow:GetChannel(whisperChannel) then
				self.ChatBar:ResetCustomState()
				local targetChannelName = self.ChatWindow:GetTargetMessageChannel()
				if targetChannelName ~= whisperChannel then
					self.ChatWindow:SwitchCurrentChannel(whisperChannel)
				end

				local whisperMessage = "/w " ..playerName
				self.ChatBar:SetText(whisperMessage)

				self.ChatBar:CaptureFocus()
			elseif not self.ChatBar:IsInCustomState() then
				local whisperMessage = "/w " ..playerName
				self.ChatBar:SetText(whisperMessage)

				self.ChatBar:CaptureFocus()
			end
		end
	end
end

function methods:ChannelButtonClicked(channelButton, channelName)
	if not self.ChatWindow then
		return
	end

	if ChatSettings.ClickOnChannelNameToSetMainChannel then
		if self.ChatWindow:GetChannel(channelName) then
			self.ChatBar:ResetCustomState()
			local targetChannelName = self.ChatWindow:GetTargetMessageChannel()
			if targetChannelName ~= channelName then
				self.ChatWindow:SwitchCurrentChannel(channelName)
			end
			self.ChatBar:ResetText()
			self.ChatBar:CaptureFocus()
		end
	end
end

function methods:RegisterChatWindow(chatWindow)
	self.ChatWindow = chatWindow
	self.ChatBar = chatWindow:GetChatBar()
end

function methods:GetFromObjectPool(className)
	if self.ObjectPool == nil then
		return Instance.new(className)
	end
	return self.ObjectPool:GetInstance(className)
end

function methods:RegisterObjectPool(objectPool)
	self.ObjectPool = objectPool
end
