function chatBarFocusLost(R,w)DoBackgroundFadeIn()chatBarFocusChanged:Fire(false)if R then local R=(ChatBar:GetTextBox()).Text if ChatBar:IsInCustomState()then local w=ChatBar:GetCustomMessage()if w then R=w end local e=ChatBar:CustomStateProcessCompletedMessage(R)ChatBar:ResetCustomState()if e then return end end R=string.sub(R,1,ChatSettings.MaximumMessageLength);(ChatBar:GetTextBox()).Text=""if R~=""then moduleApiTable.MessagePosted:fire(R)if not CommandProcessor:ProcessCompletedChatMessage(R,ChatWindow)then if ChatSettings.DisallowedWhiteSpace then for w=1,#ChatSettings.DisallowedWhiteSpace,1 do if ChatSettings.DisallowedWhiteSpace[w]=="\t"then R=string.gsub(R,ChatSettings.DisallowedWhiteSpace[w]," ")else R=string.gsub(R,ChatSettings.DisallowedWhiteSpace[w],"")end end end R=string.gsub(R,"\n","")R=string.gsub(R,"\091 \093\043"," ")local w=ChatWindow:GetTargetMessageChannel()if w then MessageSender:SendMessage(R,w)else MessageSender:SendMessage(R,nil)end end end end end local R={}function setupChatBarConnections()for w=1,#R,1 do R[w]:Disconnect()end R={}local w=(ChatBar:GetTextBox()).FocusLost:connect(chatBarFocusLost)table.insert(R,w)local e=(ChatBar:GetTextBox()).Focused:connect(chatBarFocused)table.insert(R,e)end setupChatBarConnections()ChatBar.GuiObjectsChanged:connect(setupChatBarConnections)EventFolder.OnMessageDoneFiltering.OnClientEvent:connect(function(R)if not ChatSettings.ShowUserOwnFilteredMessage then if R.FromSpeaker==LocalPlayer.Name then return end end local w=R.OriginalChannel local e=ChatWindow:GetChannel(w)if e then e:UpdateMessageFiltered(R)end if ChatSettings.GeneralChannelName and w~=ChatSettings.GeneralChannelName then local w=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if w then w:UpdateMessageFiltered(R)end end end)EventFolder.OnNewMessage.OnClientEvent:connect(function(R,w)local e=ChatWindow:GetChannel(w)if e then e:AddMessageToChannel(R)if R.FromSpeaker~=LocalPlayer.Name then ChannelsBar:UpdateMessagePostedInChannel(w)end local Q=nil if ChatSettings.GeneralChannelName and w~=ChatSettings.GeneralChannelName then Q=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if Q then Q:AddMessageToChannel(R)end end moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()end end)EventFolder.OnNewSystemMessage.OnClientEvent:connect(function(R,w)w=w or"\083\121\115\116\101\109"local e=ChatWindow:GetChannel(w)if e then e:AddMessageToChannel(R)ChannelsBar:UpdateMessagePostedInChannel(w)moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()if ChatSettings.GeneralChannelName and w~=ChatSettings.GeneralChannelName then local w=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if w then w:AddMessageToChannel(R)end end else warn(string.format("\074\117\115\116 \114\101\099\101\105\118\101\100 \115\121\115\116\101\109 \109\101\115\115\097\103\101 \102\111\114 \099\104\097\110\110\101\108 \073\'\109 \110\111\116 \105\110 \091\037\115\093",w))end end)function HandleChannelJoined(R,w,e,Q,S,D)if ChatWindow:GetChannel(R)then ChatWindow:RemoveChannel(R)end if R==ChatSettings.GeneralChannelName then DidFirstChannelsLoads=true end if Q then ChatBar:SetChannelNameColor(R,Q)end local g=ChatWindow:AddChannel(R)if g then if R=="\065\108\108"then DoSwitchCurrentChannel(R)end if e then local w=1 if#e>ChatSettings.MessageHistoryLengthPerChannel then w=#e-ChatSettings.MessageHistoryLengthPerChannel end for R=w,#e,1 do g:AddMessageToChannel(e[R])end if S then if ChatSettings.GeneralChannelName and R~=ChatSettings.GeneralChannelName then local R=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if R then R:AddMessagesToChannelByTimeStamp(e,w)end end end end if w~=""then local e={ID=-1,FromSpeaker=nil,SpeakerUserId=0,OriginalChannel=R;IsFiltered=true;MessageLength=string.len(w),Message=trimTrailingSpaces(w),MessageType=ChatConstants.MessageTypeWelcome,Time=os.time();ExtraData=nil}g:AddMessageToChannel(e)if D and not ChatSettings.ShowChannelsBar then if R~=ChatSettings.GeneralChannelName then local R=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if R then R:AddMessageToChannel(e)end end end end DoFadeInFromNewInformation()end end EventFolder.OnChannelJoined.OnClientEvent:connect(function(R,w,e,Q)HandleChannelJoined(R,w,e,Q,false,true)end)EventFolder.OnChannelLeft.OnClientEvent:connect(function(R)ChatWindow:RemoveChannel(R)DoFadeInFromNewInformation()end)EventFolder.OnMuted.OnClientEvent:connect(function(R) end)EventFolder.OnUnmuted.OnClientEvent:connect(function(R) end)EventFolder.OnMainChannelSet.OnClientEvent:connect(function(R)DoSwitchCurrentChannel(R)end);(coroutine.wrap(function()local R=DefaultChatSystemChatEvents:WaitForChild("\067\104\097\110\110\101\108\078\097\109\101\067\111\108\111\114\085\112\100\097\116\101\100",5)if R then R.OnClientEvent:connect(function(R,w)ChatBar:SetChannelNameColor(R,w)end)end end))()

