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