-- 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.BuffSets = {}
MereHealingFrames.BuffSets.Sets = {}

MereHealingFrames.BuffSets.CurrentSet = nil

local BuffSets = MereHealingFrames.BuffSets

local function FindBuffSetForRole(soulSpec)
    local currentBuffSet = nil
    local currentBuffSetName = nil
    for setName, buffSet in pairs(BuffSets.Sets) do
        if not buffSet.SoulSpecs or buffSet.SoulSpecs[soulSpec] then
            currentBuffSet = buffSet
            currentBuffSetName = setName
            break
        end
    end
    return currentBuffSet, currentBuffSetName
end

function BuffSets.UpdateCurrentSet()
	local currentSoulSpec = Inspect.TEMPORARY.Role()

	if currentSoulSpec == nil then
		currentSoulSpec = 1
	end

	MereHealingFrames.Debug(1, "Using Soul %d", currentSoulSpec)

	local currentBuffSet, currentBuffSetName = FindBuffSetForRole(currentSoulSpec)

	if not currentBuffSet then
		print("Unable to find a valid Buff Set for this soul, please configure a buff set for the current role (Note this maybe a glitch while transitioning to larger number of roles)")

        if (MereHealingFrames.BuffSets.CurrentSet) then
            print ("Keeping the current buff set.")
            return
        else
            print ("Attempting to use buff set for role 1.")
            currentBuffSet, currentBuffSetName = FindBuffSetForRole(1)
            if not currentBuffSet then
                print ("Unable to locate buffset for role 1, something is wrong with your saved variables or buff configuration.")
                return
            end
            print ("Using buff set for role 1.")
        end
	end

	if BuffSets.CurrentSet ~= currentBuffSet then
		MereHealingFrames.Debug(1, "Using BuffSet %s", currentBuffSetName)
		BuffSets.CurrentSet = currentBuffSet
		BuffSets.UpdateCurrentBuffs()
	else
		MereHealingFrames.Debug(1, "Buff Set unchanged, still using %s", currentBuffSetName)
	end
end

function BuffSets.Save()
	return BuffSets.Sets
end

function BuffSets.Load(SavedVars)
	if SavedVars == nil then
		BuffSets.CreateEmptyBuffs()
	else
		BuffSets.Sets = SavedVars
	end
	BuffSets.UpdateCurrentSet()
end

function BuffSets.CreateEmptyTracker()
	return {
		buffType = "priority",
		buffCaster = "self",
		buffSlots = {}
		}
end

function BuffSets.CreateEmptyBuffs()
	BuffSets.Sets = {
		SampleSet = {
			Trackers = {
				hots = {
					buffType = "priority",
					buffList = "Healing Spray, Latent Blaze",
					debuffsOverBuffs = false,
					buffCaster = "self",
					buffSlots = {
						true
						}
					},
				shield = {
					buffType = "priority",
					buffList = "Ward of the Ancestors",
					debuffList = "Ancestral Slumber",
					debuffsOverBuffs = true,
					buffCaster = "self",
					buffSlots = {
						false,true
						}
					},
				blaze = {
					buffType = "priority",
					buffList = "Latent Blaze",
					debuffList = "Latent Blaze",
					debuffsOverBuffs = true,
					buffCaster = "self",
					buffSlots = {
						false,false, true
						}
					},
				Blessing = {
					buffType = "priority",
					buffList = "Fiery Blessing",
					debuffList = nil,
					debuffsOverBuffs = true,
					buffCaster = "self",
					buffSlots = {
						false,false,false, true
						}
					},
			},
			SoulSpecs = MereHealingFrames.Spells.createDefaultSoulSpec()
		}
	}
end



function BuffSets.ClearSoulSpec(soulSpec)
	for setName, set in pairs(BuffSets.Sets) do
		if not set.SoulSpecs then
			set.SoulSpecs = MereHealingFrames.Spells.createDefaultSoulSpec()
		end
		set.SoulSpecs[soulSpec] = false
	end
end

function BuffSets.ClearBuffSlot(currentBuffSet, buffSlot)
	local trackers = currentBuffSet.Trackers
	for trackerName, tracker in pairs(trackers) do
		if not tracker.buffSlots then
			tracker.buffSlots = {}
		end
		tracker.buffSlots[buffSlot] = false
	end
end

function BuffSets.CreateNewBuffSet(newSetName)
	if BuffSets.Sets[newSetName] then
		return false
	end

	local newBuffSet = {
		Trackers = {},
		SoulSpecs = MereHealingFrames.Spells.createDefaultSoulSpec(false)
	}
	BuffSets.Sets[newSetName] = newBuffSet

	return true
end

function BuffSets.DeleteBuffSet(setName)
	local buffset = BuffSets.Sets[setName]

	local availableSpecs =  MereHealingFrames.Config.UI.CountAvailableSpecs()
	local maxSpecs = math.min(availableSpecs, MereHealingFrames.StaticGlobals.numberOfSoulSpecs)
	for i=1, maxSpecs do
		if buffset.SoulSpecs[i] then
			print(string.format("Buff Set %s is still in use for soul spec %d", setName, i))
			return false
		end
	end

	BuffSets.Sets[setName] = nil
	return true
end


function BuffSets.RenameSet(setName, newSetName)
	local buffset = BuffSets.Sets[setName]

	if not buffset or BuffSets.Sets[newSetName] then
		print("Unable to rename set")
		return false
	end

	BuffSets.Sets[setName] = nil
	BuffSets.Sets[newSetName] = buffset
	return true
end

-- function BuffSets.CreateNewTracker(buffSet, newTrackerName)
-- end

-- function BuffSets.DeleteTracker(buffSet, trackerName)
-- end

-- function BuffSets.RenameTracker(buffSet, setName, newSetName)
	-- if not buffSet.Trackers[setName] or not buffSet.Trackers[newSetName] then
		-- return false
	-- end

	-- local tracker = buffSet.Trackers[setName]
	-- buffSet.Trackers[setName] = nil
	-- buffSet.Trackers[newSetName] = tracker
	-- return true
-- end

function BuffSets.UpdateCurrentBuffs()
	for unitId, playerInfo in pairs(MereHealingFrames.RaidManagement.Units) do
		playerInfo.buffMgmt:RefreshBuffSet(BuffSets.CurrentSet)
	end
end

function BuffSets.GetTracker(buffSlot)
	for trackerName, tracker in pairs(BuffSets.CurrentSet.Trackers) do
		if tracker.buffSlots[buffSlot] then
			return trackerName, tracker
		end
	end
	return nil, nil
end