--Code



if year == 2007 then
	script.Parent.ClickToChat.TextColor3 = Color3.new(0, 0, 0)
	script.Parent.ChatBar.BackgroundColor3 = Color3.new(255/255, 255/255, 255/255)
end

local Chat = { 
	ChatColors = {
		BrickColor.new("Bright red"),
		BrickColor.new("Bright blue"),
		BrickColor.new("Earth green"),
		BrickColor.new("Bright violet"),
		BrickColor.new("Bright orange"),
		BrickColor.new("Bright yellow"),
		BrickColor.new("Light reddish violet"),
		BrickColor.new("Brick yellow"),
	};

	Gui = nil,
	Frame = nil,
	RenderFrame = nil,
	TapToChatLabel = nil,
	ClickToChatButton = nil,
	Templates = nil,
	EventListener = nil,
	MessageQueue = {},
	
	Configuration = {								
		FontSize = Enum.FontSize.Size12,	
		NumFontSize = 12,
		HistoryLength = 20,
		Size = UDim2.new(0.38, 0, 0.20, 0),
		MessageColor = Color3.new(1 , 1, 1),
		AdminMessageColor = Color3.new(1, 215/255, 0),
		XScale = 0.025,
		LifeTime = 45,
		Position = UDim2.new(0, 2, 0.05, 0),
		DefaultTweenSpeed = 0.15,							
	};
	
	CachedSpaceStrings_List = {},
	Messages_List = {},
	MessageThread = nil,
	TempSpaceLabel = nil,
}

function GetNameValue(pName)
	local value = 0
	for index = 1, #pName do 
		local cValue = string.byte(string.sub(pName, index, index))
		local reverseIndex = #pName - index + 1
		if #pName%2 == 1 then 
			reverseIndex = reverseIndex - 1			
		end
		if reverseIndex%4 >= 2 then 
			cValue = -cValue 			
		end 
		value = value + cValue 
	end 
	return value%8
end 

function Chat:ComputeChatColor(pName)
	return self.ChatColors[GetNameValue(pName) + 1].Color
end 

function Chat:UpdateQueue(field, diff)	 				
	for i = #self.MessageQueue, 1, -1 do 			
		if self.MessageQueue[i] then 						
			for _, label in pairs(self.MessageQueue[i]) do 						
				if label and type(label) ~= "table" and type(label) ~= "number" then					
					if label:IsA("TextLabel") or label:IsA("TextButton") then 	
						if diff then 
							label.Position = label.Position - UDim2.new(0, 0, diff, 0) 
						else											
							if field == self.MessageQueue[i] then 						
								label.Position = UDim2.new(self.Configuration.XScale, 0, label.Position.Y.Scale - field["Message"].Size.Y.Scale , 0)
							else 							
								label.Position = UDim2.new(self.Configuration.XScale, 0, label.Position.Y.Scale - field["Message"].Size.Y.Scale, 0)							
							end  
							if label.Position.Y.Scale < -0.01 then 							
								label.Visible = false 						
								label:Destroy() 
							end 
						end 
					end 
				end 						
			end 
		end 
	end	
end 

function Chat:ComputeSpaceString(pLabel)
	local nString = " "
	if not self.TempSpaceLabel then 
		self.TempSpaceLabel = self.Templates.SpaceButton
		self.TempSpaceLabel.Size = UDim2.new(0, pLabel.AbsoluteSize.X, 0, pLabel.AbsoluteSize.Y); 
	end
	self.TempSpaceLabel.Text = nString
	while self.TempSpaceLabel.TextBounds.X < pLabel.TextBounds.X do 
		nString = nString .. " "
		self.TempSpaceLabel.Text = nString 				
	end 
	nString = nString .. " "
	self.CachedSpaceStrings_List[pLabel.Text] = nString 
	self.TempSpaceLabel.Text = ""
	return nString	
end


function Chat:UpdateChat(cPlayer, message)
	local messageField = {["Player"] = cPlayer,["Message"] = message}	
	if coroutine.status(Chat.MessageThread) == "dead" then 				
		table.insert(Chat.Messages_List, messageField)				
		Chat.MessageThread = coroutine.create(function ()
			for i = 1, #Chat.Messages_List do 	
				local field = Chat.Messages_List[i]																														
				Chat:CreateMessage(field["Player"], field["Message"]) 						
			end 
			Chat.Messages_List = {}									
		end)
		coroutine.resume(Chat.MessageThread)
	else 
		table.insert(Chat.Messages_List, messageField)
	end 
end 

