-- Basic settings
local MAX_SPAM_SCORE = 5

local plr = game.Players.LocalPlayer
local tweenService = game:GetService("TweenService")
local inputService = game:GetService("UserInputService")

local defaultMsg = "To chat click here or press the '/' key"
local previousText = script.Parent.TypeArea.Text
local size = 1
local tweenInProgress = false
local spamScore = 0

function tweenToVisible()
	
	-- Tween transparencies
	local goal1 = { BackgroundTransparency = 0.6 }
	local goal2 = { TextTransparency = 0.3 }
	tweenService:Create(script.Parent, TweenInfo.new(0.35, Enum.EasingStyle.Linear), goal1):Play()
	tweenService:Create(script.Parent.WhiteArea, TweenInfo.new(0.5, Enum.EasingStyle.Linear), goal1):Play()
	tweenService:Create(script.Parent.TypeArea, TweenInfo.new(0.5, Enum.EasingStyle.Linear), goal2):Play()
	
end

function tweenToInvisible()
	
	-- Tween transparencies
	local goal1 = { BackgroundTransparency = 0.8 }
	local goal2 = { TextTransparency = 0.65 }
	tweenService:Create(script.Parent, TweenInfo.new(0.35, Enum.EasingStyle.Linear), goal1):Play()
	tweenService:Create(script.Parent.WhiteArea, TweenInfo.new(0.5, Enum.EasingStyle.Linear), goal1):Play()
	tweenService:Create(script.Parent.TypeArea, TweenInfo.new(0.5, Enum.EasingStyle.Linear), goal2):Play()
	
end

function tweenToTripleSize()
	
	-- Tween size
	tweenInProgress = true
	local goal = { Size = UDim2.new(0.5, -230 , 0, 72) }
	local tween = tweenService:Create(script.Parent, TweenInfo.new(0.05, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), goal)
	tween.Completed:connect(function()
		tweenInProgress = false
	end)
	tween:Play()
	
end

function tweenToDoubleSize()
	
	-- Tween size
	tweenInProgress = true
	local goal = { Size = UDim2.new(0.5, -230 , 0, 56) }
	local tween = tweenService:Create(script.Parent, TweenInfo.new(0.05, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), goal)
	tween.Completed:connect(function()
		tweenInProgress = false
	end)
	tween:Play()
	
end

function tweenToNormalSize()
	
	-- Tween size
	tweenInProgress = true
	local goal = { Size = UDim2.new(0.5, -230 , 0, 40) }
	local tween = tweenService:Create(script.Parent, TweenInfo.new(0.05, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), goal)
	tween.Completed:connect(function()
		tweenInProgress = false
	end)
	tween:Play()
	
end

script.Parent.TypeArea.Focused:connect(function()
		
	-- Reset text if default
	if script.Parent.TypeArea.Text == defaultMsg then
		script.Parent.TypeArea.Text = ""
	end
	
	-- Make more visible
	tweenToVisible()
	
end)

script.Parent.TypeArea.FocusLost:connect(function(enterPressed)
	
	-- Check if enter was pressed
	if enterPressed then
		
		-- Clear spaces at beginning
		local msg = script.Parent.TypeArea.Text
		for i=1, string.len(script.Parent.TypeArea.Text) do
			if string.sub(script.Parent.TypeArea.Text, i, i) == ' ' and i < string.len(script.Parent.TypeArea.Text) then
				msg = string.sub(script.Parent.TypeArea.Text, i + 1, string.len(script.Parent.TypeArea.Text))
			else
				if string.sub(script.Parent.TypeArea.Text, i, i) == ' ' then
					msg = ""
				end
				break
			end
		end
		
		-- Clear spaces at end
		if msg ~= "" then
			local prevMsg = msg
			for i=string.len(prevMsg), 1, -1 do
				if string.sub(prevMsg, i, i) == ' ' and i > 1 then
					msg = string.sub(prevMsg, 1, i - 1)
				else
					if string.sub(prevMsg, i, i) == ' ' then
						msg = ""
					end
					break
				end
			end
		end
		
		-- Send message if not blank
		if msg ~= "" and spamScore <= MAX_SPAM_SCORE then
			game.ReplicatedStorage.Interactions.Server.SendChatMessage:FireServer(msg)
			spamScore = spamScore + 2
		elseif msg ~= "" and spamScore > MAX_SPAM_SCORE then
			plr.PlayerGui.MainGui.Chat.ShowChatMessage:Fire(nil, "You are chatting too quickly. You can chat again in " .. spamScore - MAX_SPAM_SCORE .. " seconds.")
		end
		
		-- Clear set text to default text
		script.Parent.TypeArea.Text = defaultMsg
		
	else
		
		-- Set to default if blank
		if script.Parent.TypeArea.Text == "" then
			script.Parent.TypeArea.Text = defaultMsg
		end
		
	end
	
	-- Make less visible
	tweenToInvisible()
	
end)

script.Parent.TypeArea:GetPropertyChangedSignal("Text"):connect(function()
	
	-- Check if went out of bounds
	if not tweenInProgress then
		if not script.Parent.TypeArea.TextFits and size == 1 then
			tweenToDoubleSize()
			size = 2
		elseif not script.Parent.TypeArea.TextFits and size == 2 then
			tweenToTripleSize()
			size = 3
		elseif not script.Parent.TypeArea.TextFits and size == 3 then
			script.Parent.TypeArea.Text = previousText
		elseif size > 2 and script.Parent.TypeArea.TextBounds.Y == 32 then
			tweenToDoubleSize()
			size = 2
		elseif size > 1 and script.Parent.TypeArea.TextBounds.Y == 16 then
			tweenToNormalSize()
			size = 1
		end
	end
	
	-- Set previous text
	previousText = script.Parent.TypeArea.Text
	
end)

inputService.InputBegan:connect(function(inputObj)
	
	-- Check if slash
	if inputObj.KeyCode == Enum.KeyCode.Slash and not script.Parent.TypeArea:IsFocused() then
		wait()
		script.Parent.TypeArea:CaptureFocus()
	end
	
end)

script.Parent.Toggle.MouseButton1Click:connect(function()
	
	-- Toggle visibility
	script.Parent.Parent.Chat.Visible = not script.Parent.Parent.Chat.Visible
	if script.Parent.Parent.Chat.Visible then
		script.Parent.Toggle.Text = "HIDE"
	else
		script.Parent.Toggle.Text = "SHOW"
	end
	
end)

while wait(1) do
	
	-- Decrement spam score
	if spamScore > 0 then
		spamScore = spamScore - 1
	end
	
end