-- This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
-- To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
-- or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

local MereHealingFrames, privateVars = ...

MereHealingFrames.SavedVars = {}

local SavedVarsSchemaVersions = {
	colours = 1,
	spells = 1,
	layouts = 1,
	raidSave = 1,
	buffs = 1,
	}

local SavedVarsSchemaConversions = {}

function MereHealingFrames.SavedVars.Load(handle, addonIdentifier)
	MereHealingFrames.Events.BumpEventCounter("Event.Addon.SavedVariables.Load.End")

	if addonIdentifier ~= "MereHealingFrames" then
		return
	end

	if MHF_SchemaVersions then
		for schema, version in pairs(MHF_SchemaVersions) do
			local currentVersion = SavedVarsSchemaVersions[schema]
			if (currentVersion == nil) then
				error("Unknown schema type in saved vars: " .. schema)
			end
			if version < currentVersion then
				local conversionFunc = SavedVarsSchemaConversions[schema]
				if conversionFunc then
					conversionFunc(version, currentVersion)
				end
			end
		end
	end

	if (MHF_MiscGlobals) then
		MereHealingFrames.SavedVars.LoadMiscGlobals(MHF_MiscGlobals)
	end

	if (MHF_Colours and MHF_Colours.CurrentColours) then
		MereHealingFrames.CurrentColours = MHF_Colours.CurrentColours
	end

	if (MHF_Spells) then
		MereHealingFrames.SpellSets = MHF_Spells
		MereHealingFrames.Spells.UpdateCurrentSpellSet()
	else
		MereHealingFrames.Spells.CreateEmptySpells()
	end

	MereHealingFrames.BuffSets.Load(MHF_Buffs)

	if (MHF_RaidSave) then
		MereHealingFrames.RaidManagement.LoadRaid(MHF_RaidSave)
	end

	if not MereHealingFrames.SavedVars.LoadLayouts() then
		local layoutSettings = {
			Name = "Main Layout",
		}
		local layout =  MereHealingFrames.HealingPanelLayout.Create(layoutSettings)
		MereHealingFrames.Layouts["Main Layout"] = layout
	end

	for layoutName, layout in pairs(MereHealingFrames.Layouts) do
		MereHealingFrames.Debug(3, "populating %s", layoutName)
		layout:ApplyFilters()
		layout:UpdateSpells()
		layout:SetVisible(true)
	end
    MereHealingFrames.RaidManagement.LibUnitChange.PrimeSpecs()
end

function MereHealingFrames.SavedVars.Save(handle, addonIdentifier)
	MereHealingFrames.Events.BumpEventCounter("Event.Addon.SavedVariables.Save.Begin")

    if addonIdentifier ~= "MereHealingFrames" then
        return
    end

	if #MereHealingFrames.CurrentColours then
		MHF_Colours = {}
		MHF_Colours.CurrentColours = MereHealingFrames.CurrentColours
	end

	MHF_Spells = MereHealingFrames.SpellSets

	MHF_SchemaVersions = SavedVarsSchemaVersions

	MHF_RaidSave = MereHealingFrames.RaidManagement.SaveRaid()

	MHF_Buffs = MereHealingFrames.BuffSets.Save()

	MHF_MiscGlobals = MereHealingFrames.SavedVars.SaveMiscGlobals()

	MereHealingFrames.SavedVars.SaveLayouts()
end


function MereHealingFrames.SavedVars.SaveLayouts()
	MHF_Layouts = {}
	for layoutName, layout in pairs(MereHealingFrames.Layouts) do
		MHF_Layouts[layoutName] = layout:Save()
	end
end

function MereHealingFrames.SavedVars.LoadLayouts()
	if not MHF_Layouts then
		return false
	end

	local loadedSomething = false
	for layoutName, layoutSettings in pairs(MHF_Layouts) do
		loadedSomething = true
		MereHealingFrames.Debug(3, "loading %s", layoutName)
		MereHealingFrames.Layouts[layoutName] = MereHealingFrames.HealingPanelLayout.Create(layoutSettings)
	end
	return loadedSomething
end

function MereHealingFrames.SavedVars.LoadMiscGlobals(miscGlobals)
	MereHealingFrames.ChangeDebugLevel(miscGlobals.debugLevel or 0)
	MereHealingFrames.HealingPanelsLocked = miscGlobals.lockedLayouts or false
    MereHealingFrames.LastSavedVersion = miscGlobals.LastSavedVersion or ""
end

function MereHealingFrames.SavedVars.SaveMiscGlobals()
	local miscGlobals = {}
	miscGlobals.debugLevel = MereHealingFrames.DebugLevel
	miscGlobals.lockedLayouts = MereHealingFrames.HealingPanelsLocked
    miscGlobals.LastSavedVersion = MereHealingFrames.fetchVersion()
	return miscGlobals
end


Command.Event.Attach(Event.Addon.SavedVariables.Save.Begin, MereHealingFrames.SavedVars.Save, "MereHealingFrames.SavedVars.Save")
Command.Event.Attach(Event.Addon.SavedVariables.Load.End, MereHealingFrames.SavedVars.Load, "MereHealingFrames.SavedVars.Load")