function chatBarFocusLost(enterPressed, inputObject)
	DoBackgroundFadeIn()
	chatBarFocusChanged:Fire(false)

	if (enterPressed) then
		local message = ChatBar:GetTextBox().Text

		if ChatBar:IsInCustomState() then
			local customMessage = ChatBar:GetCustomMessage()
			if customMessage then
				message = customMessage
			end
			local messageSunk = ChatBar:CustomStateProcessCompletedMessage(message)
			ChatBar:ResetCustomState()
			if messageSunk then
				return
			end
		end

		message = string.sub(message, 1, ChatSettings.MaximumMessageLength)

		ChatBar:GetTextBox().Text = ""

		if message ~= "" then
			--// Sends signal to eventually call Player:Chat() to handle C++ side legacy stuff.
			moduleApiTable.MessagePosted:fire(message)

			if not CommandProcessor:ProcessCompletedChatMessage(message, ChatWindow) then
				if ChatSettings.DisallowedWhiteSpace then
					for i = 1, #ChatSettings.DisallowedWhiteSpace do
						if ChatSettings.DisallowedWhiteSpace[i] == "\t" then
							message = string.gsub(message, ChatSettings.DisallowedWhiteSpace[i], " ")
						else
							message = string.gsub(message, ChatSettings.DisallowedWhiteSpace[i], "")
						end
					end
				end
				message = string.gsub(message, "\n", "")
				message = string.gsub(message, "[ ]+", " ")

				local targetChannel = ChatWindow:GetTargetMessageChannel()
				if targetChannel then
					MessageSender:SendMessage(message, targetChannel)
				else
					MessageSender:SendMessage(message, nil)
				end
			end
		end

	end
end

local ChatBarConnections = {}
function setupChatBarConnections()
	for i = 1, #ChatBarConnections do
		ChatBarConnections[i]:Disconnect()
	end
	ChatBarConnections = {}

	local focusLostConnection = ChatBar:GetTextBox().FocusLost:connect(chatBarFocusLost)
	table.insert(ChatBarConnections, focusLostConnection)

	local focusGainedConnection = ChatBar:GetTextBox().Focused:connect(chatBarFocused)
	table.insert(ChatBarConnections, focusGainedConnection)
end

setupChatBarConnections()
ChatBar.GuiObjectsChanged:connect(setupChatBarConnections)

EventFolder.OnMessageDoneFiltering.OnClientEvent:connect(function(messageData)
	if not ChatSettings.ShowUserOwnFilteredMessage then
		if messageData.FromSpeaker == LocalPlayer.Name then
			return
		end
	end

	local channelName = messageData.OriginalChannel
	local channelObj = ChatWindow:GetChannel(channelName)
	if channelObj then
		channelObj:UpdateMessageFiltered(messageData)
	end

	if ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName then
		local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
		if generalChannel then
			generalChannel:UpdateMessageFiltered(messageData)
		end
	end
end)

EventFolder.OnNewMessage.OnClientEvent:connect(function(messageData, channelName)
	local channelObj = ChatWindow:GetChannel(channelName)
	if (channelObj) then
		channelObj:AddMessageToChannel(messageData)

		if (messageData.FromSpeaker ~= LocalPlayer.Name) then
			ChannelsBar:UpdateMessagePostedInChannel(channelName)
		end

		local generalChannel = nil
		if (ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName) then
			generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
			if (generalChannel) then
				generalChannel:AddMessageToChannel(messageData)
			end
		end

		moduleApiTable.MessageCount = moduleApiTable.MessageCount + 1
		moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)

		DoFadeInFromNewInformation()
	end
