local b={}b.__index=b function b.Destroy(b)b.Destroyed=true end function b.SetActive(y,b)if b==y.Active then return end if b==false then y.MessageLogDisplay:Clear()else y.MessageLogDisplay:SetCurrentChannelName(y.Name)for b=1,#y.MessageLog,1 do y.MessageLogDisplay:AddMessage(y.MessageLog[b])end end y.Active=b end function b.UpdateMessageFiltered(y,b)local V=1 local S=y.MessageLog local X=nil while#S>=V do local y=S[V]if y.ID==b.ID then X=y break end V=V+1 end if X then X.Message=b.Message X.IsFiltered=true if y.Active then if UserFlagRemoveMessageFromMessageLog then if X.Message==""then table.remove(y.MessageLog,V)end end y.MessageLogDisplay:UpdateMessageFiltered(X)end else y:AddMessageToChannelByTimeStamp(b)end end function b.AddMessageToChannel(y,b)table.insert(y.MessageLog,b)if y.Active then y.MessageLogDisplay:AddMessage(b)end if#y.MessageLog>ChatSettings.MessageHistoryLengthPerChannel then y:RemoveLastMessageFromChannel()end end function b.InternalAddMessageAtTimeStamp(y,b)for V=1,#y.MessageLog,1 do if b.Time<y.MessageLog[V].Time then table.insert(y.MessageLog,V,b)return end end table.insert(y.MessageLog,b)end function b.AddMessagesToChannelByTimeStamp(V,b,y)for y=y,#b,1 do V:InternalAddMessageAtTimeStamp(b[y])end while#V.MessageLog>ChatSettings.MessageHistoryLengthPerChannel do table.remove(V.MessageLog,1)end if V.Active then V.MessageLogDisplay:Clear()for b=1,#V.MessageLog,1 do V.MessageLogDisplay:AddMessage(V.MessageLog[b])end end end function b.AddMessageToChannelByTimeStamp(y,b)if#y.MessageLog>=1 then if y.MessageLog[1].Time>b.Time then return elseif b.Time>=y.MessageLog[#y.MessageLog].Time then y:AddMessageToChannel(b)return end for V=1,#y.MessageLog,1 do if b.Time<y.MessageLog[V].Time then table.insert(y.MessageLog,V,b)if#y.MessageLog>ChatSettings.MessageHistoryLengthPerChannel then y:RemoveLastMessageFromChannel()end if y.Active then y.MessageLogDisplay:AddMessageAtIndex(b,V)end return end end else y:AddMessageToChannel(b)end end function b.RemoveLastMessageFromChannel(b)table.remove(b.MessageLog,1)if b.Active then b.MessageLogDisplay:RemoveLastMessage()end end function b.ClearMessageLog(b)b.MessageLog={}if b.Active then b.MessageLogDisplay:Clear()end end function b.RegisterChannelTab(y,b)y.ChannelTab=b end
--////////////////////////////// Methods
--//////////////////////////////////////
local methods = {}
methods.__index = methods

function methods:Destroy()
	self.Destroyed = true
end

function methods:SetActive(active)
	if active == self.Active then
		return
	end
	if active == false then
		self.MessageLogDisplay:Clear()
	else
		self.MessageLogDisplay:SetCurrentChannelName(self.Name)
		for i = 1, #self.MessageLog do
			self.MessageLogDisplay:AddMessage(self.MessageLog[i])
		end
	end
	self.Active = active
end

function methods:UpdateMessageFiltered(messageData)
	local searchIndex = 1
	local searchTable = self.MessageLog
	local messageObj = nil
	while #searchTable >= searchIndex do
		local obj = searchTable[searchIndex]

		if obj.ID == messageData.ID then
			messageObj = obj
			break
		end

		searchIndex = searchIndex + 1
	end

	if messageObj then
		messageObj.Message = messageData.Message
		messageObj.IsFiltered = true
		if self.Active then
			if UserFlagRemoveMessageFromMessageLog then
				if messageObj.Message == "" then
					table.remove(self.MessageLog, searchIndex)
				end
			end
			self.MessageLogDisplay:UpdateMessageFiltered(messageObj)
		end
	else
		-- We have not seen this filtered message before, but we should still add it to our log.
		self:AddMessageToChannelByTimeStamp(messageData)
	end
end

function methods:AddMessageToChannel(messageData)
	table.insert(self.MessageLog, messageData)
	if self.Active then
		self.MessageLogDisplay:AddMessage(messageData)
	end
	if #self.MessageLog > ChatSettings.MessageHistoryLengthPerChannel then
		self:RemoveLastMessageFromChannel()
	end
end

function methods:InternalAddMessageAtTimeStamp(messageData)
	for i = 1, #self.MessageLog do
		if messageData.Time < self.MessageLog[i].Time then
			table.insert(self.MessageLog, i, messageData)
			return
		end
	end
	table.insert(self.MessageLog, messageData)
end

function methods:AddMessagesToChannelByTimeStamp(messageLog, startIndex)
	for i = startIndex, #messageLog do
		self:InternalAddMessageAtTimeStamp(messageLog[i])
	end
	while #self.MessageLog > ChatSettings.MessageHistoryLengthPerChannel do
		table.remove(self.MessageLog, 1)
	end
	if self.Active then
		self.MessageLogDisplay:Clear()
		for i = 1, #self.MessageLog do
			self.MessageLogDisplay:AddMessage(self.MessageLog[i])
		end
	end
end

function methods:AddMessageToChannelByTimeStamp(messageData)
	if #self.MessageLog >= 1 then
		-- These are the fast cases to evalutate.
		if self.MessageLog[1].Time > messageData.Time then
			return
		elseif messageData.Time >= self.MessageLog[#self.MessageLog].Time then
			self:AddMessageToChannel(messageData)
			return
		end

		for i = 1, #self.MessageLog do
			if messageData.Time < self.MessageLog[i].Time then
				table.insert(self.MessageLog, i, messageData)

				if #self.MessageLog > ChatSettings.MessageHistoryLengthPerChannel then
					self:RemoveLastMessageFromChannel()
				end

				if self.Active then
					self.MessageLogDisplay:AddMessageAtIndex(messageData, i)
				end

				return
			end
		end
	else
		self:AddMessageToChannel(messageData)
	end
end

function methods:RemoveLastMessageFromChannel()
	table.remove(self.MessageLog, 1)

	if self.Active then
		self.MessageLogDisplay:RemoveLastMessage()
	end
end

function methods:ClearMessageLog()
	self.MessageLog = {}

	if self.Active then
		self.MessageLogDisplay:Clear()
	end
end

function methods:RegisterChannelTab(tab)
	self.ChannelTab = tab
end
