local W=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local q=game:GetService("\080\108\097\121\101\114\115")local e=W.AvatarEditor local t=require(e.Shared.Util.Maid)local V=require(e.Shared.Util.Promise)local d=require(e.Shared.Util.Signal)local E=require(e.Shared.Settings)local L=require(e.Client.Theme)local v=(require(e.Gui.CostumeTemplateToLua))()local F=q.LocalPlayer local U={}U.__index=U local function z()local W=v:Clone()return W end function U.new(W)local q=setmetatable({},U)q.Maid=t.new()q.Listeners=t.new()q.ScrollingFrame=W.ScrollingFrame q.GridLayout=q.ScrollingFrame.UIGridLayout q.Wear=d.new()q.Delete=d.new()q.Children={}q.Pool={}for W=1,50,1 do q.Pool[W]=z()end q.Maid:GiveTask(L.Changed:Connect(function()q:Render()end))q.Maid:GiveTask(L:Bind(q.ScrollingFrame,"\083\099\114\111\108\108\066\097\114\073\109\097\103\101\067\111\108\111\114\051","\083\099\114\111\108\108\098\097\114"));((V.new(function(W,q)local e=F:WaitForChild("\065\069\095\067\111\115\116\117\109\101\115")W(e)end)):andThen(function(W)q.Folder=W q.Maid:GiveTask(W.ChildAdded:Connect(function()q:Render()end))q.Maid:GiveTask(W.ChildRemoved:Connect(function()q:Render()end))q:Render()end)):catch(warn)return q end function U.GetItem(W)local q=W.Pool local e=table.remove(q,#q)if not e then e=z()end return e end function U.Render(W)if not W.Folder then return end local e=W.Folder local t=W.Listeners local d=W.Children local v=W.ScrollingFrame local F=W.GridLayout local U=W.Pool local z=e:GetChildren()local C={}t:DoCleaning()for e,F in ipairs(z)do local U=d[e]or W:GetItem()C[e]=U U.ItemLabel.Text=F.Name U.More.Visible=false t:GiveTask(L:Bind(U,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\066\117\116\116\111\110"))t:GiveTask(L:Bind(U.ItemLabel,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))t:GiveTask(L:Bind(U.ItemLabel,"\084\101\120\116\083\116\114\111\107\101\067\111\108\111\114\051","\084\101\120\116\083\116\114\111\107\101"))t:GiveTask(L:Bind(U.More,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\068\101\108\101\116\101"))t:GiveTask(L:Bind(U.More,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"));((V.new(function(W,e)local t,V=pcall(function()return q:CreateHumanoidModelFromDescription(F,Enum.HumanoidRigType.R15,Enum.AssetTypeVerification.ClientOnly)end)if t then W(V)else e(V)end end)):andThen(function(W)t:GiveTask(W)W:SetPrimaryPartCFrame(CFrame.new())local q=W.Humanoid q.DisplayDistanceType=Enum.HumanoidDisplayDistanceType.None q.HealthDisplayType=Enum.HumanoidHealthDisplayType.AlwaysOff W.Animate:Destroy()local e=U.ViewportFrame local V=Instance.new("\067\097\109\101\114\097")t:GiveTask(V)V.CFrame=CFrame.lookAt(Vector3.new(-2,2,-5),W.PrimaryPart.Position)V.Parent=U e.CurrentCamera=V W.Parent=e.WorldModel t:GiveTask(function()e.CurrentCamera=nil end)end)):catch(warn)t:GiveTask(U.More.Activated:Connect(function(q)W.Delete:Fire(F)end))t:GiveTask(U.TouchLongPress:Connect(function(q,e)U.ItemLabel.Visible=false U.More.Visible=false W.Delete:Fire(F)end))t:GiveTask(U.InputBegan:Connect(function(W)if not E.HOVER_INPUTS[W.UserInputType]then return end U.ItemLabel.Visible=true if W.UserInputType==Enum.UserInputType.MouseMovement then U.More.Visible=true end end))t:GiveTask(U.InputEnded:Connect(function(W)if not E.HOVER_INPUTS[W.UserInputType]then return end U.ItemLabel.Visible=false U.More.Visible=false end))t:GiveTask(U.Activated:Connect(function(q)W.Wear:Fire(F)end))U.Parent=v end for W=#z+1,#d,1 do local q=d[W]q.Parent=nil table.insert(U,q)end W.Children=C v.CanvasSize=UDim2.new(0,0,0,F.AbsoluteContentSize.Y)end function U.Destroy(W)W.Maid:DoCleaning()W.Maid=nil W.Listeners:DoCleaning()W.Listeners=nil for W,q in ipairs(W.Children)do q:Destroy()end table.clear(W.Children)for W,q in ipairs(W.Pool)do q:Destroy()end table.clear(W.Pool)setmetatable(W,nil)end return U
-- Class
-- 0_1195
-- January 10, 2021

--[[



]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local AvatarEditor = ReplicatedStorage.AvatarEditor
local Maid = require(AvatarEditor.Shared.Util.Maid)
local Promise = require(AvatarEditor.Shared.Util.Promise)
local Signal = require(AvatarEditor.Shared.Util.Signal)
local Settings = require(AvatarEditor.Shared.Settings)
local Theme = require(AvatarEditor.Client.Theme)

local buttonTemplate = require(AvatarEditor.Gui.CostumeTemplateToLua)()

local player = Players.LocalPlayer

local Class = {}
Class.__index = Class


local function createButton()
	local button = buttonTemplate:Clone()
	return button
end


function Class.new(frame)
	local self = setmetatable({}, Class)
	
	self.Maid = Maid.new()
	self.Listeners = Maid.new()
	self.ScrollingFrame = frame.ScrollingFrame
	self.GridLayout = self.ScrollingFrame.UIGridLayout
	self.Wear = Signal.new()
	self.Delete = Signal.new()
	self.Children = {}
	self.Pool = {}
	
	for i = 1, 50 do
		self.Pool[i] = createButton()
	end
	
	self.Maid:GiveTask(Theme.Changed:Connect(function()
		self:Render()
	end))
	
	self.Maid:GiveTask(Theme:Bind(self.ScrollingFrame, "ScrollBarImageColor3", "Scrollbar"))
	
	Promise.new(function(resolve, reject)
		local folder = player:WaitForChild("AE_Costumes")
		resolve(folder)
	end)
	:andThen(function(folder)
		self.Folder = folder
		self.Maid:GiveTask(folder.ChildAdded:Connect(function()
			self:Render()
		end))
		self.Maid:GiveTask(folder.ChildRemoved:Connect(function()
			self:Render()
		end))
		self:Render()
	end)
	:catch(warn)
	
	return self
end


function Class:GetItem()
	local pool = self.Pool
	local button = table.remove(pool, #pool)
	if not button then
		button = createButton()
	end
	return button
end


function Class:Render()
	if not self.Folder then
		return
	end
	
	local folder = self.Folder
	local maid = self.Listeners
	local children = self.Children
	local scrollingFrame = self.ScrollingFrame
	local uiGridLayout = self.GridLayout
	local pool = self.Pool
	
	local list = folder:GetChildren()
	local newChildren = {}
	
	maid:DoCleaning()
	
	for i, v in ipairs(list) do
		local button = children[i] or self:GetItem()
		newChildren[i] = button
		
		button.ItemLabel.Text = v.Name
		button.More.Visible = false
		
		maid:GiveTask(Theme:Bind(button, "BackgroundColor3", "Button"))
		maid:GiveTask(Theme:Bind(button.ItemLabel, "TextColor3", "Text"))
		maid:GiveTask(Theme:Bind(button.ItemLabel, "TextStrokeColor3", "TextStroke"))
		maid:GiveTask(Theme:Bind(button.More, "BackgroundColor3", "Delete"))
		maid:GiveTask(Theme:Bind(button.More, "TextColor3", "Text"))
		
		Promise.new(function(resolve, reject)
			local success, result = pcall(function()
				return Players:CreateHumanoidModelFromDescription(v, Enum.HumanoidRigType.R15, Enum.AssetTypeVerification.ClientOnly)
			end)
			if success then
				resolve(result)
			else
				reject(result)
			end
		end)
		:andThen(function(result)
			maid:GiveTask(result)
			result:SetPrimaryPartCFrame(CFrame.new())
			
			local humanoid = result.Humanoid
			humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
			humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
			result.Animate:Destroy()
			
			local viewport = button.ViewportFrame
			local camera = Instance.new("Camera")
			maid:GiveTask(camera)
			camera.CFrame = CFrame.lookAt(Vector3.new(-2, 2, -5), result.PrimaryPart.Position)
			camera.Parent = button
			viewport.CurrentCamera = camera
			result.Parent = viewport.WorldModel
			maid:GiveTask(function()
				viewport.CurrentCamera = nil
			end)
		end)
		:catch(warn)
		
		maid:GiveTask(button.More.Activated:Connect(function(inputObject)
			self.Delete:Fire(v)
		end))
		
		maid:GiveTask(button.TouchLongPress:Connect(function(touchPositions, inputState)
			button.ItemLabel.Visible = false
			button.More.Visible = false
			self.Delete:Fire(v)
		end))
		
		maid:GiveTask(button.InputBegan:Connect(function(inputObject)
			if not Settings.HOVER_INPUTS[inputObject.UserInputType] then
				return
			end

			button.ItemLabel.Visible = true
			if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
				button.More.Visible = true
			end
		end))

		maid:GiveTask(button.InputEnded:Connect(function(inputObject)
			if not Settings.HOVER_INPUTS[inputObject.UserInputType] then
				return
			end

			button.ItemLabel.Visible = false
			button.More.Visible = false
		end))
		
		maid:GiveTask(button.Activated:Connect(function(inputObject)
			self.Wear:Fire(v)
		end))
		
		button.Parent = scrollingFrame
	end
	
	for i = #list + 1, #children do
		local button = children[i]
		button.Parent = nil
		table.insert(pool, button)
	end
	
	self.Children = newChildren
	scrollingFrame.CanvasSize = UDim2.new(0, 0, 0, uiGridLayout.AbsoluteContentSize.Y)
end


function Class:Destroy()
	self.Maid:DoCleaning()
	self.Maid = nil
	self.Listeners:DoCleaning()
	self.Listeners = nil
	for i, v in ipairs(self.Children) do
		v:Destroy()
	end
	table.clear(self.Children)
	for i, v in ipairs(self.Pool) do
		v:Destroy()
	end
	table.clear(self.Pool)
	setmetatable(self, nil)
end


return Class