AddonClearer = {}

function AddonClearer:SetAddons(toDo)
	table.sort(ZO_AddOnsList.data, function(a,b) return a.data.addOnFileName == "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

	ZO_AddOnManager.isDirty = true
	-- Reset the view
	SCENE_MANAGER:RemoveFragment(ADDONS_FRAGMENT)
	SCENE_MANAGER:AddFragment(ADDONS_FRAGMENT)
end

local firstControl = nil

function AddonClearer:MultiSelect(button)
	if not IsShiftKeyDown() then firstControl = self return false end
	if firstControl == nil then firstControl = self return false end
	if self ~= firstControl and IsShiftKeyDown() then
		local firstRow = firstControl:GetParent().data
		local currentRow = self:GetParent().data
		local AddOnManager = GetAddOnManager()
		local step = firstRow.sortIndex	- currentRow.sortIndex < 0 and 1 or -1
		for i = firstRow.sortIndex, currentRow.sortIndex, step do
			AddOnManager:SetAddOnEnabled(AddonClearer.ReverseLookup[i], firstRow.addOnEnabled)
		end
	end
	return false
end

function AddonClearer:ShowPanel(...)
	local _, a,b = ...

	if not (a == 16 and b == 5) then return end

	for i,control in ipairs(ZO_AddOnsList.activeControls) do
		local name = control:GetNamedChild("Name")
		local enabled = control:GetNamedChild("Enabled")
		local enabledClick = enabled:GetHandler("OnClicked")
		name:SetMouseEnabled(true)
		name:SetHandler("OnMouseDown", function(self, button) AddonClearer.MultiSelect(enabled, button) enabledClick(enabled, button) end)
		ZO_PreHookHandler(enabled, "OnClicked", function(...) AddonClearer.MultiSelect(...) return false end)
	end

	AddonClearer.ReverseLookup = {}

	for i,v in ipairs(ZO_AddOnsList.data) do
		AddonClearer.ReverseLookup[v.data.sortIndex] = v.data.index
	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("AddonClearerShowing", EVENT_ACTION_LAYER_PUSHED, function(...) AddonClearer:ShowPanel(...) end)
end

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