local O={}O.__index=O function ReturnToObjectPoolRecursive(O,V)local b=O:GetChildren()for O=1,#b,1 do ReturnToObjectPoolRecursive(b[O],V)end O.Parent=nil V:ReturnInstance(O)end function GetMessageCreators()local O={}local V=messageCreatorModules:GetChildren()for b=1,#V,1 do if V[b]:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")then if V[b].Name~="\085\116\105\108"then local i=require(V[b])O[i[messageCreatorUtil.KEY_MESSAGE_TYPE]]=i[messageCreatorUtil.KEY_CREATOR_FUNCTION]end end end return O end function O.WrapIntoMessageObject(b,O,V)local i=V[messageCreatorUtil.KEY_BASE_FRAME]local I=nil if messageCreatorUtil.KEY_BASE_MESSAGE then I=V[messageCreatorUtil.KEY_BASE_MESSAGE]end local u=V[messageCreatorUtil.KEY_UPDATE_TEXT_FUNC]local r=V[messageCreatorUtil.KEY_GET_HEIGHT]local T=V[messageCreatorUtil.KEY_FADE_IN]local n=V[messageCreatorUtil.KEY_FADE_OUT]local o=V[messageCreatorUtil.KEY_UPDATE_ANIMATION]local M={}M.ID=O.ID M.BaseFrame=i M.BaseMessage=I M.UpdateTextFunction=u 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 M.GetHeightFunction=r M.FadeInFunction=T M.FadeOutFunction=n M.UpdateAnimFunction=o M.ObjectPool=b.ObjectPool M.Destroyed=false function M.Destroy(O)ReturnToObjectPoolRecursive(O.BaseFrame,O.ObjectPool)O.Destroyed=true end return M end function O.CreateMessageLabel(b,O,V)local i=O.MessageType if b.MessageCreators[i]then local I=b.MessageCreators[i](O,V)if I then return b:WrapIntoMessageObject(O,I)end elseif b.DefaultCreatorType then local i=b.MessageCreators[b.DefaultCreatorType](O,V)if i then return b:WrapIntoMessageObject(O,i)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 "..i)end end
--////////////////////////////// Methods
--//////////////////////////////////////
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
