local D={}D.cr=coroutine.resume D.cc=coroutine.create D.crcc=function(D)return coroutine.resume(coroutine.create(D))end D.FindFirstChildWithAttribute=function(D,n,X,b)if type(D)=="\116\097\098\108\101"then D=n n=X end for D,J in pairs(D:GetChildren())do if J:GetAttribute(n)then if X==true then return J:GetAttribute(n)else if b and J:GetAttribute(n)==b then return J else return J end end end end return nil end D.FindFirstChildWithPropertyValue=function(D,n,X)for D,b in pairs(D:GetChildren())do if b[n]and b[n]==X then return b end end return nil end D.GlobalTrackSwitch=function(n,X)local b=D.FindFirstChildWithPropertyValue(workspace.GlobalSounds,"\080\108\097\121\105\110\103",true)local J=workspace.GlobalSounds:FindFirstChild(n)local r=J.Volume if J.Playing==true then return else J:Play()J.Volume=0 end local G=TweenService:Create(b,TweenInfo.new(X),{Volume=0})local e=TweenService:Create(J,TweenInfo.new(X),{Volume=r})G:Play()e:Play()D.crcc(function()task.wait(X)b:Stop()end)end D.TextTransition=function(D,n,X,b,J)local r=D[n]local G=string.len(r)local e=string.len(X)local y=b or 1 local N=J or"\067\111\108\108\097\112\115\101"if N=="\067\111\108\108\097\112\115\101"then for X=2,G/2,1 do D[n]=string.sub(r,X,G-(X-1))task.wait((y/2)*.25)end D[n]=" "task.wait((y/2)*.25)for b=e/2,0,-1 do D[n]=string.sub(X,b,e-(b-1))task.wait((y/2)*.25)end elseif N=="\083\099\114\097\109\098\108\101"then end end return D
--[[ 

TODO: Add a function to find first child with an attribute, corotine.resume, coroutine.create shortenings to cr and cc
		 
]]

local Utilities = {}

Utilities.cr = coroutine.resume
Utilities.cc = coroutine.create
Utilities.crcc = function(func)
	return coroutine.resume(coroutine.create(func))
end

Utilities.FindFirstChildWithAttribute = function(Parent, AttributeName, ReturnAttributeValue, Value)
	if type(Parent) == "table" then
		Parent = AttributeName
		AttributeName = ReturnAttributeValue
	end

	for _, Child in pairs(Parent:GetChildren()) do
		if Child:GetAttribute(AttributeName) then
			if ReturnAttributeValue == true then
				return Child:GetAttribute(AttributeName)
			else
				if Value and Child:GetAttribute(AttributeName) == Value then
					return Child
				else
					return Child
				end
			end
		end
	end

	return nil
end

Utilities.FindFirstChildWithPropertyValue = function(Parent, Property, Value)
	for _, Child in pairs(Parent:GetChildren()) do
		if Child[Property] and Child[Property] == Value then
			return Child
		end
	end

	return nil
end

Utilities.GlobalTrackSwitch = function(NewTrack, FadeTime)
	-- Find what Sound in workspace.GlobalSounds is playing, tween fade it out and tween fade in NewTrack

	-- Find the current track
	local CurrentTrack = Utilities.FindFirstChildWithPropertyValue(workspace.GlobalSounds, "Playing", true)
	local NewTrack = workspace.GlobalSounds:FindFirstChild(NewTrack)
	local NewTrackInitVol = NewTrack.Volume

	--print(CurrentTrack)

	-- If the new track is already playing, return
	if NewTrack.Playing == true then
		return
	else
		NewTrack:Play()
		NewTrack.Volume = 0
	end

	-- Fade out current track and fade in new track
	local CurrentTrackFade = TweenService:Create(CurrentTrack, TweenInfo.new(FadeTime), { Volume = 0 })
	local NewTrackFade = TweenService:Create(NewTrack, TweenInfo.new(FadeTime), { Volume = NewTrackInitVol })
	CurrentTrackFade:Play()
	NewTrackFade:Play()

	Utilities.crcc(function()
		task.wait(FadeTime)
		CurrentTrack:Stop()
	end)
end

Utilities.TextTransition = function(Object, TextProperty, EndText, Speed, TransitionType)
	local StartText = Object[TextProperty]
	local StartTextLength = string.len(StartText)
	local EndTextLength = string.len(EndText)

	local Speed = Speed or 1
	local TransitionType = TransitionType or "Collapse"

	if TransitionType == "Collapse" then
		-- Take one letter from beginning and end until no letters are left, then do the opposite with EndText

		for i = 2, StartTextLength / 2 do -- Collapse
			Object[TextProperty] = string.sub(StartText, i, StartTextLength - (i - 1))
			task.wait(Speed / 2 * 0.25)
		end

		Object[TextProperty] = " "
		task.wait(Speed / 2 * 0.25)

		for i = EndTextLength / 2, 0, -1 do -- Expand from middle
			Object[TextProperty] = string.sub(EndText, i, EndTextLength - (i - 1))
			task.wait(Speed / 2 * 0.25)
		end
	elseif TransitionType == "Scramble" then
		-- Randomly swap out letters from StartText to EndText
	end
end

return Utilities
