-- Basic settings


local MAX_SPAM_SCORE = 5

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

local defaultMsg = "Tap here to chat"
local previousText = script.Parent.TypeArea.Text
local size = 1
local tweenInProgress = false
local spamScore = 0

function tweenToTripleSize()
	
	-- Tween size
	tweenInProgress = true
	local goal = { Size = UDim2.new(1, -365, 0, 60) }
	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(1, -365, 0, 48) }
	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(1, -365, 0, 36) }
	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
	
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
	
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 == 24 then
			tweenToDoubleSize()
			size = 2
		elseif size > 1 and script.Parent.TypeArea.TextBounds.Y == 12 then
			tweenToNormalSize()
			size = 1
		end
	end
	
	-- Set previous text
	previousText = script.Parent.TypeArea.Text
	
end)

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