local H=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local y=H.AvatarEditor local N=require(y.Shared.Util.Maid)local l=require(y.Shared.Util.Signal)local b=require(y.Client.Theme)local v={}v.__index=v local function P(H)local y,N,l=string.match(H,"\094\035\063\040\037\119\037\119\041\040\037\119\037\119\041\040\037\119\037\119\041\036")return Color3.fromRGB(tonumber(y,16),tonumber(N,16),tonumber(l,16))end local function t(H)return string.format("\035\037\048\050\088\037\048\050\088\037\048\050\088",H.R*255,H.G*255,H.B*255)end local function I(H,y)return t(H)==t(y)end function v.new(H,P,t)local z=setmetatable({},v)z.Frame=H z.Wearing=t z.Children={}z.Maid=N.new()z.Listeners=N.new()z.ItemSelected=l.new()z.Maid:GiveTask(b.Changed:Connect(function()z:Render()end))z.Maid:GiveTask(b:Bind(H,"\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"))for N,l in ipairs(P)do local v=y.ColorTemplate:Clone()v.BackgroundColor3=l v.Wearing.Visible=I(t,l)v.Parent=H z.Children[N]=v z.Maid:GiveTask(b:Bind(v.Wearing,"\073\109\097\103\101\067\111\108\111\114\051","\087\101\097\114\105\110\103"))z.Maid:GiveTask(v.Activated:Connect(function(H)z.ItemSelected:Fire(N)end))end return z end function v.Render(H)local y=H.Listeners local N=H.Wearing y:DoCleaning()for H,y in ipairs(H.Children)do y.Wearing.Visible=I(N,y.BackgroundColor3)end end function v.UpdateWearing(y,H)y.Wearing=H y:Render()end function v.Destroy(H)H.Maid:Destroy()H.Maid=nil H.Listeners:Destroy()H.Listeners=nil H.Frame=nil H.Wearing=nil for H,y in ipairs(H.Children)do y:Destroy()end table.clear(H.Children)H.ItemSelected:Destroy()H.ItemSelected=nil end return v


local ReplicatedStorage = game:GetService("ReplicatedStorage")

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

local Class = {}
Class.__index = Class


local function fromHex(hex)
	local r, g, b = string.match(hex, "^#?(%w%w)(%w%w)(%w%w)$")
	return Color3.fromRGB(tonumber(r, 16), tonumber(g, 16), tonumber(b, 16))
end

local function toHex(color)
	return string.format("#%02X%02X%02X", color.R * 255, color.G * 255, color.B * 255)
end


local function isWearing(assets, color3)
	return toHex(assets) == toHex(color3)
end


function Class.new(frame, colorData, wearingTone)
	local self = setmetatable({}, Class)
	
	self.Frame = frame
	self.Wearing = wearingTone
	self.Children = {}
	self.Maid = Maid.new()
	self.Listeners = Maid.new()
	self.ItemSelected = Signal.new()
	
	self.Maid:GiveTask(Theme.Changed:Connect(function()
		self:Render()
	end))

	self.Maid:GiveTask(Theme:Bind(frame, "ScrollBarImageColor3", "Scrollbar"))
	
	for i, v in ipairs(colorData) do
		local button = AvatarEditor.ColorTemplate:Clone()
		button.BackgroundColor3 = v
		button.Wearing.Visible = isWearing(wearingTone, v)
		button.Parent = frame
		self.Children[i] = button
		
		self.Maid:GiveTask(Theme:Bind(button.Wearing, "ImageColor3", "Wearing"))
		
		self.Maid:GiveTask(button.Activated:Connect(function(inputObject)
			self.ItemSelected:Fire(i)
		end))
	end
	
	return self
end


function Class:Render()
	local maid = self.Listeners
	local wearingTone = self.Wearing
	
	maid:DoCleaning()
	
	for i, v in ipairs(self.Children) do
		v.Wearing.Visible = isWearing(wearingTone, v.BackgroundColor3)
	end
end


function Class:UpdateWearing(wearingTone)
	self.Wearing = wearingTone
	self:Render()
end


function Class:Destroy()
	self.Maid:Destroy()
	self.Maid = nil
	self.Listeners:Destroy()
	self.Listeners = nil
	self.Frame = nil
	self.Wearing = nil
	for i, v in ipairs(self.Children) do
		v:Destroy()
	end
	table.clear(self.Children)
	self.ItemSelected:Destroy()
	self.ItemSelected = nil
end


return Class
