local m={}m.__index=m function m.Destroy(m)m.Destroyed=true end function m.SetActive(C,m)if m==C.Active then return end if m==false then C.MessageLogDisplay:Clear()else C.MessageLogDisplay:SetCurrentChannelName(C.Name)for m=1,#C.MessageLog,1 do C.MessageLogDisplay:AddMessage(C.MessageLog[m])end end C.Active=m end function m.UpdateMessageFiltered(C,m)local r=1 local v=C.MessageLog local Z=nil while#v>=r do local C=v[r]if C.ID==m.ID then Z=C break end r=r+1 end if Z then Z.Message=m.Message Z.IsFiltered=true if C.Active then C.MessageLogDisplay:UpdateMessageFiltered(Z)end else C:AddMessageToChannelByTimeStamp(m)end end function m.AddMessageToChannel(C,m)table.insert(C.MessageLog,m)if C.Active then C.MessageLogDisplay:AddMessage(m)end if#C.MessageLog>ChatSettings.MessageHistoryLengthPerChannel then C:RemoveLastMessageFromChannel()end end function m.InternalAddMessageAtTimeStamp(C,m)for r=1,#C.MessageLog,1 do if m.Time<C.MessageLog[r].Time then table.insert(C.MessageLog,r,m)return end end table.insert(C.MessageLog,m)end function m.AddMessagesToChannelByTimeStamp(r,m,C)for C=C,#m,1 do r:InternalAddMessageAtTimeStamp(m[C])end while#r.MessageLog>ChatSettings.MessageHistoryLengthPerChannel do table.remove(r.MessageLog,1)end if r.Active then r.MessageLogDisplay:Clear()for m=1,#r.MessageLog,1 do r.MessageLogDisplay:AddMessage(r.MessageLog[m])end end end function m.AddMessageToChannelByTimeStamp(C,m)if#C.MessageLog>=1 then if C.MessageLog[1].Time>m.Time then return elseif m.Time>=C.MessageLog[#C.MessageLog].Time then C:AddMessageToChannel(m)return end for r=1,#C.MessageLog,1 do if m.Time<C.MessageLog[r].Time then table.insert(C.MessageLog,r,m)if#C.MessageLog>ChatSettings.MessageHistoryLengthPerChannel then C:RemoveLastMessageFromChannel()end if C.Active then C.MessageLogDisplay:AddMessageAtIndex(m,r)end return end end else C:AddMessageToChannel(m)end end function m.RemoveLastMessageFromChannel(m)table.remove(m.MessageLog,1)if m.Active then m.MessageLogDisplay:RemoveLastMessage()end end function m.ClearMessageLog(m)m.MessageLog={}if m.Active then m.MessageLogDisplay:Clear()end end function m.RegisterChannelTab(C,m)C.ChannelTab=m end

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
