local h=script.Parent.Parent local i=require(h:WaitForChild("\067\104\097\116\083\101\116\116\105\110\103\115"))local a=require(h:WaitForChild("\067\104\097\116\067\111\110\115\116\097\110\116\115"))local M=require(script.Parent:WaitForChild("\085\116\105\108"))function CreateMessageLabel(h,a)local r=h.FromSpeaker local H=h.Message local v=h.ExtraData or{}local w=v.Font or i.DefaultFont local A=v.TextSize or i.ChatWindowTextSize local n=v.NameColor or i.DefaultNameColor local J=v.ChatColor or i.DefaultMessageColor local o=v.ChannelColor or J local t=v.Tags or{}local B=v.Icons or{}local L=string.format("\091\037\115\093\058",r)local j=M:GetStringTextBounds(L,w,A)local W=M:GetNumberOfSpaces(L,w,A)+1 local l,s=M:CreateBaseMessage("",w,A,J)local c=M:AddNameButtonToBaseMessage(s,n,L,r)local K=nil local I=UDim2.new(0,0,0,0)if a~=h.OriginalChannel then local i=string.format("\123\037\115\125",h.OriginalChannel)K=M:AddChannelButtonToBaseMessage(s,o,i,h.OriginalChannel)I=UDim2.new(0,K.Size.X.Offset+(M:GetStringTextBounds(" ",w,A)).X,0,0)W=(W+M:GetNumberOfSpaces(i,w,A))+1 end local m={}for h,i in pairs(t)do local a=i.TagColor or Color3.fromRGB(255,0,255)local r=i.TagText or"\063\063\063"local H=string.format("\091\037\115\093 ",r)local v=M:AddTagLabelToBaseMessage(s,a,H)v.Position=I W=W+M:GetNumberOfSpaces(H,w,A)I=I+UDim2.new(0,v.Size.X.Offset,0,0)table.insert(m,v)end local Z={}for h,i in pairs(B)do local a=M:AddIconLabelToBaseMessage(s,i)a.Position=I W=W+M:GetNumberOfSpaces(a,w,A)I=I+UDim2.new(0,a.Size.X.Offset,0,0)table.insert(Z,a)end c.Position=I local function S(i)if h.IsFiltered then s.Text=string.rep(" ",W)..i.Message else s.Text=string.rep(" ",W)..string.rep("\095",i.MessageLength)end end S(h)local function F(h)return M:GetMessageHeight(s,l,h)end local P={}P[c]={TextTransparency={FadedIn=0,FadedOut=1};TextStrokeTransparency={FadedIn=.75;FadedOut=1}}P[s]={TextTransparency={FadedIn=0;FadedOut=1},TextStrokeTransparency={FadedIn=.75,FadedOut=1}}for h,i in pairs(m)do local a=string.format("\084\097\103\037\100",h)P[i]={TextTransparency={FadedIn=0;FadedOut=1};TextStrokeTransparency={FadedIn=.75,FadedOut=1}}end for h,i in pairs(Z)do P[i]={ImageTransparency={FadedIn=0,FadedOut=1}}end if K then P[K]={TextTransparency={FadedIn=0;FadedOut=1};TextStrokeTransparency={FadedIn=.75,FadedOut=1}}end local X,V,p=M:CreateFadeFunctions(P)return{[M.KEY_BASE_FRAME]=l,[M.KEY_BASE_MESSAGE]=s;[M.KEY_UPDATE_TEXT_FUNC]=S;[M.KEY_GET_HEIGHT]=F,[M.KEY_FADE_IN]=X;[M.KEY_FADE_OUT]=V,[M.KEY_UPDATE_ANIMATION]=p}end return{[M.KEY_MESSAGE_TYPE]=a.MessageTypeDefault,[M.KEY_CREATOR_FUNCTION]=CreateMessageLabel}


local clientChatModules = script.Parent.Parent
local ChatSettings = require(clientChatModules:WaitForChild("ChatSettings"))
local ChatConstants = require(clientChatModules:WaitForChild("ChatConstants"))
local util = require(script.Parent:WaitForChild("Util"))

