--This module is for any client FX related to the lab debris


local DoorFX = {}

local tweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)



local InitialModelCFrames = {}

function DoorFX.OPEN(door)
	if not door then return end
	
	local EffectsBrick = door:WaitForChild("Effects")
	local Effects = EffectsBrick:GetChildren()

	for _, Item in pairs (Effects) do
		if Item.Name == "Unlock" then Item:Play() continue end
		if Item:IsA("Beam") or Item:IsA("PointLight") or Item:IsA("ParticleEmitter") then
			Item.Enabled = true
		end
	end

	local Components = door:GetChildren()
	for _, Component in pairs (Components) do
		if not Component:IsA("Model") then continue end

		local descendants = Component:GetDescendants()
		for _, item in pairs (descendants) do
			if item:IsA("BasePart") and item ~= Component.PrimaryPart then
				local newWeld = Instance.new("WeldConstraint")
				newWeld.Part0 = item
				newWeld.Part1 = Component.PrimaryPart
				newWeld.Parent = Component.PrimaryPart
				item.Anchored = false
			end
		end

		InitialModelCFrames[Component] = Component.PrimaryPart.CFrame

		local CFrameGoal = (Component.PrimaryPart.CFrame + Vector3.new(math.random(-1,1),3,math.random(-1,1))) 
			* CFrame.Angles(math.rad(math.random(1,360)), math.rad(math.random(1,360)), math.rad(math.random(1,360)))
	
		local TweenInf = TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.In)
		tweenService:Create(Component.PrimaryPart, TweenInf, {CFrame = CFrameGoal}):Play()
	end

	wait(1)

	for _, Component in pairs (Components) do
		if not Component:IsA("Model") then continue end

		local CFrameGoal = (Component.PrimaryPart.CFrame + Vector3.new(math.random(-20,20),50,math.random(-20,20))) 
			* CFrame.Angles(math.rad(math.random(1,360)), math.rad(math.random(1,360)), math.rad(math.random(1,360)))
	
		local TweenInf = TweenInfo.new(2, Enum.EasingStyle.Linear)
		local Tween = tweenService:Create(Component.PrimaryPart, TweenInf, {CFrame = CFrameGoal})
		Tween:Play()
		local Event = nil
		Event = Tween.Completed:Connect(function()
			Event:Disconnect()
			Component.PrimaryPart.CFrame = Component.PrimaryPart.CFrame - Vector3.new(0, 300, 0)		
		end)
	end

	wait(.5)

	for _, Item in pairs (Effects) do
		if Item:IsA("Beam") or Item:IsA("PointLight") or Item:IsA("ParticleEmitter") then
			Item.Enabled = false
		end
	end

end

function DoorFX.CLOSE(door)
	if not door then return end

	local Components = door:GetChildren()
	for _, Component in pairs (Components) do
		if Component:IsA("Model") then
			Component.PrimaryPart.CFrame = InitialModelCFrames[Component] or Component.PrimaryPart.CFrame 
			--this errors when the module is called for some reason so thats why its like that
		end
	end
end

return DoorFX
