local p={}p.__index=p function ReturnToObjectPoolRecursive(p,J)local v=p:GetChildren()for p=1,#v,1 do ReturnToObjectPoolRecursive(v[p],J)end p.Parent=nil J:ReturnInstance(p)end function GetMessageCreators()local p={}local J=messageCreatorModules:GetChildren()for v=1,#J,1 do if J[v]:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")then if J[v].Name~="\085\116\105\108"then local A=require(J[v])p[A[messageCreatorUtil.KEY_MESSAGE_TYPE]]=A[messageCreatorUtil.KEY_CREATOR_FUNCTION]end end end return p end function p.WrapIntoMessageObject(v,p,J)local A=J[messageCreatorUtil.KEY_BASE_FRAME]local d=nil if messageCreatorUtil.KEY_BASE_MESSAGE then d=J[messageCreatorUtil.KEY_BASE_MESSAGE]end local g=J[messageCreatorUtil.KEY_UPDATE_TEXT_FUNC]local X=J[messageCreatorUtil.KEY_GET_HEIGHT]local M=J[messageCreatorUtil.KEY_FADE_IN]local t=J[messageCreatorUtil.KEY_FADE_OUT]local D=J[messageCreatorUtil.KEY_UPDATE_ANIMATION]local V={}V.ID=p.ID V.BaseFrame=A V.BaseMessage=d V.UpdateTextFunction=g or function()warn("\078\079 \077\069\083\083\065\071\069 \082\069\083\073\090\069 \070\085\078\067\084\073\079\078")end V.GetHeightFunction=X V.FadeInFunction=M V.FadeOutFunction=t V.UpdateAnimFunction=D V.ObjectPool=v.ObjectPool V.Destroyed=false function V.Destroy(p)ReturnToObjectPoolRecursive(p.BaseFrame,p.ObjectPool)p.Destroyed=true end return V end function p.CreateMessageLabel(v,p,J)local A=p.MessageType if v.MessageCreators[A]then local d=v.MessageCreators[A](p,J)if d then return v:WrapIntoMessageObject(p,d)end elseif v.DefaultCreatorType then local A=v.MessageCreators[v.DefaultCreatorType](p,J)if A then return v:WrapIntoMessageObject(p,A)end else error("\078\111 \109\101\115\115\097\103\101 \099\114\101\097\116\111\114 \097\118\097\105\108\097\098\108\101 \102\111\114 \109\101\115\115\097\103\101 \116\121\112\101\058 "..A)end end

local methods = {}
methods.__index = methods

function ReturnToObjectPoolRecursive(instance, objectPool)
	local children = instance:GetChildren()
	for i = 1, #children do
		ReturnToObjectPoolRecursive(children[i], objectPool)
	end
	instance.Parent = nil
	objectPool:ReturnInstance(instance)
end

function GetMessageCreators()
	local typeToFunction = {}
	local creators = messageCreatorModules:GetChildren()
	for i = 1, #creators do
		if creators[i]:IsA("ModuleScript") then
			if creators[i].Name ~= "Util" then
				local creator = require(creators[i])
				typeToFunction[creator[messageCreatorUtil.KEY_MESSAGE_TYPE]] = creator[messageCreatorUtil.KEY_CREATOR_FUNCTION]
			end
		end
	end
	return typeToFunction
end

function methods:WrapIntoMessageObject(messageData, createdMessageObject)
	local BaseFrame = createdMessageObject[messageCreatorUtil.KEY_BASE_FRAME]
	local BaseMessage = nil
	if messageCreatorUtil.KEY_BASE_MESSAGE then
		BaseMessage = createdMessageObject[messageCreatorUtil.KEY_BASE_MESSAGE]
	end
	local UpdateTextFunction = createdMessageObject[messageCreatorUtil.KEY_UPDATE_TEXT_FUNC]
	local GetHeightFunction = createdMessageObject[messageCreatorUtil.KEY_GET_HEIGHT]
	local FadeInFunction = createdMessageObject[messageCreatorUtil.KEY_FADE_IN]
	local FadeOutFunction = createdMessageObject[messageCreatorUtil.KEY_FADE_OUT]
	local UpdateAnimFunction = createdMessageObject[messageCreatorUtil.KEY_UPDATE_ANIMATION]

	local obj = {}

	obj.ID = messageData.ID
	obj.BaseFrame = BaseFrame
	obj.BaseMessage = BaseMessage
	obj.UpdateTextFunction = UpdateTextFunction or function() warn("NO MESSAGE RESIZE FUNCTION") end
	obj.GetHeightFunction = GetHeightFunction
	obj.FadeInFunction = FadeInFunction
	obj.FadeOutFunction = FadeOutFunction
	obj.UpdateAnimFunction = UpdateAnimFunction
	obj.ObjectPool = self.ObjectPool
	obj.Destroyed = false

	function obj:Destroy()
		ReturnToObjectPoolRecursive(self.BaseFrame, self.ObjectPool)
		self.Destroyed = true
	end

	return obj
end

function methods:CreateMessageLabel(messageData, currentChannelName)
	local messageType = messageData.MessageType
	if self.MessageCreators[messageType] then
		local createdMessageObject = self.MessageCreators[messageType](messageData, currentChannelName)
		if createdMessageObject then
			return self:WrapIntoMessageObject(messageData, createdMessageObject)
		end
	elseif self.DefaultCreatorType then
		local createdMessageObject = self.MessageCreators[self.DefaultCreatorType](messageData, currentChannelName)
		if createdMessageObject then
			return self:WrapIntoMessageObject(messageData, createdMessageObject)
		end
	else
		error("No message creator available for message type: " ..messageType)
	end
end
