local x=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local C=x.AvatarEditor local I=require(C.Shared.Util.Signal)local N=require(C.Shared.Util.Maid)local X=require(C.Shared.Settings)local a=C.Themes local O=require(a[X.DEFAULT_THEME])local q=X.DEFAULT_THEME local c={}c.Changed=I.new()function c.GetColor(C,x)return O[x]end function c.Set(C,x)if q==x then return end local I=a:FindFirstChild(x)if I then O=nil O=require(I)q=I.Name c.Changed:Fire(x)else warn(x,"\110\111\116 \097 \118\097\108\105\100 \116\104\101\109\101")end end function c.Bind(X,x,C,I)local a=N.new()local function O()x[C]=X:GetColor(I)end O()a:GiveTask(c.Changed:Connect(O))a:GiveTask(x.AncestryChanged:Connect(function(x,C)if not C then a:DoCleaning()O=nil end end))return a end function c.GetAll(x)local C={}local I=a:GetChildren()table.sort(I,function(x,C)return x.Name<C.Name end)for x,I in ipairs(I)do table.insert(C,I.Name)end return C end return c
-- Class
-- 0_1195
-- January 10, 2021

--[[



]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")

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

local themesFolder = AvatarEditor.Themes

local currentTheme = require(themesFolder[Settings.DEFAULT_THEME])
local currentName = Settings.DEFAULT_THEME


local module = {}
module.Changed = Signal.new()


function module:GetColor(style)
	return currentTheme[style]
end


function module:Set(themeName)
	if currentName == themeName then
		return
	end
	
	local themeModule = themesFolder:FindFirstChild(themeName)
	if themeModule then
		currentTheme = nil
		currentTheme = require(themeModule)
		currentName = themeModule.Name
		
		module.Changed:Fire(themeName)
	else
		warn(themeName, "not a valid theme")
	end
end


function module:Bind(frame, property, style)
	local connections = Maid.new()
	--maid:GiveTask(connections)
	--local ancestorChangedConnection, themeChangedConnection
	local function update()
		frame[property] = self:GetColor(style)
	end
	update()
	connections:GiveTask(module.Changed:Connect(update))
	connections:GiveTask(frame.AncestryChanged:Connect(function(_, parent)
		if not parent then
			connections:DoCleaning()
			update = nil
		end
	end))
	return connections
end


function module:GetAll()
	local themes = {}
	local themeModules = themesFolder:GetChildren()
	table.sort(themeModules, function(a, b)
		return a.Name < b.Name
	end)
	for i, v in ipairs(themeModules) do
		table.insert(themes, v.Name)
	end
	return themes
end


return module
