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