end)

EventFolder.OnNewSystemMessage.OnClientEvent:connect(function(messageData, channelName)
	channelName = channelName or "System"

	local channelObj = ChatWindow:GetChannel(channelName)
	if (channelObj) then
		channelObj:AddMessageToChannel(messageData)

		ChannelsBar:UpdateMessagePostedInChannel(channelName)

		moduleApiTable.MessageCount = moduleApiTable.MessageCount + 1
		moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)

		DoFadeInFromNewInformation()

		if (ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName) then
			local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
			if (generalChannel) then
				generalChannel:AddMessageToChannel(messageData)
			end
		end
	else
		warn(string.format("Just received system message for channel I'm not in [%s]", channelName))
	end
end)


function HandleChannelJoined(channel, welcomeMessage, messageLog, channelNameColor, addHistoryToGeneralChannel,
	addWelcomeMessageToGeneralChannel)
	if ChatWindow:GetChannel(channel) then
		--- If the channel has already been added, remove it first.
		ChatWindow:RemoveChannel(channel)
	end

	if (channel == ChatSettings.GeneralChannelName) then
		DidFirstChannelsLoads = true
	end

	if channelNameColor then
		ChatBar:SetChannelNameColor(channel, channelNameColor)
	end

	local channelObj = ChatWindow:AddChannel(channel)

	if (channelObj) then
		if (channel == "All") then
			DoSwitchCurrentChannel(channel)
		end

		if (messageLog) then
			local startIndex = 1
			if #messageLog > ChatSettings.MessageHistoryLengthPerChannel then
				startIndex = #messageLog - ChatSettings.MessageHistoryLengthPerChannel
			end

			for i = startIndex, #messageLog do
				channelObj:AddMessageToChannel(messageLog[i])
			end

			if addHistoryToGeneralChannel then
				if ChatSettings.GeneralChannelName and channel ~= ChatSettings.GeneralChannelName then
					local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
					if generalChannel then
						generalChannel:AddMessagesToChannelByTimeStamp(messageLog, startIndex)
					end
				end
			end
		end

		if (welcomeMessage ~= "") then
			local welcomeMessageObject = {
				ID = -1,
				FromSpeaker = nil,
				SpeakerUserId = 0,
				OriginalChannel = channel,
				IsFiltered = true,
				MessageLength = string.len(welcomeMessage),
				Message = trimTrailingSpaces(welcomeMessage),
				MessageType = ChatConstants.MessageTypeWelcome,
				Time = os.time(),
				ExtraData = nil,
			}
			channelObj:AddMessageToChannel(welcomeMessageObject)

			if addWelcomeMessageToGeneralChannel and not ChatSettings.ShowChannelsBar then
				if channel ~= ChatSettings.GeneralChannelName then
					local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
					if generalChannel then
						generalChannel:AddMessageToChannel(welcomeMessageObject)
					end
				end
			end
		end

		DoFadeInFromNewInformation()
	end

end

EventFolder.OnChannelJoined.OnClientEvent:connect(function(channel, welcomeMessage, messageLog, channelNameColor)
	HandleChannelJoined(channel, welcomeMessage, messageLog, channelNameColor, false, true)
end)

EventFolder.OnChannelLeft.OnClientEvent:connect(function(channel)
	ChatWindow:RemoveChannel(channel)

	DoFadeInFromNewInformation()
end)

EventFolder.OnMuted.OnClientEvent:connect(function(channel)
	--// Do something eventually maybe?
	--// This used to take away the chat bar in channels the player was muted in.
	--// We found out this behavior was inconvenient for doing chat commands though.
end)

EventFolder.OnUnmuted.OnClientEvent:connect(function(channel)
	--// Same as above.
end)

EventFolder.OnMainChannelSet.OnClientEvent:connect(function(channel)
	DoSwitchCurrentChannel(channel)
end)

coroutine.wrap(function()
	-- ChannelNameColorUpdated may not exist if the client version is older than the server version.
	local ChannelNameColorUpdated = DefaultChatSystemChatEvents:WaitForChild("ChannelNameColorUpdated", 5)
	if ChannelNameColorUpdated then
		ChannelNameColorUpdated.OnClientEvent:connect(function(channelName, channelNameColor)
			ChatBar:SetChannelNameColor(channelName, channelNameColor)
		end)
	end
end)()

