AddonClearer = {}

function AddonClearer:SetAddons(toDo)
	table.sort(ZO_AddOnsList.data, function(a,b) return a.data.addOnName == "AddonClearer" end) -- In place sorting. oh dear
	local thisAddonIndex = ZO_AddOnsList.data[1].data.index

	local notEmpty = false

	if not toDo then -- Save current setup
		AddonClearer.settings.savedSetup = {}
		for i,v in ipairs(ZO_AddOnsList.data) do AddonClearer.settings.savedSetup[v.data.index] = v.data.addOnEnabled end
	end

	if not toDo then
		local fullHouse = true
		local emptyHouse = true
		for i,v in ipairs(AddonClearer.settings.savedSetup) do
			if i ~= thisAddonIndex and not v then fullHouse = false end
			if i ~= thisAddonIndex and v then emptyHouse = false end
		end
		if not fullHouse and not emptyHouse then
			SelectAddonButton:SetText("Select Saved")
		else
			SelectAddonButton:SetText("Select All")
		end
	end

	local AddOnManager = GetAddOnManager()
	local numAddons = AddOnManager:GetNumAddOns()
	for i = 1, numAddons do
		if i ~= thisAddonIndex then -- ignore this addon
			if SelectAddonButton.nameLabel:GetText() == "Select Saved" and toDo then -- Are we restoring from save?
				AddOnManager:SetAddOnEnabled(i, AddonClearer.settings.savedSetup[i])
			else -- Otherwise continue as normal
				AddOnManager:SetAddOnEnabled(i, toDo)
			end
		end
	end
	-- Reset the view
	SCENE_MANAGER:RemoveFragment(ADDONS_FRAGMENT)
	SCENE_MANAGER:AddFragment(ADDONS_FRAGMENT)
end

function AddonClearer:ShowPanel(...)
	local a,b = ...
	if not (a == 14 and b == 5) then return end

end

function AddonClearer:Loaded(eventCode, addonName)
	if(addonName ~= "AddonClearer") then return end
	AddonClearer.settings  = ZO_SavedVars:NewAccountWide("AddonClearerSV", 1, nil, {savedSetup = {}})

	EVENT_MANAGER:RegisterForEvent("AddonShowing", EVENT_ACTION_LAYER_PUSHED, function(...) AddonClearer:ShowPanel(...) end)
end

EVENT_MANAGER:RegisterForEvent("AddonClearerLoaded", EVENT_ADD_ON_LOADED, function(...) AddonClearer:Loaded(...) end)