local M=game:GetService("\080\108\097\121\101\114\115")local B=script.Parent.Parent local f=B.Components local e=B.Modules local D=require(e.Packages)local A=require(D.Directory.Roact)local n=require(D.Directory.Signal)local H=require(f.StudioTheme)local b=require(f.PacketFrames)local d=require(f.TopbarButtonsGroup)local z=require(f.TopbarButton)local p=D.IsPlugin and game:GetService("\067\111\114\101\071\117\105")or M.LocalPlayer.PlayerGui local s=10 local C=A.Component:extend("\080\097\099\107\101\116\080\114\111\102\105\108\101\114")function C.init(M)M:setState({MaxFrameSize=1000})M.Cleanup={}M.PacketProfilerPaused,M.SetPacketProfilerPaused=A.createBinding(false)M.OnPacketProfilerPaused=n.new()table.insert(M.Cleanup,M.OnPacketProfilerPaused:Connect(function(B)M.SetPacketProfilerPaused(B)end))function M.Pause(B)M.OnPacketProfilerPaused:Fire(B)end end function C.didMount(M)table.insert(M.Cleanup,M.props.Signals.ProfilerPaused:Connect(function(B)M.Pause(B)end))end function C.willUnmount(M)for M,B in M.Cleanup do B:Disconnect()end end function C.render(M)local B=D.IsPlugin and"\076\101\102\116"or"\082\105\103\104\116"local f=D.IsPlugin and-1 or 1 return A.createElement(A.Portal,{target=p},{PacketProfiler=A.createElement("\083\099\114\101\101\110\071\117\105",{DisplayOrder=10,IgnoreGuiInset=true,ResetOnSpawn=false;ZIndexBehavior=Enum.ZIndexBehavior.Sibling},{Background=H(function(B)return A.createElement("\070\114\097\109\101",{BackgroundColor3=B:GetColor("\076\105\103\104\116");BorderSizePixel=1;BorderColor3=B:GetColor("\066\111\114\100\101\114");Size=UDim2.new(1,0,0,50),Position=UDim2.fromOffset(0,s)},{PacketFrames=A.createElement(b,{Enabled=M.props.Enabled;MaxFrameSize=M.state.MaxFrameSize;OnPacketProfilerPaused=M.OnPacketProfilerPaused;OnPacketProfilerEnabled=M.props.OnEnabled;Signals=M.props.Signals})})end);Topbar=H(function(e)return A.createElement("\070\114\097\109\101",{BackgroundColor3=e:GetColor("\073\116\101\109");BorderSizePixel=0;ZIndex=2;Size=UDim2.new(1,0,0,s)},{UIListLayout=A.createElement("\085\073\076\105\115\116\076\097\121\111\117\116",{FillDirection=Enum.FillDirection.Horizontal;HorizontalAlignment=B=="\076\101\102\116"and Enum.HorizontalAlignment.Left or Enum.HorizontalAlignment.Right,VerticalAlignment=Enum.VerticalAlignment.Center,SortOrder=Enum.SortOrder.LayoutOrder,Padding=UDim.new(0,5)});Title=A.createElement(z,{Text="\080\097\099\107\101\116\080\114\111\102\105\108\101\114";Theme=e,LayoutOrder=f});MaxKBScale=A.createElement(d,{Theme=e;Text="\077\097\120 \075\066 \115\099\097\108\101";Options={{Name="\049\048 \066",Callback=function()M:setState({MaxFrameSize=10})end};{Name="\049\048\048 \066";Callback=function()M:setState({MaxFrameSize=100})end},{Name="\049 \075\066",Callback=function()M:setState({MaxFrameSize=1000})end};{Name="\049\048 \075\066";Callback=function()M:setState({MaxFrameSize=10000})end};{Name="\053\048 \075\066";Callback=function()M:setState({MaxFrameSize=50000})end};{Name="\049\048\048 \075\066";Callback=function()M:setState({MaxFrameSize=100000})end}};LayoutOrder=f*2}),PausedLabel=A.createElement(z,{Text=M.PacketProfilerPaused:map(function(M)return M and"\091\080\097\117\115\101\100\093"or"\091\082\117\110\110\105\110\103\093"end);Theme=e,OnClick=function()M.props.Signals.ProfilerPaused:Fire(not M.PacketProfilerPaused:getValue())end,LayoutIndex=f*3})})end)})})end return C
local Players = game:GetService("Players")

local PacketProfiler = script.Parent.Parent
local Components = PacketProfiler.Components
local Modules = PacketProfiler.Modules

local Packages = require(Modules.Packages)
local Roact = require(Packages.Directory.Roact)
local Signal = require(Packages.Directory.Signal)

local StudioTheme = require(Components.StudioTheme)
local PacketFrames = require(Components.PacketFrames)
local TopbarButtonsGroup = require(Components.TopbarButtonsGroup)
local TopbarButton = require(Components.TopbarButton)

