local H={}H.__index=H function H.SayMessage(Q,H,G,n)if Q.ChatService:InternalDoProcessCommands(Q.Name,H,G)then return end if not G then return end local O=Q.Channels[G:lower()]if not O then error("\083\112\101\097\107\101\114 \105\115 \110\111\116 \105\110 \099\104\097\110\110\101\108 \""..(G.."\""))end local Y=O:InternalPostMessage(Q,H,n)if Y then local H,n=pcall(function()Q.eSaidMessage:Fire(Y,G)end)if not H and n then print("\069\114\114\111\114 \115\097\121\105\110\103 \109\101\115\115\097\103\101\058 "..n)end end return Y end function H.JoinChannel(G,H)if G.Channels[H:lower()]then warn("\083\112\101\097\107\101\114 \105\115 \097\108\114\101\097\100\121 \105\110 \099\104\097\110\110\101\108 \""..(H.."\""))return end local n=G.ChatService:GetChannel(H)if not n then error("\067\104\097\110\110\101\108 \""..(H.."\" \100\111\101\115 \110\111\116 \101\120\105\115\116\033"))end G.Channels[H:lower()]=n n:InternalAddSpeaker(G)local Q,O=pcall(function()G.eChannelJoined:Fire(n.Name,n:GetWelcomeMessageForSpeaker(G))end)if not Q and O then print("\069\114\114\111\114 \106\111\105\110\105\110\103 \099\104\097\110\110\101\108\058 "..O)end end function H.LeaveChannel(G,H)if not G.Channels[H:lower()]then warn("\083\112\101\097\107\101\114 \105\115 \110\111\116 \105\110 \099\104\097\110\110\101\108 \""..(H.."\""))return end local n=G.Channels[H:lower()]G.Channels[H:lower()]=nil n:InternalRemoveSpeaker(G)local Q,O=pcall(function()G.eChannelLeft:Fire(n.Name)end)if not Q and O then print("\069\114\114\111\114 \108\101\097\118\105\110\103 \099\104\097\110\110\101\108\058 "..O)end end function H.IsInChannel(G,H)return G.Channels[H:lower()]~=nil end function H.GetChannelList(H)local G={}for H,n in pairs(H.Channels)do table.insert(G,n.Name)end return G end function H.SendMessage(O,H,G,n,Q)local Y=O.Channels[G:lower()]if Y then Y:SendMessageToSpeaker(H,O.Name,n,Q)else warn(string.format("\083\112\101\097\107\101\114 \'\037\115\' \105\115 \110\111\116 \105\110 \099\104\097\110\110\101\108 \'\037\115\' \097\110\100 \099\097\110\110\111\116 \114\101\099\101\105\118\101 \097 \109\101\115\115\097\103\101 \105\110 \105\116\046",O.Name,G))end end function H.SendSystemMessage(Q,H,G,n)local O=Q.Channels[G:lower()]if O then O:SendSystemMessageToSpeaker(H,Q.Name,n)else warn(string.format("\083\112\101\097\107\101\114 \'\037\115\' \105\115 \110\111\116 \105\110 \099\104\097\110\110\101\108 \'\037\115\' \097\110\100 \099\097\110\110\111\116 \114\101\099\101\105\118\101 \097 \115\121\115\116\101\109 \109\101\115\115\097\103\101 \105\110 \105\116\046",Q.Name,G))end end function H.GetPlayer(H)return H.PlayerObj end function H.SetExtraData(n,H,G)n.ExtraData[H]=G n.eExtraDataUpdated:Fire(H,G)end function H.GetExtraData(G,H)return G.ExtraData[H]end function H.SetMainChannel(G,H)local n,Q=pcall(function()G.eMainChannelSet:Fire(H)end)if not n and Q then print("\069\114\114\111\114 \115\101\116\116\105\110\103 \109\097\105\110 \099\104\097\110\110\101\108\058 "..Q)end end
--////////////////////////////// Methods
--//////////////////////////////////////
local methods = {}
methods.__index = methods

function methods:SayMessage(message, channelName, extraData)
	if self.ChatService:InternalDoProcessCommands(self.Name, message, channelName) then
		return
	end
	if not channelName then
		return
	end

	local channel = self.Channels[channelName:lower()]
	if not channel then
		error('Speaker is not in channel "' .. channelName .. '"')
	end

	local messageObj = channel:InternalPostMessage(self, message, extraData)
	if messageObj then
		local success, err = pcall(function()
			self.eSaidMessage:Fire(messageObj, channelName)
		end)
		if not success and err then
			print("Error saying message: " .. err)
		end
	end

	return messageObj
end

function methods:JoinChannel(channelName)
	if self.Channels[channelName:lower()] then
		warn('Speaker is already in channel "' .. channelName .. '"')
		return
	end

	local channel = self.ChatService:GetChannel(channelName)
	if not channel then
		error('Channel "' .. channelName .. '" does not exist!')
	end

	self.Channels[channelName:lower()] = channel
	channel:InternalAddSpeaker(self)
	local success, err = pcall(function()
		self.eChannelJoined:Fire(channel.Name, channel:GetWelcomeMessageForSpeaker(self))
	end)
	if not success and err then
		print("Error joining channel: " .. err)
	end
end

function methods:LeaveChannel(channelName)
	if not self.Channels[channelName:lower()] then
		warn('Speaker is not in channel "' .. channelName .. '"')
		return
	end

	local channel = self.Channels[channelName:lower()]

	self.Channels[channelName:lower()] = nil
	channel:InternalRemoveSpeaker(self)
	local success, err = pcall(function()
		self.eChannelLeft:Fire(channel.Name)
	end)
	if not success and err then
		print("Error leaving channel: " .. err)
	end
end

function methods:IsInChannel(channelName)
	return (self.Channels[channelName:lower()] ~= nil)
end

function methods:GetChannelList()
	local list = {}
	for i, channel in pairs(self.Channels) do
		table.insert(list, channel.Name)
	end
	return list
end

function methods:SendMessage(message, channelName, fromSpeaker, extraData)
	local channel = self.Channels[channelName:lower()]
	if channel then
		channel:SendMessageToSpeaker(message, self.Name, fromSpeaker, extraData)
	else
		warn(
			string.format(
				"Speaker '%s' is not in channel '%s' and cannot receive a message in it.",
				self.Name,
				channelName
			)
		)
	end
end

function methods:SendSystemMessage(message, channelName, extraData)
	local channel = self.Channels[channelName:lower()]
	if channel then
		channel:SendSystemMessageToSpeaker(message, self.Name, extraData)
	else
		warn(
			string.format(
				"Speaker '%s' is not in channel '%s' and cannot receive a system message in it.",
				self.Name,
				channelName
			)
		)
	end
end

function methods:GetPlayer()
	return self.PlayerObj
end

function methods:SetExtraData(key, value)
	self.ExtraData[key] = value
	self.eExtraDataUpdated:Fire(key, value)
end

function methods:GetExtraData(key)
	return self.ExtraData[key]
end

function methods:SetMainChannel(channelName)
	local success, err = pcall(function()
		self.eMainChannelSet:Fire(channelName)
	end)
	if not success and err then
		print("Error setting main channel: " .. err)
	end
end