function Chat:CreateMessage(cPlayer, message)		
	local pName = cPlayer ~= nil and cPlayer.Name or "GAME"	
	message = message:gsub("^%s*(.-)%s*$", "%1")		
	self.MessageQueue[#self.MessageQueue] = (#self.MessageQueue > self.Configuration.HistoryLength) and nil or self.MessageQueue[#self.MessageQueue]
	local pLabel = self.Templates.pLabel:clone()
	pLabel.Name = pName;
	pLabel.Text = pName .. ";";
	pLabel.Parent = self.RenderFrame;			
	pLabel.TextColor3 = (cPlayer.Neutral) and Chat:ComputeChatColor(pName) or cPlayer.TeamColor.Color 
	local nString = self.CachedSpaceStrings_List[pName] or Chat:ComputeSpaceString(pLabel)
	local mLabel = self.Templates.mLabel:clone()
	mLabel.Name = pName .. " - message";					
	mLabel.TextColor3 = Chat.Configuration.MessageColor;					
	mLabel.Text = nString .. message;
	mLabel.Parent = self.RenderFrame;	
	local heightField = mLabel.TextBounds.Y	
	mLabel.Size = UDim2.new(1, 0, heightField/self.RenderFrame.AbsoluteSize.Y, 0)	
	pLabel.Size = mLabel.Size
	local yPixels = self.RenderFrame.AbsoluteSize.Y
	local yFieldSize = mLabel.TextBounds.Y
	local queueField = {}	
	queueField["Player"] = pLabel 
	queueField["Message"] = mLabel 
	queueField["SpawnTime"] = tick()
	table.insert(self.MessageQueue, 1, queueField)		
	Chat:UpdateQueue(queueField)
end

function Chat:CreateChatBar()	
	self.ClickToChatButton = self.Gui:WaitForChild("ClickToChat")
	self.ChatBar = self.Gui:WaitForChild("ChatBar")
	local mouse = game.Players.LocalPlayer:GetMouse()
	mouse.KeyDown:connect(function (key)
		if key:byte() == 47 or key == "/" then
			wait(0.00000001)
			self.ClickToChatButton.Visible = false
			if year > 2007 then
				self.ChatBar.BackgroundColor3 = Color3.new(0/255, 0/255, 0/255)
			end
			self.ChatBar:CaptureFocus()
		end
	end)
	self.ClickToChatButton.MouseButton1Click:connect(function ()
		self.ClickToChatButton.Visible = false
		if year > 2007 then
			self.ChatBar.BackgroundColor3 = Color3.new(0/255, 0/255, 0/255)
		end
		self.ChatBar:CaptureFocus()
	end)	
end

function Chat:PlayerChat(message)
	local m = Instance.new("StringValue")
	m.Name = "NewMessage"
	m.Value = message
	m.Parent = game.Players.LocalPlayer
	game:GetService("Debris"):AddItem(m, 2)
end

function Chat:CreateGui()
	self.Gui = script.Parent
	self.Frame = self.Gui:WaitForChild("ChatFrame")
	self.RenderFrame = self.Frame:WaitForChild("ChatRenderFrame")
	self.Templates = self.Gui:WaitForChild("Templates")	
	Chat:CreateChatBar()
	self.ChatBar.FocusLost:connect(function(enterPressed)
		if year > 2007 then
			self.ChatBar.BackgroundColor3 = Color3.new(0/255, 0/255, 0/255)
		end
		if enterPressed and self.ChatBar.Text ~= "" then 
			local cText = self.ChatBar.Text
			if string.sub(self.ChatBar.Text, 1, 1)  == "%" then 
				cText = "(TEAM) " .. string.sub(cText, 2, #cText)
			end
			Chat:PlayerChat(cText)											
			if self.ClickToChatButton then 
				self.ClickToChatButton.Visible = true 
			end 
			self.ChatBar.Text = ""	
			self.ClickToChatButton.Visible = true							
		end	
	end)	
end

function Chat:TrackPlayerChats()
	local function chatRegister(p)
		p.ChildAdded:connect(function (obj)
			if obj:IsA("StringValue") and obj.Name == "NewMessage" then
				if game.Players.LocalPlayer.Neutral or p.TeamColor == game.Players.LocalPlayer.TeamColor then
					Chat:UpdateChat(p, obj.Value)
				end
			end
		end)
	end
	for _,v in pairs(game.Players:GetPlayers()) do
		chatRegister(v)
	end
	game.Players.PlayerAdded:connect(chatRegister)
end

function Chat:CullThread()
	Spawn(function ()
		while true do 
			if #self.MessageQueue > 0 then 
				for _, field in pairs(self.MessageQueue) do 				
					if field["SpawnTime"] and field["Player"] and field["Message"] and tick() - field["SpawnTime"] > self.Configuration.LifeTime then 
						field["Player"].Visible = false 
						field["Message"].Visible = false 
					end 
				end 
			end 
			wait(5)
		end
	end)
end

function Chat:Initialize()			
	Chat:CreateGui()
	Chat:TrackPlayerChats()
	self.MessageThread = coroutine.create(function () end)
						 coroutine.resume(self.MessageThread)
	Chat:CullThread()
end

Chat:Initialize()