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


return function(data)
	local playergui   = service.PlayerGui
	local localplayer = service.Players.LocalPlayer
	local gui     = script.Parent.Parent
	local openTab = data.Tab
	
	local drag  = gui.Drag
	local frame = gui.Drag.Frame
	local main  = gui.Drag.Frame.Main
	local title = gui.Drag.Frame.Title
	local entry = gui.Entry
	local desc  = gui.Desc
	
	local donateTab   = gui.Drag.Frame.Main.Donate
	local infoTab     = gui.Drag.Frame.Main.Info
	local keybindsTab = gui.Drag.Frame.Main.Keybinds
	local clientTab   = gui.Drag.Frame.Main.Client
	local settingsTab = gui.Drag.Frame.Main.Settings
	
	local close    = gui.Drag.Frame.Close
	local donate   = gui.Drag.Frame.DonateTab
	local info	   = gui.Drag.Frame.InfoTab
	local keybinds = gui.Drag.Frame.KeybindsTab
	local clientb  = gui.Drag.Frame.ClientTab
	local settings = gui.Drag.Frame.SettingsTab
	
	local colorBut  = gui.Color
	local stringBut = gui.String
	local tableBut  = gui.Table
	local toggleBut = gui.Toggle
	
	
	local playerData = client.Remote.Get("PlayerData")
	local setts 	 = client.Remote.Get("Setting",{"Prefix","SpecialPrefix","BatchKey","AnyPrefix"})
	
	settings.Visible = false
	
	gTable:Ready()
	drag.Position = UDim2.new(0,-500, 0.5, -200)
	drag:TweenPosition(UDim2.new(0.5, -190, 0.5, -200),nil,nil,0.5)
	
	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
	
	local function sortedPairs(t, f)
		local a = {}
		for n,b in pairs(t) do table.insert(a, n) end
		table.sort(a, f)
		local i = 0     
		local iter = function () 
			i = i + 1
			if a[i] == nil then 
				return nil
			else 
				return a[i], t[a[i]]
			end
		end
		return iter
	end
	
	local function openInfo()
		donateTab.Visible=false
		infoTab.Visible=true
		keybindsTab.Visible=false
		clientTab.Visible=false
		settingsTab.Visible=false
		donate.BackgroundTransparency=0.7
		info.BackgroundTransparency=0
		keybinds.BackgroundTransparency=0.7
		clientb.BackgroundTransparency=0.7
		settings.BackgroundTransparency=0.7
	end
	
	local function openDonate()
		donateTab.Visible=true
		infoTab.Visible=false
		keybindsTab.Visible=false
		clientTab.Visible=false
		settingsTab.Visible=false
		donate.BackgroundTransparency=0
		info.BackgroundTransparency=0.7
		keybinds.BackgroundTransparency=0.7
		clientb.BackgroundTransparency=0.7
		settings.BackgroundTransparency=0.7
	end
	
	local function openKeybinds()
		donateTab.Visible=false
		infoTab.Visible=false
		keybindsTab.Visible=true
		clientTab.Visible=false
		settingsTab.Visible=false
		donate.BackgroundTransparency=0.7
		info.BackgroundTransparency=0.7
		keybinds.BackgroundTransparency=0
		clientb.BackgroundTransparency=0.7
		settings.BackgroundTransparency=0.7
	end
	
	local function openClient()
		donateTab.Visible=false
		infoTab.Visible=false
		keybindsTab.Visible=false
		clientTab.Visible=true
		settingsTab.Visible=false
		donate.BackgroundTransparency=0.7
		info.BackgroundTransparency=0.7
		keybinds.BackgroundTransparency=0.7
		clientb.BackgroundTransparency=0
		settings.BackgroundTransparency=0.7
	end
	
	local function openSettings()
		donateTab.Visible=false
		infoTab.Visible=false
		keybindsTab.Visible=false
		clientTab.Visible=false
		settingsTab.Visible=true
		donate.BackgroundTransparency=0.7
		info.BackgroundTransparency=0.7
		keybinds.BackgroundTransparency=0.7
		clientb.BackgroundTransparency=0.7
		settings.BackgroundTransparency=0
	end
	
	if openTab then
		if string.lower(openTab)=="info" then
			openInfo()
		elseif string.lower(openTab)=="donate" then
			openDonate()
		elseif string.lower(openTab)=="keybinds" then
			openKeybinds()
		elseif string.lower(openTab)=="client" then
			openClient()
		elseif string.lower(openTab)=="settings" then
			openSettings()
		end
	else
		openInfo()
	end
	
	close.MouseButton1Click:connect(function()
		drag.Draggable = false
		drag:TweenPosition(UDim2.new(0,-500, drag.Position.Y.Scale, drag.Position.Y.Offset), nil, nil, 0.5)
		wait(1)
		gui:Destroy()
	end)
	
	donate.MouseButton1Click:connect(function()
		openDonate()
	end)
	
	info.MouseButton1Click:connect(function()
		openInfo()
	end)
	
	keybinds.MouseButton1Click:connect(function()
		openKeybinds()
	end)
	
	clientb.MouseButton1Click:connect(function()
		openClient()
	end)
	
	settings.MouseButton1Click:connect(function()
		openSettings()
	end)
	
	
	local prefix = setts.Prefix or ":"
	local specialPrefix = setts.SpecialPrefix or ""
	local batchKey = setts.BatchKey or "|"
	local anyPrefix = setts.AnyPrefix or "!"
	
	if not gui.Parent then return end
	
	-- Info tab
	
	local getScript = infoTab.GetScript
	local showCommands = infoTab.Cmds
	local donateBtn = infoTab.Donate
	local credit = infoTab.Credit
	local usageBtn = infoTab.Usage
	
	showCommands.MouseButton1Click:connect(function()
		client.Remote.Send("ProcessCommand",prefix.."cmds")
	end)
	
	getScript.MouseButton1Click:connect(function()
		client.Remote.Send('ProcessCommand',anyPrefix..'getscript') 
	end)
	
	donateBtn.MouseButton1Click:connect(function()
		openDonate()
	end)
	
	credit.MouseButton1Click:connect(function()
		client.Remote.Send('ProcessCommand',anyPrefix..'credit') 
	end)
	
	usageBtn.MouseButton1Click:connect(function()
		client.Remote.Send("ProcessCommand",anyPrefix.."usage")
	end)
	
	--[[
	local num=0
	for i,v in pairs(usage) do
		local NewClone = entry:clone()
		NewClone.Parent = Commands
		NewClone.Position = UDim2.new(0, 0, 0, num*20)
		NewClone.Text=v
		NewClone.Desc.Value=v
		NewClone.Visible=true
		Expand(NewClone, desc) 
		num = num +1
		Commands.CanvasSize = UDim2.new(0, 0, (num+1)/20, 0)
	end
	--]]
	
	-- Client Settings
	
	local cliScroller = clientTab.Scroll
	local cliSettings = {
		{
			Text = "Keybinds: ";
			Desc = "- Enabled/Disables Keybinds";
			Entry = toggleBut;
			Setting = "KeybindsEnabled";
			Function = function(clone)
				local toggle = clone.TextButton
				local keyDebounce = false
				toggle.MouseButton1Click:connect(function()
					if not keyDebounce then
						if client.Variables.KeybindsEnabled then
							client.Variables.KeybindsEnabled = false
							toggle.Text = "Disabled"
						else
							client.Variables.KeybindsEnabled = true
							toggle.Text = "Enabled"
						end
						local text = toggle.Text
						toggle.Text = "Saving.."
						client.Remote.Get("UpdateClient","KeybindsEnabled",client.Variables.KeybindsEnabled)
						toggle.Text = text
						keyDebounce = false
					end
				end)
				
				if client.Variables.KeybindsEnabled then
					toggle.Text = "Enabled"
				else
					toggle.Text = "Disabled"
				end
			end
		};
		{
			Text = "UI Keep Alive: ";
			Desc = "- Prevents Adonis UI deletion on death";
			Entry = toggleBut;
			Setting = "UIKeepAlive";
			Function = function(clone)
				local toggle = clone.TextButton
				local keepDebounce = false
				toggle.MouseButton1Click:connect(function()
					if not keepDebounce then
						if client.Variables.UIKeepAlive then
							client.Variables.UIKeepAlive = false
							toggle.Text = "Disabled"
						else
							client.Variables.UIKeepAlive = true
							toggle.Text = "Enabled"
						end
						local text = toggle.Text
						toggle.Text = "Saving.."
						client.Remote.Get("UpdateClient","UIKeepAlive",client.Variables.UIKeepAlive)
						toggle.Text = text
						keepDebounce = false
					end
				end)
				
				if client.Variables.UIKeepAlive then
					toggle.Text = "Enabled"
				else
					toggle.Text = "Disabled"
				end
			end
		};
		{
			Text = "Particle Effects: ";
			Desc = "- Enables/Disables certain Adonis made effects like sparkles";
			Entry = toggleBut;
			Setting = "ParticlesEnabled";
			Function = function(clone)
				local toggle = clone.TextButton
				local effectDebounce = false
				toggle.MouseButton1Click:connect(function()
					if not effectDebounce then
						if client.Variables.ParticlesEnabled then
							client.Variables.ParticlesEnabled = false
							client.Functions.EnableParticles(false)
							toggle.Text = "Disabled"
						else
							client.Variables.ParticlesEnabled = true
							client.Functions.EnableParticles(true)
							toggle.Text = "Enabled"
						end
						local text = toggle.Text
						toggle.Text = "Saving.."
						client.Remote.Get("UpdateClient","ParticlesEnabled",client.Variables.ParticlesEnabled)
						toggle.Text = text
						effectDebounce = false
					end
				end)
				
				if client.Variables.ParticlesEnabled then
					toggle.Text = "Enabled"
				else
					toggle.Text = "Disabled"
				end
			end
		};
		{
			Text = "Capes: ";
			Desc = "- Allows you to disable all player capes locally";
			Entry = toggleBut;
			Setting = "CapesEnabled";
			Function = function(clone)
				local toggle = clone.TextButton
				local capeCDebounce = false
				toggle.MouseButton1Click:connect(function()
					if not capeCDebounce then
						capeCDebounce = true
						if client.Variables.CapesEnabled then
							toggle.Text = "Disabled"
							client.Variables.CapesEnabled = false
							client.Functions.HideCapes(true)
						else
							toggle.Text = "Enabled"
							client.Variables.CapesEnabled = true
							client.Functions.HideCapes(false)
						end
						local text = toggle.Text
						toggle.Text = "Saving.."
						client.Remote.Get("UpdateClient","CapesEnabled",client.Variables.CapesEnabled)
						toggle.Text = text
						capeCDebounce = false
						if client.Variables.CapesEnabled then
							client.Functions.MoveCapes()
						else
							service.StopLoop("CapeMover")
						end
					end
				end)
				
				if client.Variables.CapesEnabled then
					toggle.Text = "Enabled"
				else
					toggle.Text = "Disabled"
				end
			end
		};
		{
			Text = "Console Key: ";
			Desc = "Key used to open the console";
			Entry = toggleBut;
			Setting = "CustomConsoleKey";
			Function = function(clone)
				local toggle = clone.TextButton
				local debounce = false
				toggle.Text = client.Variables.CustomConsoleKey or client.Remote.Get("Setting","ConsoleKeyCode")
				toggle.MouseButton1Click:connect(function()
					if not debounce then
						local gotKey
						debounce = true
						toggle.Text = "Waiting..."
						local event = service.UserInputService.InputBegan:connect(function(InputObject)
							local textbox = service.UserInputService:GetFocusedTextBox()
							if not (textbox) and rawequal(InputObject.UserInputType, Enum.UserInputType.Keyboard) then
								gotKey = InputObject.KeyCode.Name
							end
						end)
						
						repeat wait() until gotKey
						
						client.Variables.CustomConsoleKey = gotKey
						event:Disconnect()
						toggle.Text = "Saving.."
						client.Remote.Get("UpdateClient","CustomConsoleKey",client.Variables.CustomConsoleKey)
						toggle.Text = gotKey
						debounce = false
					end
				end)
			end
		};
		{
			Text = "Theme: ";
			Desc = "- Allows you to set the Adonis UI theme";
			Entry = gui.Theme;
			Setting = "CustomTheme";
			Function = function(clone)
				local toggle = clone.TextButton
				local themePicker = gui.ThemePicker
				local themeFrame = themePicker.Frame
				local themeEnt = themePicker.Entry
				local function showThemes()
					themeFrame:ClearAllChildren()
					
					local themes = {"Reset"}
					local num = 0
					
					for i,v in pairs(client.Deps.UI:GetChildren()) do
						table.insert(themes,v.Name)
					end
					
					for i,v in pairs(themes) do
						local new = themeEnt:Clone()
						new.Text = v
						new.Position = UDim2.new(0,0,0,20*num)
						new.Parent = themeFrame
						new.Visible = true
						new.MouseButton1Click:connect(function()
							service.Debounce("ClientSelectingTheme",function()
								themePicker.Visible = false
								toggle.Text = v
								client.Variables.CustomTheme = v
								if v == "Reset" then
									client.Remote.Get("UpdateClient","CustomTheme",nil)
								else
									client.Remote.Get("UpdateClient","CustomTheme",v)
								end
							end)
						end)
						num = num+1
					end
					
					themePicker.Position = UDim2.new(0,toggle.AbsolutePosition.X, 0, toggle.AbsolutePosition.Y)
					themePicker.Visible = true
				end
				
				toggle.MouseButton1Click:connect(function()
					service.Debounce("ClientDisplayThemes",function()
						if themePicker.Visible then
							themePicker.Visible = false
						else
							showThemes()
						end
					end)
				end)
				
				if client.Variables.CustomTheme then
					toggle.Text = client.Variables.CustomTheme
				else
					toggle.Text = "Reset"
				end
			end
		}
	}
	
	cliScroller:ClearAllChildren()
	local num = 0
	for i,tab in next,cliSettings do
		local clone = tab.Entry:Clone()
		local desc  = entry:Clone()
		
		clone.Text = tab.Text
		desc.Text  = tab.Desc
		
		clone.Position = UDim2.new(0,0,0,num*20)
		desc.Position  = UDim2.new(0,0,0,(num+1)*20)
		
		clone.Visible = true
		desc.Visible  = true
		
		desc.Font       = clone.Font
		desc.FontSize   = clone.FontSize
		desc.TextSize   = clone.TextSize
		desc.TextScaled = false
		
		clone.Parent = cliScroller
		desc.Parent  = cliScroller
				
		tab.Function(clone)
				
		num = num+2
	end
	
	cliScroller.CanvasSize = UDim2.new(0,0,0,num*20)
	
	-- Donate tab
	
	local donorEnabled = client.Remote.Get("Setting","DonorCommands")
	local pass = client.Remote.Get("Variable","DonorPass")
	
	if not 	gui.Parent then return end
	
	local capeTexture  = donateTab.Texture
	local capeColor    = donateTab.CapeColor
	local capeToggle   = donateTab.CapeToggle
	local capeMaterial = donateTab.Material
	local perksEnabled = donateTab.Enabled
	local Perks        = donateTab.Perks
	
	local robux     = donateTab.Robux
	local tickets   = donateTab.Tickets
	local extra     = donateTab.Extra
	local status    = donateTab.Status
	local extraPane = donateTab.ExtraPane
	
	local colorPicker    = capeColor.ColorPicker
	local materialPicker = capeMaterial.MaterialPicker
	local texturePicker  = capeTexture.TexturePicker
	local color  = colorPicker.Frame.Color
	local blue   = colorPicker.Frame.B
	local green  = colorPicker.Frame.G
	local red    = colorPicker.Frame.R
	local confirmColor = colorPicker.Frame.Confirm
	local cancelColor  = colorPicker.Frame.Cancel
	
	local donorData       = playerData.Donor
	local currentMaterial = donorData and donorData.Cape.Material
	local currentTexture  = donorData and donorData.Cape.Image
	local currentColor    = donorData and donorData.Cape.Color
	
	if donorEnabled then
		perksEnabled.Text = "Donor Commands: Enabled"
	else
		perksEnabled.Text = "Donor Commands: Disabled"
	end
	
	if type(currentColor)=="table" then
		currentColor = Color3.new(currentColor[1],currentColor[2],currentColor[3])
	else
		currentColor = BrickColor.new(donorData.Cape.Color).Color	
	end
	
	capeColor.BackgroundColor3 	= currentColor
	capeTexture.Text 	= currentTexture
	capeMaterial.Text 	= currentMaterial
	
	if playerData.Donor.Enabled then
		capeToggle.Text = "Enabled"
	else
		capeToggle.Text = "Disabled"
	end
	
	-- Perks
	local perkstab = {
		"If Enabled: ";
		"Customizable Cape";
		"Access to !cape";
		"Access to !uncape";
		"";
		--"Donor Gear (If enabled. Default Bongos)";
		"Access to !sparkles <Color>";
		"Access to !unsparkles";
		"Access to !particle";
		"Access to !unparticle";
		"Access to !fire <Color>";
		"Access to !unfire";
		"Access to !light <Color>";
		"Access to !unlight";
		"Access to !hat <ID>";
		"Access to !removehats";
		--"Access to !piano";
		"Access to !face";
		"Access to !shirt";
		"Access to !pants";
	}
	
	local num = 0
	for i,v in pairs(perkstab) do
		local NewClone = entry:clone()
		NewClone.Parent = Perks
		NewClone.Position = UDim2.new(0, 0, 0, num*20)
		NewClone.Text = v
		NewClone.Desc.Value = v
		NewClone.Visible = true
		NewClone.ZIndex = 10
		--Expand(NewClone, desc) 
		num = num +1
	end
	Perks.CanvasSize = UDim2.new(0, 0, 0, num*20)
	
	local num=0
	--FadeIn(0,0.5, cmf, top, NewClone, Hide, main)
	local donations = {
		{Price="10",ID=304651585};
		{Price="50",ID=304651782};
		{Price="100",ID=304651828};
		{Price="250",ID=304651879};
		{Price="500",ID=304651926};
		{Price="750",ID=304651990};
		{Price="1000",ID=304652066};
		{Price="2500",ID=304652168};
		{Price="5000",ID=304652233};
		{Price="7500",ID=304652294};
		{Price="10000",ID=304652425};
		{Price="50000",ID=304652516};
		{Price="100000",ID=304652569};--?????
	}
	for i,v in pairs(donations) do
		local cl = entry:clone()
		cl.Visible = true
		Expand(cl,desc)
		cl.Parent = extraPane
		cl.Visible = true
		cl.Text = v.Price
		cl.Desc.Value="Extra donation options. THIS DOES NOT GIVE PERKS!"
		cl.Position = UDim2.new(0,0,0,num*20)
		local rb = Instance.new("TextButton", cl) 
		rb.Style = "Custom" 
		rb.Size = UDim2.new(0,35,1,0)
		rb.TextScaled=true 
		rb.Position = UDim2.new(1,-40,0,0) 
		rb.ZIndex = 2
		rb.Font = "Arial"
		rb.FontSize = "Size18" 
		rb.Text = "RB" 
		rb.BackgroundColor3=Color3.new(0,170/255,0)
		rb.BorderSizePixel=0
		rb.TextColor3=Color3.new(1,1,1)
		rb.BackgroundTransparency=0.5
		rb.MouseButton1Click:connect(function() 
			service.MarketPlace:PromptPurchase(localplayer,v.ID)
		end)