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



return function(data)
	local updateList
	local Title = data.Title
	local Tab = data.Table or data.Tab
	local Update = data.Update
	local UpdateArgs = data.UpdateArgs
	local AutoUpdate = data.AutoUpdate
	local LoadTime = data.LoadTime
	local origTable = gTable
	local gIndex = data.gIndex
	local gTable = data.gTable
	local Dots = data.Dots
	local Size = data.Size
	local Sanitize = data.Sanitize
	local Stacking = data.Stacking
	
	local gui = gTable.Object
	local info = gui.Desc
	local check = gui.Check
	local drag = gui.Drag
	local main = gui.Drag.Main
	local scroll = gui.Drag.Main.ScrollingFrame
	local title = gui.Drag.Title
	local close = gui.Drag.Close
	local hide = gui.Drag.Hide
	local entry = gui.Entry
	local drag2 = gui.Drag.Main.Dragger
	local search = gui.Drag.Search
	local refresh = gui.Drag.Refresh
	
	if Size then
		drag.Size = UDim2.new(0,Size[1],drag.Size.Y.Scale,drag.Size.Y.Offset)
		main.Size = UDim2.new(main.Size.X.Scale,main.Size.X.Offset,0,Size[2])
	end
	
	refresh.Visible = false
	search.Visible = false
	client.UI.Remove(Title,gui)
	gTable.Name = Title
	title.Text = Title
	
	drag.BackgroundColor3 = main.BackgroundColor3
	drag.Position = UDim2.new(0, -600, 0.5, -100)	
	
	search.InputBegan:connect(function(t)
		drag.Draggable = false
	end)
	
	search.InputEnded:connect(function(t)
		drag.Draggable = true
	end)
	
	close.MouseButton1Click:connect(function()
		drag.Draggable = false
		drag:TweenPosition(UDim2.new(drag.Position.X.Scale, drag.Position.X.Offset, 1,0), "Out", "Sine", 0.5, true)
		wait(0.5)
		gTable:Destroy()
	end)

	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.Players.LocalPlayer: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<200 then nx=200 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<220 then
				if topText=="" then
					topText=title.Text
				end
				title.Text=""
			else
				if topText~="" then
					title.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)
		if Dots then
			updateList()
		end
	end)
	
	LogGuiInfo = function(Type)
		local NewNumber = 0
		local newTab
		
		if Stacking then
			newTab = {}
			local lastTab
			for ind,ent in next,Tab do
				ent.Text = service.Trim(ent.Text)
				ent.Desc = service.Trim(ent.Desc)
				if not lastTab then
					lastTab = ent
					table.insert(newTab, ent)
				else
					if lastTab.Text == ent.Text and lastTab.Desc == ent.Desc then
						lastTab.Duplicates = (lastTab.Duplicates and lastTab.Duplicates+1) or 2
					else
						lastTab = ent
						table.insert(newTab, ent)
					end
				end
			end
		end
		
		scroll.CanvasSize = UDim2.new(0, 0, 0,(#Tab*20))
		for i,v in next,newTab or Tab do
			if (type(v) == "table" and ((v.Filter and string.find(string.lower(v.Filter),string.lower(Type))) or string.find(string.lower(v.Text),string.lower(Type)))) or (type(v) == "string" and string.find(string.lower(v),Type:lower())) or string.lower(Type) == "all" then
				local NewClone = entry:Clone()
				Expand(NewClone, info) 
				NewClone.Parent = scroll
				if not (type(LoadTime)=="boolean" and LoadTime==false) then -- Dark"s doin"
					if LoadTime then
						wait(LoadTime)
					elseif not AutoUpdate then
						service.Wait("RenderStepped")
					end
				end
				----
				if type(v) == "table" then 
					if v.Desc and v.Text then
						local text = NewClone:FindFirstChild("Text")
						local dot = NewClone:FindFirstChild("Dot")
						if text then
							local desc = NewClone:WaitForChild("Desc")
							text.Text = (data.Sanitize and service.SanitizeString(v.Text)) or v.Text
							desc.Value = v.Desc or ""
							if data.Font then
								text.Font = data.Font
							end
							if v.Duplicates then
								text.Text = "(x"..v.Duplicates..") "..text.Text
							end
							if v.Time then 
								text.Text = "["..v.Time.."] "..text.Text
							end
							if v.Color then
								text.TextColor3 = v.Color
								dot.TextColor3 = v.Color
							end
							if Dots then
								NewClone:WaitForChild("Dot")
								if text.TextFits then
									dot.Visible = false
								else
									dot.Visible = true
								end
							else
								text.TextScaled = true
								text.TextWrapped = true
								text.ClipsDescendants = false
							end
						end
					end
				elseif type(v) == "string" then
					local text = NewClone:FindFirstChild("Text")
					local dot = NewClone:FindFirstChild("Dot")
					if text then
						local desc = NewClone:WaitForChild("Desc")
						text.Text = v
						if data.Font then
							text.Font = data.Font
						end
						desc.Value = NewClone.Text.Text
						if Dots then
							if text.TextFits then
								dot.Visible = false
							else
								dot.Visible = true
							end
						else
							text.TextScaled = true
							text.TextWrapped = true
							text.ClipsDescendants = false
						end
					end
				end
				----
				NewClone.Position = UDim2.new(0, 2, 0, (NewNumber*20))
				NewClone.Visible = true
				NewNumber = NewNumber+1
				----
				if LoadTime then 
					scroll.CanvasSize = UDim2.new(0, 0, 0,(NewNumber*20))
				end
				----
			end
		end
		scroll.CanvasSize = UDim2.new(0, 0, 0, ((NewNumber)*20))
	end
	
	search.Visible = true
	
	search.FocusLost:connect(function(enterPressed)
		if enterPressed and (search.Text ~= "Search" and search.Text ~= "" or string.len(search.Text) > 0) then
			scroll:ClearAllChildren()
			LogGuiInfo(search.Text)
		elseif enterPressed and (search.Text=="" or string.len(search.Text)==0 or string.lower(search.Text)=="all") then
			scroll:ClearAllChildren()
			search.Text="Search"
			LogGuiInfo("all")
		else
			search.Text="Search"
		end
	end)
	
	refresh.Visible = false
	
	updateList = function()
		if (search.Text ~= "Search" and search.Text ~= "") then
			scroll:ClearAllChildren()
			LogGuiInfo(search.Text)
		else
			scroll:ClearAllChildren()
			LogGuiInfo("all")
		end
	end
	
	local autoupdating = true 
	
	search.Text = "Search"
	if Update then refresh.Visible = true end
	refresh.MouseButton1Click:connect(function()
		Tab = client.Remote.Get("UpdateList",Update,unpack(UpdateArgs or {}))
		updateList()
	end)
	
	Routine(function() wait(0.5) LogGuiInfo("all") end)
	
	gTable:Ready()
	drag:TweenPosition(UDim2.new(0, 35, 0.5, -100), "Out", "Sine", 0.5)
	
	if AutoUpdate then
		while gTable.Active and wait(AutoUpdate) do
			Tab = client.Remote.Get("UpdateList",Update,unpack(UpdateArgs or {}))
			updateList()
		end
	end
end