function CreateMessageLabel(messageData, channelName)

	local fromSpeaker = messageData.FromSpeaker
	local message = messageData.Message

	local extraData = messageData.ExtraData or {}
	local useFont = extraData.Font or ChatSettings.DefaultFont
	local useTextSize = extraData.TextSize or ChatSettings.ChatWindowTextSize
	local useNameColor = extraData.NameColor or ChatSettings.DefaultNameColor
	local useChatColor = extraData.ChatColor or ChatSettings.DefaultMessageColor
	local useChannelColor = extraData.ChannelColor or useChatColor
	local tags = extraData.Tags or {}
	local icons = extraData.Icons or {}

	local formatUseName = string.format("[%s]:", fromSpeaker)
	local speakerNameSize = util:GetStringTextBounds(formatUseName, useFont, useTextSize)
	local numNeededSpaces = util:GetNumberOfSpaces(formatUseName, useFont, useTextSize) + 1

	local BaseFrame, BaseMessage = util:CreateBaseMessage("", useFont, useTextSize, useChatColor)
	local NameButton = util:AddNameButtonToBaseMessage(BaseMessage, useNameColor, formatUseName, fromSpeaker)
	local ChannelButton = nil

	local guiObjectSpacing = UDim2.new(0, 0, 0, 0)

	if channelName ~= messageData.OriginalChannel then
			local formatChannelName = string.format("{%s}", messageData.OriginalChannel)
			ChannelButton = util:AddChannelButtonToBaseMessage(BaseMessage, useChannelColor, formatChannelName, messageData.OriginalChannel)
			guiObjectSpacing = UDim2.new(0, ChannelButton.Size.X.Offset + util:GetStringTextBounds(" ", useFont, useTextSize).X, 0, 0)
			numNeededSpaces = numNeededSpaces + util:GetNumberOfSpaces(formatChannelName, useFont, useTextSize) + 1
	end

	local tagLabels = {}
	for i, tag in pairs(tags) do
		local tagColor = tag.TagColor or Color3.fromRGB(255, 0, 255)
		local tagText = tag.TagText or "???"
		local formatTagText = string.format("[%s] ", tagText)
		local label = util:AddTagLabelToBaseMessage(BaseMessage, tagColor, formatTagText)
		label.Position = guiObjectSpacing

		numNeededSpaces = numNeededSpaces + util:GetNumberOfSpaces(formatTagText, useFont, useTextSize)
		guiObjectSpacing = guiObjectSpacing + UDim2.new(0, label.Size.X.Offset, 0, 0)

		table.insert(tagLabels, label)
	end
	
	local iconLabels={}
	for i,icon in pairs(icons) do
		local label=util:AddIconLabelToBaseMessage(BaseMessage,icon)
		label.Position=guiObjectSpacing
		
		numNeededSpaces=numNeededSpaces+util:GetNumberOfSpaces(label,useFont,useTextSize)
		guiObjectSpacing=guiObjectSpacing+UDim2.new(0,label.Size.X.Offset,0,0)
		
		table.insert(iconLabels,label)
	end

	NameButton.Position = guiObjectSpacing

	local function UpdateTextFunction(messageObject)
		if messageData.IsFiltered then
			BaseMessage.Text = string.rep(" ", numNeededSpaces) .. messageObject.Message
		else
			BaseMessage.Text = string.rep(" ", numNeededSpaces) .. string.rep("_", messageObject.MessageLength)
		end
	end

	UpdateTextFunction(messageData)

	local function GetHeightFunction(xSize)
		return util:GetMessageHeight(BaseMessage, BaseFrame, xSize)
	end

	local FadeParmaters = {}
	FadeParmaters[NameButton] = {
		TextTransparency = {FadedIn = 0, FadedOut = 1},
		TextStrokeTransparency = {FadedIn = 0.75, FadedOut = 1}
	}

	FadeParmaters[BaseMessage] = {
		TextTransparency = {FadedIn = 0, FadedOut = 1},
		TextStrokeTransparency = {FadedIn = 0.75, FadedOut = 1}
	}

	for i, tagLabel in pairs(tagLabels) do
		local index = string.format("Tag%d", i)
		FadeParmaters[tagLabel] = {
			TextTransparency = {FadedIn = 0, FadedOut = 1},
			TextStrokeTransparency = {FadedIn = 0.75, FadedOut = 1}
		}
	end
	
	for i,icon in pairs(iconLabels) do
		FadeParmaters[icon] ={
			ImageTransparency={FadedIn=0,FadedOut=1}
		}
	end

	if ChannelButton then
		FadeParmaters[ChannelButton] = {
			TextTransparency = {FadedIn = 0, FadedOut = 1},
			TextStrokeTransparency = {FadedIn = 0.75, FadedOut = 1}
		}
	end

	local FadeInFunction, FadeOutFunction, UpdateAnimFunction = util:CreateFadeFunctions(FadeParmaters)

	return {
		[util.KEY_BASE_FRAME] = BaseFrame,
		[util.KEY_BASE_MESSAGE] = BaseMessage,
		[util.KEY_UPDATE_TEXT_FUNC] = UpdateTextFunction,
		[util.KEY_GET_HEIGHT] = GetHeightFunction,
		[util.KEY_FADE_IN] = FadeInFunction,
		[util.KEY_FADE_OUT] = FadeOutFunction,
		[util.KEY_UPDATE_ANIMATION] = UpdateAnimFunction
	}
end

return {
	[util.KEY_MESSAGE_TYPE] = ChatConstants.MessageTypeDefault,
	[util.KEY_CREATOR_FUNCTION] = CreateMessageLabel
}
