--// All global vars will be wiped/replaced except script

local function Expand(ent, point)
	ent.MouseLeave:connect(function(x,y)
		point.Visible = false
	end)
	
	ent.MouseMoved:connect(function(x,y)
		point.Label.Text = ent.Desc.Value
		point.Size = UDim2.new(0, 10000, 0, 10000)
		local bounds = point.Label.TextBounds.X
		local rows = math.floor(bounds/500)
		rows = rows+1
		if rows<1 then rows = 1 end
		local newx = 500
		if bounds<500 then newx = bounds end
		point.Visible = true
		point.Size = UDim2.new(0, newx+5, 0, (rows*20)+5)
		point.Position = UDim2.new(0, x, 0, y-40-((rows*20)+5))
	end)
end

return function(data)
	local parent = game
	local Plr = service.Player
	local GUI = gTable.Object
	local NewCloneBar = GUI.Entry
	local Point = GUI.Desc
	local drag = GUI.Drag
	local main = GUI.Drag.Main
	local ScrollFr = GUI.Drag.Main.ScrollingFrame
	local top = GUI.Drag.Title
	local cls = GUI.Drag.Close
	local hide = GUI.Drag.Hide
	local fakeDrag = GUI.Drag.Main.FakeDragger
	local drag2 = GUI.Drag.Main.Dragger
	local Search = GUI.Drag.Search
	local Refresh = GUI.Drag.Refresh
	
	Refresh.Visible=true
	Search.Visible=true

	Search.InputBegan:connect(function(t)
		drag.Draggable=false
	end)
	Search.InputEnded:connect(function(t)
		drag.Draggable=true
	end)
	
	cls.MouseButton1Click:connect(function()
		drag.Draggable = false
		drag:TweenPosition(UDim2.new(0, -drag.Size.X.Offset, drag.Position.Y.Scale, drag.Position.Y.Offset), "Out", "Sine", 0.5, true)
		wait(0.5)
		gTable:Destroy()
	end)

	drag.BackgroundColor3 = main.BackgroundColor3
	
	hide.MouseButton1Click:connect(function() 
		if main.Visible then
			main.Visible = false
			drag.BackgroundTransparency = main.BackgroundTransparency
			hide.Text = "+"
		else
			main.Visible = true
			drag.BackgroundTransparency = 1
			hide.Text = "-"
		end
	end)
	
	local num = 0
	local mouse=service.Player:GetMouse()
	local nx,ny=drag.AbsoluteSize.X,main.AbsoluteSize.Y--405,400
	local dragging=false
	local defx,defy=nx,ny
	local topText=""
	mouse.Move:connect(function(x,y) 
		if dragging then
			nx=defx+(drag2.Position.X.Offset+20)
			ny=defy+(drag2.Position.Y.Offset+20)
			if nx<230 then nx=230 end
			if ny<200 then ny=200 end
			drag.Size=UDim2.new(0, nx, 0, 30) 
			main.Size=UDim2.new(1, 0, 0, ny)
			if nx<350 then
				if topText=="" then
					topText=top.Text
				end
				top.Text=""
			else
				if topText~="" then
					top.Text=topText
					topText=""
				end
			end
		end
	end)
	drag2.DragBegin:connect(function(init)
		dragging=true
	end)
	drag2.DragStopped:connect(function(x,y)
		dragging=false
		defx=nx
		defy=ny
		drag2.Position=UDim2.new(1,-20,1,-20)
		ScrollFr.CanvasSize = UDim2.new(0, 0, 0, (#ScrollFr:children())*20)
	end)
	
	local populate; populate = function(term)
		ScrollFr:ClearAllChildren()
		local num = 0
		local clone = NewCloneBar:Clone()
		clone.Position = UDim2.new(0, 5, 0, num*20)
		clone.Text = "Previous..."
		clone.Desc.Value = "Go back"
		clone.Visible = true
		clone.Parent = ScrollFr
		clone.Open.MouseButton1Click:connect(function()
			if parent.Parent then
				parent = parent.Parent
				populate()
			end
		end)
		Expand(clone, Point)
		num = num+1
		ScrollFr.CanvasSize = UDim2.new(0, 0, (num+1)/20, 0)
		
		for i,child in pairs(parent:GetChildren()) do
			if not term or term == "all" or term == "" or (term and string.find(string.lower(child.Name),string.lower(term))) then
				pcall(function()
					local clone = NewCloneBar:Clone()
					clone.Position = UDim2.new(0, 5, 0, num*20)
					clone.Text = tostring(child)
					clone:WaitForChild("Desc")
					clone.Desc.Value = child.ClassName
					clone.Visible = true
					clone.Parent = ScrollFr
					clone.Delete.MouseButton1Click:connect(function()
						child:Destroy()
						populate(term)
					end)
					clone.Open.MouseButton1Click:connect(function()
						parent = child
						populate()
					end)
					Expand(clone, Point)
					num = num+1
					ScrollFr.CanvasSize = UDim2.new(0, 0, (num+1)/20, 0)
				end)
			end
		end
	end
	----
	Search.FocusLost:connect(function(enterPressed)
		if enterPressed and (Search.Text ~= "Search" and Search.Text ~= "" or string.len(Search.Text) > 0) then
			populate(Search.Text)
		elseif enterPressed and (Search.Text=="" or string.len(Search.Text)==0 or string.lower(Search.Text)=="all") then
			ScrollFr:ClearAllChildren()
			Search.Text = "Search"
			populate()
		else
			Search.Text = "Search"
		end
	end)
	----
	Refresh.Visible = false
	----
	Search.Text="Search"
	
	Refresh.MouseButton1Click:connect(function()
		if (Search.Text ~= "Search" and Search.Text ~= "") then
			populate(Search.Text)
		else
			populate("all")
		end
	end)
	
	gTable:Ready()
	drag.Position = UDim2.new(0, -600, 0.5, -160)	
	drag:TweenPosition(UDim2.new(0, 35, 0.5, -160), "Out", "Sine", 0.5)
	populate("all")
end
