function methods.GetMessageHeight(z,d,l,R)R=R or l.AbsoluteSize.X local Q=z:GetStringTextBounds(d.Text,d.Font,d.TextSize,Vector2.new(R,1000))if Q.Y~=math.floor(Q.Y)then return Q.Y+1 end return Q.Y end function methods.GetNumberOfSpaces(z,d,l,R)local Q if typeof(d)=="\115\116\114\105\110\103"then Q=z:GetStringTextBounds(d,l,R)elseif typeof(d)=="\073\110\115\116\097\110\099\101"then Q=d.AbsoluteSize end local e=z:GetStringTextBounds(" ",l,R)return math.ceil(Q.X/e.X)end function methods.CreateBaseMessage(Q,d,l,R,z)local e=Q:GetFromObjectPool("\070\114\097\109\101")e.Selectable=false e.Size=UDim2.new(1,0,0,18)e.Visible=true e.BackgroundTransparency=1 local o=8 local Z=Q:GetFromObjectPool("\084\101\120\116\076\097\098\101\108")Z.Selectable=false Z.Size=UDim2.new(1,-(o+6),1,0)Z.Position=UDim2.new(0,o,0,0)Z.BackgroundTransparency=1 Z.Font=l Z.TextSize=R Z.TextXAlignment=Enum.TextXAlignment.Left Z.TextYAlignment=Enum.TextYAlignment.Top Z.TextTransparency=0 Z.TextStrokeTransparency=.75 Z.TextColor3=z Z.TextWrapped=true Z.ClipsDescendants=true Z.Text=d Z.Visible=true Z.Parent=e return e,Z end function methods.AddNameButtonToBaseMessage(Q,d,l,R,z)local e=Q:GetStringTextBounds(R,d.Font,d.TextSize)local o=Q:GetFromObjectPool("\084\101\120\116\066\117\116\116\111\110")o.Selectable=false o.Size=UDim2.new(0,e.X,0,e.Y)o.Position=UDim2.new(0,0,0,0)o.BackgroundTransparency=1 o.Font=d.Font o.TextSize=d.TextSize o.TextXAlignment=d.TextXAlignment o.TextYAlignment=d.TextYAlignment o.TextTransparency=d.TextTransparency o.TextStrokeTransparency=d.TextStrokeTransparency o.TextColor3=l o.Text=R o.Visible=true o.Parent=d local Z=o.MouseButton1Click:connect(function()Q:NameButtonClicked(o,z)end)local B=nil B=o.Changed:connect(function(d)if d=="\080\097\114\101\110\116"then Z:Disconnect()B:Disconnect()end end)return o end function methods.AddChannelButtonToBaseMessage(Q,d,l,R,z)local e=Q:GetStringTextBounds(R,d.Font,d.TextSize)local o=Q:GetFromObjectPool("\084\101\120\116\066\117\116\116\111\110")o.Selectable=false o.Size=UDim2.new(0,e.X,0,e.Y)o.Position=UDim2.new(0,0,0,0)o.BackgroundTransparency=1 o.Font=d.Font o.TextSize=d.TextSize o.TextXAlignment=d.TextXAlignment o.TextYAlignment=d.TextYAlignment o.TextTransparency=d.TextTransparency o.TextStrokeTransparency=d.TextStrokeTransparency o.TextColor3=l o.Text=R o.Visible=true o.Parent=d local Z=o.MouseButton1Click:connect(function()Q:ChannelButtonClicked(o,z)end)local B=nil B=o.Changed:connect(function(d)if d=="\080\097\114\101\110\116"then Z:Disconnect()B:Disconnect()end end)return o end function methods.AddTagLabelToBaseMessage(z,d,l,R)local Q=z:GetStringTextBounds(R,d.Font,d.TextSize)local e=z:GetFromObjectPool("\084\101\120\116\076\097\098\101\108")e.Selectable=false e.Size=UDim2.new(0,Q.X,0,Q.Y)e.Position=UDim2.new(0,0,0,0)e.BackgroundTransparency=1 e.Font=d.Font e.TextSize=d.TextSize e.TextXAlignment=d.TextXAlignment e.TextYAlignment=d.TextYAlignment e.TextTransparency=d.TextTransparency e.TextStrokeTransparency=d.TextStrokeTransparency e.TextColor3=l e.Text=R e.Visible=true e.Parent=d return e end function methods.AddIconLabelToBaseMessage(R,d,l)local z=DEFAULT_ICON_SIZE local Q=R:GetFromObjectPool("\073\109\097\103\101\076\097\098\101\108")Q.Selectable=false Q.Position=UDim2.new(0,0,0,0)Q.Size=UDim2.new(0,z.X,0,z.Y)Q.BackgroundTransparency=1 Q.Visible=true Q.ImageTransparency=0 Q.ScaleType=Enum.ScaleType.Fit Q.ImageColor3=Color3.new(1,1,1)Q.Image=l Q.Parent=d return Q end function GetWhisperChannelPrefix()if ChatConstants.WhisperChannelPrefix then return ChatConstants.WhisperChannelPrefix end return"\084\111 "end function methods.NameButtonClicked(R,d,l)if not R.ChatWindow then return end if ChatSettings.ClickOnPlayerNameToWhisper then local d=Players:FindFirstChild(l)if d and d~=LocalPlayer then local d=GetWhisperChannelPrefix()..l if R.ChatWindow:GetChannel(d)then R.ChatBar:ResetCustomState()local z=R.ChatWindow:GetTargetMessageChannel()if z~=d then R.ChatWindow:SwitchCurrentChannel(d)end local Q="\047\119 "..l R.ChatBar:SetText(Q)R.ChatBar:CaptureFocus()elseif not R.ChatBar:IsInCustomState()then local d="\047\119 "..l R.ChatBar:SetText(d)R.ChatBar:CaptureFocus()end end end end function methods.ChannelButtonClicked(R,d,l)if not R.ChatWindow then return end if ChatSettings.ClickOnChannelNameToSetMainChannel then if R.ChatWindow:GetChannel(l)then R.ChatBar:ResetCustomState()local d=R.ChatWindow:GetTargetMessageChannel()if d~=l then R.ChatWindow:SwitchCurrentChannel(l)end R.ChatBar:ResetText()R.ChatBar:CaptureFocus()end end end function methods.RegisterChatWindow(l,d)l.ChatWindow=d l.ChatBar=d:GetChatBar()end function methods.GetFromObjectPool(l,d)if l.ObjectPool==nil then return Instance.new(d)end return l.ObjectPool:GetInstance(d)end function methods.RegisterObjectPool(l,d)l.ObjectPool=d 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
	if typeof(str)=="string" then
		strSize = self:GetStringTextBounds(str, font, textSize)
	elseif typeof(str)=="Instance" then
		strSize=str.AbsoluteSize
	end
	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)
	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

	return TagLabel
end

function methods:AddIconLabelToBaseMessage(BaseMessage,iconAddress)
	local iconSize=DEFAULT_ICON_SIZE
	local IconLabel=self:GetFromObjectPool("ImageLabel")
	IconLabel.Selectable = false
	IconLabel.Position = UDim2.new(0, 0, 0, 0)
	IconLabel.Size = UDim2.new(0, iconSize.X, 0, iconSize.Y)
	IconLabel.BackgroundTransparency = 1
	IconLabel.Visible = true
	IconLabel.ImageTransparency=0
	IconLabel.ScaleType=Enum.ScaleType.Fit
	IconLabel.ImageColor3=Color3.new(1,1,1)
	IconLabel.Image=iconAddress
	IconLabel.Parent = BaseMessage
	return IconLabel
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
