-- 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.Config = MereHealingFrames.Config or {}
MereHealingFrames.Config.UI = MereHealingFrames.Config.UI or {}

MereHealingFrames.Config.UI.SoulSpecsTab = {
	CallBackFunc = nil,
	Silent = false
	}

local MaxSoulSpecs = MereHealingFrames.StaticGlobals.numberOfSoulSpecs
local ChangeRoleAbility = "A2DEBC0F3EAB4AA48"

function MereHealingFrames.Config.UI.SoulSpecsTab:new ()
	local this = {}

	setmetatable(this, self)
	self.__index = self
	return this
end

local AvailableSpecs = MaxSoulSpecs
local AvailableSpecsNeedsUpdate = true

function MereHealingFrames.Config.UI.CountAvailableSpecs()
    if AvailableSpecsNeedsUpdate then
        AvailableSpecs = MereHealingFrames.Config.UI.ScanAvailableSpecs()
        AvailableSpecsNeedsUpdate = false
    end

    return AvailableSpecs
end

function MereHealingFrames.Config.UI.AvailableSpecsNeedUpdating()
    AvailableSpecsNeedsUpdate = true
end

-- TODO this should really live in a utility function somewhere
function MereHealingFrames.Config.UI.ScanAvailableSpecs()
	local abilityDetail = Inspect.Ability.New.Detail(ChangeRoleAbility)

	if not abilityDetail then
		MereHealingFrames.Debug(1, "Didn't find ability %s, perhaps no roles have been purchased, using one role (let me know if this is wrong)", ChangeRoleAbility)
		return 1
	end

	local availableSpecs = 0
	local abilityIds = Inspect.Ability.New.List()
	local abilityDetails = Inspect.Ability.New.Detail(abilityIds)
	for id, details in pairs(abilityDetails) do
		if details.name == abilityDetail.name then
			MereHealingFrames.Debug(1, "Found %s, id: %s", details.name, id)
			availableSpecs = availableSpecs + 1
		end
	end

	if availableSpecs == 0 then
		MereHealingFrames.Debug(1, "Didn't find any abilities called %s, which is odd, assuming %d specs.", abilityDetail.name, MaxSoulSpecs)
		return MaxSoulSpecs
	end

	return availableSpecs
end

function MereHealingFrames.Config.UI.SoulSpecsTab:CreateUI(parentFrame)
	self.Frame = UI.CreateFrame("Frame", "SoulSpecsTab", parentFrame)

	self.SpecsLabel = UI.CreateFrame("Text", "SpecsLabel", self.Frame)
	self.SpecsLabel:SetText("Pick Soul Specs")
	self.SpecsLabel:SetPoint("TOPLEFT", self.Frame, "TOPLEFT", 5, 5)

	self.SpecBoxes = {}
	local aboveFrame = self.SpecsLabel

	local availableSpecs = MereHealingFrames.Config.UI.CountAvailableSpecs()

	for i=1, MaxSoulSpecs do
		local checkBox = UI.CreateFrame("SimpleCheckbox", "SpecsBox"..i, self.Frame)

		checkBox:SetPoint("TOPLEFT", aboveFrame, "BOTTOMLEFT")
		checkBox:SetText("Soul Spec " .. i)
		checkBox:SetLabelPos("left")
		checkBox.Event.CheckboxChange = function() MereHealingFrames.Config.UI.SoulSpecsTab.SoulChanged(self, i) end
		self.SpecBoxes[i] = checkBox
		aboveFrame = checkBox

		if (i == availableSpecs) and (availableSpecs ~= MaxSoulSpecs)  then
			self.AvailableLabel =  UI.CreateFrame("Text", "SpecsAvailableLabel", self.Frame)
			self.AvailableLabel:SetText("Only specs/roles above are available, the ones below will activate when acquired")
			self.AvailableLabel:SetPoint("TOPLEFT", aboveFrame, "BOTTOMLEFT")
			aboveFrame = self.AvailableLabel
		end
	end
end

-- TODO: Maybe this should be an event?
function MereHealingFrames.Config.UI.SoulSpecsTab:SoulChanged(specId)
	if not self.Silent and self.CallBackFunc then
		self.CallBackFunc(specId, self.SpecBoxes[specId]:GetChecked())
	end
end

function MereHealingFrames.Config.UI.SoulSpecsTab:SetCallback(callbackFunc)
	self.CallBackFunc = callbackFunc
end

function MereHealingFrames.Config.UI.SoulSpecsTab:SetText(labelText)
	self.SpecsLabel:SetText(labelText)
end

function MereHealingFrames.Config.UI.SoulSpecsTab:GetContent()
	return self.Frame
end

function MereHealingFrames.Config.UI.SoulSpecsTab:SetSpecs(specsTable)
	self.Silent = true
	local AvailableSpecs = MereHealingFrames.Config.UI.CountAvailableSpecs()

	for i=1, MaxSoulSpecs do
		self.SpecBoxes[i]:SetChecked(specsTable[i] or false)
		self.SpecBoxes[i]:SetEnabled(i <= AvailableSpecs and not specsTable[i])
	end
	self.Silent = false
end