local TargetGui = Packages.IsPlugin and game:GetService("CoreGui") or Players.LocalPlayer.PlayerGui

local TOPBAR_HEIGHT = 10

local ProfilerComponent = Roact.Component:extend("PacketProfiler")

function ProfilerComponent:init()
	self:setState({
		MaxFrameSize = 1*1000,
	})

	self.Cleanup = {}

	self.PacketProfilerPaused, self.SetPacketProfilerPaused = Roact.createBinding(false)
	self.OnPacketProfilerPaused = Signal.new()

	table.insert(self.Cleanup, self.OnPacketProfilerPaused:Connect(function(IsPaused)
		self.SetPacketProfilerPaused(IsPaused)
	end))

	function self.Pause(IsPaused)
		self.OnPacketProfilerPaused:Fire(IsPaused)
	end
end

function ProfilerComponent:didMount()
	table.insert(self.Cleanup, self.props.Signals.ProfilerPaused:Connect(function(IsPaused)
		self.Pause(IsPaused)
	end))
end
function ProfilerComponent:willUnmount()
	for _, Connection in self.Cleanup do
		Connection:Disconnect()
	end
end

function ProfilerComponent:render()
	-- When using in-game, Roblox's topbar is on the top-left corner and obstructs the buttons
	local LayoutDirection = Packages.IsPlugin and "Left" or "Right"
	local LayoutIndex = Packages.IsPlugin and -1 or 1
	return Roact.createElement(Roact.Portal, {
		target = TargetGui,
	}, {
		PacketProfiler = Roact.createElement("ScreenGui", {
			DisplayOrder = 10,
			IgnoreGuiInset = true,
			ResetOnSpawn = false,
			ZIndexBehavior = Enum.ZIndexBehavior.Sibling,
		}, {
			Background = StudioTheme(function(Theme)
				return Roact.createElement("Frame", {
					BackgroundColor3 = Theme:GetColor("Light"),
					BorderSizePixel = 1,
					BorderColor3 = Theme:GetColor("Border"),
					Size = UDim2.new(1, 0, 0, 50),
					Position = UDim2.fromOffset(0, TOPBAR_HEIGHT),
				}, {
					PacketFrames = Roact.createElement(PacketFrames, {
						Enabled = self.props.Enabled,
						MaxFrameSize = self.state.MaxFrameSize,
						OnPacketProfilerPaused = self.OnPacketProfilerPaused,
						OnPacketProfilerEnabled = self.props.OnEnabled,
						Signals = self.props.Signals,
					})
				})
			end),
			Topbar = StudioTheme(function(Theme)
				return Roact.createElement("Frame", {
					BackgroundColor3 = Theme:GetColor("Item"),
					BorderSizePixel = 0,
					ZIndex = 2,
					Size = UDim2.new(1, 0, 0, TOPBAR_HEIGHT),
				}, {
					UIListLayout = Roact.createElement("UIListLayout", {
						FillDirection = Enum.FillDirection.Horizontal,
						HorizontalAlignment = LayoutDirection == "Left" and Enum.HorizontalAlignment.Left or Enum.HorizontalAlignment.Right,
						VerticalAlignment = Enum.VerticalAlignment.Center,
						SortOrder = Enum.SortOrder.LayoutOrder,
						Padding = UDim.new(0, 5),
					}),
					["Title"] = Roact.createElement(TopbarButton, {
						Text = "PacketProfiler",
						Theme = Theme,
						LayoutOrder = LayoutIndex
					}),
					["MaxKBScale"] = Roact.createElement(TopbarButtonsGroup, {
						Theme = Theme,
						Text = "Max KB scale",
						Options = {
							{
								Name = "10 B",
								Callback = function()
									self:setState({
										MaxFrameSize = 10,
									})
								end,
							},
							{
								Name = "100 B",
								Callback = function()
									self:setState({
										MaxFrameSize = 100,
									})
								end,
							},
							{
								Name = "1 KB",
								Callback = function()
									self:setState({
										MaxFrameSize = 1*1000,
									})
								end,
							},
							{
								Name = "10 KB",
								Callback = function()
									self:setState({
										MaxFrameSize = 10*1000,
									})
								end,
							},
							{
								Name = "50 KB",
								Callback = function()
									self:setState({
										MaxFrameSize = 50*1000,
									})
								end,
							},
							{
								Name = "100 KB",
								Callback = function()
									self:setState({
										MaxFrameSize = 100*1000,
									})
								end
							},
						},
						LayoutOrder = LayoutIndex * 2,
					}),
					["PausedLabel"] = Roact.createElement(TopbarButton, {
						Text = self.PacketProfilerPaused:map(function(IsPaused)
							return IsPaused and "[Paused]" or "[Running]"
						end),
						Theme = Theme,
						OnClick = function()
							self.props.Signals.ProfilerPaused:Fire(not self.PacketProfilerPaused:getValue())
						end,
						LayoutIndex = LayoutIndex * 3,
					})
				})
			end),
		})
	})
end

return ProfilerComponent