-- << CUSTOM >>
-- Theme

local ratio = 6.1/#themeColors
local themeFrame = pages.custom["AB ThemeSelection"]
local themeDe = true
local function updateThemeSelection(themeName, themeColor)
	if themeName == nil then
		for i,v in pairs(themeColors) do
			if v[1] == main.pdata.Theme then
				themeName = v[1]
				themeColor = v[2]
				break
			end
		end
	end
	if themeName then
		local themeFrames = {}
		for a,b in pairs(main.gui:GetDescendants()) do
			if b:IsA("BoolValue") and b.Name == "Theme" then
				table.insert(themeFrames, b.Parent)
			end
		end 
		for _,f in pairs(themeFrames) do
			local newThemeColor = themeColor
			if f.Theme.Value then
				local h,s,v = Color3.toHSV(themeColor)
				newThemeColor = Color3.fromHSV(h, s, v*1.35)
			end
			if f:IsA("TextLabel") then
				local h,s,v = Color3.toHSV(themeColor)
				newThemeColor = Color3.fromHSV(h, s, v*2)
				f.TextColor3 = newThemeColor
			else
				f.BackgroundColor3 = newThemeColor
			end
		end
		for a,b in pairs(themeFrame:GetChildren()) do
			if b:IsA("TextButton") then
				if b.Name == themeName then
					b.BorderSizePixel = 1
				else
					b.BorderSizePixel = 0
				end
			end
		end
	end
end
for i, theme in pairs(themeColors) do
	local themeName = theme[1]
	local themeColor = theme[2]
	local box = themeFrame.ThemeTemplate:Clone()
	box.Name = themeName
	box.UIAspectRatioConstraint.AspectRatio = ratio
	box.BackgroundColor3 = themeColor
	box.MouseButton1Down:Connect(function()
		if themeDe then
			themeDe = false
			main.signals.ChangeSetting:InvokeServer{"Theme", themeName}
			updateThemeSelection(themeName, themeColor)
			themeDe = true
		end
	end)
	box.Visible = true
	box.Parent = themeFrame
end
