local function L(L,C,t)return C+(t-C)*math.min(getMessageLength(L)/75.0,1.0)end local function C()local L={}L.data={}local C=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")L.Emptied=C.Event function L.Size(C)return#L.data end function L.Empty(C)return L:Size()<=0 end function L.PopFront(t)table.remove(L.data,1)if L:Empty()then C:Fire()end end function L.Front(C)return L.data[1]end function L.Get(t,C)return L.data[C]end function L.PushBack(t,C)table.insert(L.data,C)end function L.GetData(C)return L.data end return L end local function t()local L={}L.Fifo=C()L.BillboardGui=nil return L end local function l()local L={}L.data={}local C=0 function L.Size(L)return C end function L.Erase(l,t)if L.data[t]then C=C-1 end L.data[t]=nil end function L.Set(g,t,l)L.data[t]=l if l then C=C+1 end end function L.Get(l,C)if not C then return end if not L.data[C]then L.data[C]=t()local l=nil l=L.data[C].Fifo.Emptied:connect(function()l:disconnect()L:Erase(C)end)end return L.data[C]end function L.GetData(C)return L.data end return L end local function g(C,t,l)local g={}function g.ComputeBubbleLifetime(l,C,t)if t then return L(C,8,15)else return L(C,12,20)end end g.Origin=nil g.RenderBubble=nil g.Message=C g.BubbleDieDelay=g:ComputeBubbleLifetime(C,l)g.BubbleColor=t g.IsLocalPlayer=l return g end local function I(L,C,t)local l=g(C,BubbleColor.WHITE,t)if L then l.User=L.Name l.Origin=L.Character end return l end local function R(L,C,t,l)local I=g(C,l,t)I.Origin=L return I end function createChatBubbleMain(L,C)local t=Instance.new("\073\109\097\103\101\076\097\098\101\108")t.Name="\067\104\097\116\066\117\098\098\108\101"t.ScaleType=Enum.ScaleType.Slice t.SliceCenter=C t.Image="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047"..(tostring(L).."\046\112\110\103")t.BackgroundTransparency=1 t.BorderSizePixel=0 t.Size=UDim2.new(1.0,0,1.0,0)t.Position=UDim2.new(0,0,0,0)return t end function createChatBubbleTail(L,C)local t=Instance.new("\073\109\097\103\101\076\097\098\101\108")t.Name="\067\104\097\116\066\117\098\098\108\101\084\097\105\108"t.Image="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\117\105\047\100\105\097\108\111\103\095\116\097\105\108\046\112\110\103"t.BackgroundTransparency=1 t.BorderSizePixel=0 t.Position=L t.Size=C return t end function createChatBubbleWithTail(L,C,t,l)local g=createChatBubbleMain(L,l)local I=createChatBubbleTail(C,t)I.Parent=g return g end function createScaledChatBubbleWithTail(L,C,t,l)local g=createChatBubbleMain(L,l)local I=Instance.new("\070\114\097\109\101")I.Name="\067\104\097\116\066\117\098\098\108\101\084\097\105\108\070\114\097\109\101"I.BackgroundTransparency=1 I.SizeConstraint=Enum.SizeConstraint.RelativeXX I.Position=UDim2.new(.5,0,1,0)I.Size=UDim2.new(C,0,C,0)I.Parent=g local R=createChatBubbleTail(t,UDim2.new(1,0,.5,0))R.Parent=I return g end function createChatImposter(L,C,t)local l=Instance.new("\073\109\097\103\101\076\097\098\101\108")l.Name="\068\105\097\108\111\103\080\108\097\099\101\104\111\108\100\101\114"l.Image="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047"..(tostring(L).."\046\112\110\103")l.BackgroundTransparency=1 l.BorderSizePixel=0 l.Position=UDim2.new(0,0,-1.25,0)l.Size=UDim2.new(1,0,1,0)local g=Instance.new("\073\109\097\103\101\076\097\098\101\108")g.Name="\068\111\116\068\111\116\068\111\116"g.Image="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047"..(tostring(C).."\046\112\110\103")g.BackgroundTransparency=1 g.BorderSizePixel=0 g.Position=UDim2.new(.001,0,t,0)g.Size=UDim2.new(1,0,.7,0)g.Parent=l return l end local A={}A.ChatBubble={}A.ChatBubbleWithTail={}A.ScalingChatBubbleWithTail={}A.CharacterSortedMsg=l()


local function lerpLength(msg, min, max)
	return min + (max-min) * math.min(getMessageLength(msg)/75.0, 1.0)
end

local function createFifo()
	local this = {}
	this.data = {}

	local emptyEvent = Instance.new("BindableEvent")
	this.Emptied = emptyEvent.Event

	function this:Size()
		return #this.data
	end

	function this:Empty()
		return this:Size() <= 0
	end

	function this:PopFront()
		table.remove(this.data, 1)
		if this:Empty() then emptyEvent:Fire() end
	end

	function this:Front()
		return this.data[1]
	end

	function this:Get(index)
		return this.data[index]
	end

	function this:PushBack(value)
		table.insert(this.data, value)
	end

	function this:GetData()
		return this.data
	end

	return this
end

