local z={}z.__index=z function z.SayMessage(u,z,c,I)if u.ChatService:InternalDoProcessCommands(u.Name,z,c)then return end if not c then return end local F=u.Channels[c:lower()]if not F then error("\083\112\101\097\107\101\114 \105\115 \110\111\116 \105\110 \099\104\097\110\110\101\108 \""..(c.."\""))end local P=F:InternalPostMessage(u,z,I)if P then local z,I=pcall(function()u.eSaidMessage:Fire(P,c)end)if not z and I then print("\069\114\114\111\114 \115\097\121\105\110\103 \109\101\115\115\097\103\101\058 "..I)end end return P end function z.JoinChannel(c,z)if c.Channels[z: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 \""..(z.."\""))return end local I=c.ChatService:GetChannel(z)if not I then error("\067\104\097\110\110\101\108 \""..(z.."\" \100\111\101\115 \110\111\116 \101\120\105\115\116\033"))end c.Channels[z:lower()]=I I:InternalAddSpeaker(c)local u,F=pcall(function()c.eChannelJoined:Fire(I.Name,I.WelcomeMessage)end)if not u and F then print("\069\114\114\111\114 \106\111\105\110\105\110\103 \099\104\097\110\110\101\108\058 "..F)end end function z.LeaveChannel(c,z)if not c.Channels[z:lower()]then warn("\083\112\101\097\107\101\114 \105\115 \110\111\116 \105\110 \099\104\097\110\110\101\108 \""..(z.."\""))return end local I=c.Channels[z:lower()]c.Channels[z:lower()]=nil I:InternalRemoveSpeaker(c)local u,F=pcall(function()c.eChannelLeft:Fire(I.Name)end)if not u and F then print("\069\114\114\111\114 \108\101\097\118\105\110\103 \099\104\097\110\110\101\108\058 "..F)end end function z.IsInChannel(c,z)return c.Channels[z:lower()]~=nil end function z.GetChannelList(z)local c={}for z,I in pairs(z.Channels)do table.insert(c,I.Name)end return c end function z.SendMessage(F,z,c,I,u)local P=F.Channels[c:lower()]if P then P:SendMessageToSpeaker(z,F.Name,I,u)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",F.Name,c))end end function z.SendSystemMessage(u,z,c,I)local F=u.Channels[c:lower()]if F then F:SendSystemMessageToSpeaker(z,u.Name,I)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",u.Name,c))end end function z.GetPlayer(z)return z.PlayerObj end function z.SetExtraData(I,z,c)I.ExtraData[z]=c I.eExtraDataUpdated:Fire(z,c)end function z.GetExtraData(c,z)return c.ExtraData[z]end function z.SetMainChannel(c,z)local I,u=pcall(function()c.eMainChannelSet:Fire(z)end)if not I and u 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 "..u)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.WelcomeMessage)
	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
