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