local R={}R.__index=R function R.Destroy(R)R.Destroyed=true end function R.SetActive(l,R)if R==l.Active then return end if R==false then l.MessageLogDisplay:Clear()else l.MessageLogDisplay:SetCurrentChannelName(l.Name)for R=1,#l.MessageLog,1 do l.MessageLogDisplay:AddMessage(l.MessageLog[R])end end l.Active=R end function R.UpdateMessageFiltered(l,R)local a=1 local M=l.MessageLog local U=nil while#M>=a do local l=M[a]if l.ID==R.ID then U=l break end a=a+1 end if U then U.Message=R.Message U.IsFiltered=true if l.Active then l.MessageLogDisplay:UpdateMessageFiltered(U)end else l:AddMessageToChannelByTimeStamp(R)end end function R.AddMessageToChannel(l,R)table.insert(l.MessageLog,R)if l.Active then l.MessageLogDisplay:AddMessage(R)end if#l.MessageLog>ChatSettings.MessageHistoryLengthPerChannel then l:RemoveLastMessageFromChannel()end end function R.InternalAddMessageAtTimeStamp(l,R)for a=1,#l.MessageLog,1 do if R.Time<l.MessageLog[a].Time then table.insert(l.MessageLog,a,R)return end end table.insert(l.MessageLog,R)end function R.AddMessagesToChannelByTimeStamp(a,R,l)for l=l,#R,1 do a:InternalAddMessageAtTimeStamp(R[l])end while#a.MessageLog>ChatSettings.MessageHistoryLengthPerChannel do table.remove(a.MessageLog,1)end if a.Active then a.MessageLogDisplay:Clear()for R=1,#a.MessageLog,1 do a.MessageLogDisplay:AddMessage(a.MessageLog[R])end end end function R.AddMessageToChannelByTimeStamp(l,R)if#l.MessageLog>=1 then if l.MessageLog[1].Time>R.Time then return elseif R.Time>=l.MessageLog[#l.MessageLog].Time then l:AddMessageToChannel(R)return end for a=1,#l.MessageLog,1 do if R.Time<l.MessageLog[a].Time then table.insert(l.MessageLog,a,R)if#l.MessageLog>ChatSettings.MessageHistoryLengthPerChannel then l:RemoveLastMessageFromChannel()end if l.Active then l.MessageLogDisplay:AddMessageAtIndex(R,a)end return end end else l:AddMessageToChannel(R)end end function R.RemoveLastMessageFromChannel(R)table.remove(R.MessageLog,1)if R.Active then R.MessageLogDisplay:RemoveLastMessage()end end function R.ClearMessageLog(R)R.MessageLog={}if R.Active then R.MessageLogDisplay:Clear()end end function R.RegisterChannelTab(l,R)l.ChannelTab=R 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
			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