local function createCharacterChats()
	local this = {}

	this.Fifo = createFifo()
	this.BillboardGui = nil

	return this
end

local function createMap()
	local this = {}
	this.data = {}
	local count = 0

	function this:Size()
		return count
	end

	function this:Erase(key)
		if this.data[key] then count = count - 1 end
		this.data[key] = nil
	end

	function this:Set(key, value)
		this.data[key] = value
		if value then count = count + 1 end
	end

	function this:Get(key)
		if not key then return end
		if not this.data[key] then
			this.data[key] = createCharacterChats()
			local emptiedCon = nil
			emptiedCon = this.data[key].Fifo.Emptied:connect(function()
				emptiedCon:disconnect()
				this:Erase(key)
			end)
		end
		return this.data[key]
	end

	function this:GetData()
		return this.data
	end

	return this
end

local function createChatLine(message, bubbleColor, isLocalPlayer)
	local this = {}

	function this:ComputeBubbleLifetime(msg, isSelf)
		if isSelf then
			return lerpLength(msg,8,15)
		else
			return lerpLength(msg,12,20)
		end
	end

	this.Origin = nil
	this.RenderBubble = nil
	this.Message = message
	this.BubbleDieDelay = this:ComputeBubbleLifetime(message, isLocalPlayer)
	this.BubbleColor = bubbleColor
	this.IsLocalPlayer = isLocalPlayer

	return this
end

local function createPlayerChatLine(player, message, isLocalPlayer)
	local this = createChatLine(message, BubbleColor.WHITE, isLocalPlayer)

	if player then
		this.User = player.Name
		this.Origin = player.Character
	end

	return this
end

local function createGameChatLine(origin, message, isLocalPlayer, bubbleColor)
	local this = createChatLine(message, bubbleColor, isLocalPlayer)
	this.Origin = origin

	return this
end

function createChatBubbleMain(filePrefix, sliceRect)
	local chatBubbleMain = Instance.new("ImageLabel")
	chatBubbleMain.Name = "ChatBubble"
	chatBubbleMain.ScaleType = Enum.ScaleType.Slice
	chatBubbleMain.SliceCenter = sliceRect
	chatBubbleMain.Image = "rbxasset://textures/" .. tostring(filePrefix) .. ".png"
	chatBubbleMain.BackgroundTransparency = 1
	chatBubbleMain.BorderSizePixel = 0
	chatBubbleMain.Size = UDim2.new(1.0, 0, 1.0, 0)
	chatBubbleMain.Position = UDim2.new(0,0,0,0)

	return chatBubbleMain
end

function createChatBubbleTail(position, size)
	local chatBubbleTail = Instance.new("ImageLabel")
	chatBubbleTail.Name = "ChatBubbleTail"
	chatBubbleTail.Image = "rbxasset://textures/ui/dialog_tail.png"
	chatBubbleTail.BackgroundTransparency = 1
	chatBubbleTail.BorderSizePixel = 0
	chatBubbleTail.Position = position
	chatBubbleTail.Size = size

	return chatBubbleTail
end

function createChatBubbleWithTail(filePrefix, position, size, sliceRect)
	local chatBubbleMain = createChatBubbleMain(filePrefix, sliceRect)

	local chatBubbleTail = createChatBubbleTail(position, size)
	chatBubbleTail.Parent = chatBubbleMain

	return chatBubbleMain
end

function createScaledChatBubbleWithTail(filePrefix, frameScaleSize, position, sliceRect)
	local chatBubbleMain = createChatBubbleMain(filePrefix, sliceRect)

	local frame = Instance.new("Frame")
	frame.Name = "ChatBubbleTailFrame"
	frame.BackgroundTransparency = 1
	frame.SizeConstraint = Enum.SizeConstraint.RelativeXX
	frame.Position = UDim2.new(0.5, 0, 1, 0)
	frame.Size = UDim2.new(frameScaleSize, 0, frameScaleSize, 0)
	frame.Parent = chatBubbleMain

	local chatBubbleTail = createChatBubbleTail(position, UDim2.new(1,0,0.5,0))
	chatBubbleTail.Parent = frame

	return chatBubbleMain
end

function createChatImposter(filePrefix, dotDotDot, yOffset)
	local result = Instance.new("ImageLabel")
	result.Name = "DialogPlaceholder"
	result.Image = "rbxasset://textures/" .. tostring(filePrefix) .. ".png"
	result.BackgroundTransparency = 1
	result.BorderSizePixel = 0
	result.Position = UDim2.new(0, 0, -1.25, 0)
	result.Size = UDim2.new(1, 0, 1, 0)

	local image = Instance.new("ImageLabel")
	image.Name = "DotDotDot"
	image.Image = "rbxasset://textures/" .. tostring(dotDotDot) .. ".png"
	image.BackgroundTransparency = 1
	image.BorderSizePixel = 0
	image.Position = UDim2.new(0.001, 0, yOffset, 0)
	image.Size = UDim2.new(1, 0, 0.7, 0)
	image.Parent = result

	return result
end


local this = {}
this.ChatBubble = {}
this.ChatBubbleWithTail = {}
this.ScalingChatBubbleWithTail = {}
this.CharacterSortedMsg = createMap()
