-- 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.Spells = {}
MereHealingFrames.Config.UI.Spells.buttonTab = {}
MereHealingFrames.Config.UI.ButtonConfig = {}

function MereHealingFrames.Config.UI.Spells:new ()
	local this = {}
	this.initialized = false

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

local modifiers = { "none", "ctrl", "alt", "shift" }

function MereHealingFrames.Config.UI.Spells:CreateUI (configWindow)
	self.abilityNames = nil

	self.Frame = UI.CreateFrame("Frame", "SpellsConfigFrame", configWindow)
	self.Frame:SetPoint("TOPLEFT", configWindow, "TOPLEFT")
	self.Frame:SetPoint("BOTTOMRIGHT", configWindow, "BOTTOMRIGHT")

	local SpellSetFrame = {
		getSetList = MereHealingFrames.Config.UI.Spells.getSpellSetList,	-- table <- nil
		renameSet = MereHealingFrames.Spells.RenameSoulSpec,	-- nil <- currentName, newName
		addSet = MereHealingFrames.Spells.CreateNewSpellSet,		-- nil <- setName
		deleteSet = MereHealingFrames.Spells.DeleteSpellSet,	-- nil <- setName
		setSelected = function(...)  MereHealingFrames.Config.UI.Spells.ChangeSpellSetSelection(self, ...) end,	-- nil <- setName
		addButtonText = "Add Spell Set",
		deleteButtonText = "Delete Spell Set",
		renameButtonText = "Rename Spell Set",
	}
	self.SpellSets = MereHealingFrames.Config.UI.SetsFrame:new (SpellSetFrame, self:GetContent())
	self.SpellSets:CreateUI(self:GetContent())
	self.SpellSets:GetContent():SetPoint("TOPLEFT", self:GetContent(), "TOPLEFT")
	self.SpellSets:GetContent():SetPoint("BOTTOMLEFT", self:GetContent(), "BOTTOMLEFT")

	self.LayoutTabs = UI.CreateFrame("SimpleTabView", "Config_Tabs", self:GetContent())
	self.LayoutTabs:SetBackgroundColor(0,0,0,0.5)
	self.LayoutTabs:SetPoint("TOPLEFT", self.SpellSets:GetContent(), "TOPRIGHT", 5, 0)
	self.LayoutTabs:SetPoint("BOTTOMRIGHT", self:GetContent(), "BOTTOMRIGHT")

	self.LayoutTabs:AddTab("Buttons", self:CreateButtonTab(self.LayoutTabs))

	self.SoulSpecs = MereHealingFrames.Config.UI.SoulSpecsTab:new()
	self.SoulSpecs:CreateUI(self.LayoutTabs)
	self.SoulSpecs:SetCallback(function (...) MereHealingFrames.Config.UI.Spells.SpecChange(self, ...) end)
	self.SoulSpecs:SetText("Pick Soul Specs to associate with this spell set:")

	self.LayoutTabs:AddTab("Soul Specs", self.SoulSpecs:GetContent())

	--self.LayoutTabs:AddTab("Filtering", self:CreateFilteringTab())

	--self.buttonScrollView:SetPoint("BOTTOMRIGHT", self.ButtonFrame, "BOTTOMLEFT", -10, 0)
	--self:RefreshAbilities()

	self.SpellSets:SetSelectedIndex(1)
end

function MereHealingFrames.Config.UI.Spells.getSpellSetList()
	local items = {}
	for key, layout in pairs(MereHealingFrames.SpellSets) do
		table.insert(items, key)
	end
	return items
end

function MereHealingFrames.Config.UI.Spells:ReloadSpellSetList()
	return self.SpellSets:ReloadSetList()
end

function MereHealingFrames.Config.UI.Spells:SpecChange(specId, newValue)
	if not self.currentSpellSet or not self.currentSpellSet.SoulSpecs then return end

	if newValue then
		MereHealingFrames.Spells.ClearSoulSpec(specId)
	end

	self.currentSpellSet.SoulSpecs[specId] = newValue

	-- should run a check that all specs have a valid spell set

	if (newValue) then
		self.SoulSpecs:SetSpecs(self.currentSpellSet.SoulSpecs)
		MereHealingFrames.Spells.UpdateCurrentSpellSet()
	end
end

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

function MereHealingFrames.Config.UI.Spells:RefreshAbilities()
	local abilityList = Inspect.Ability.New.List()
	local abilitydetails = Inspect.Ability.New.Detail(abilityList)

	self.abilityNames = {}
	local namesArray = {}
	for id, ability in pairs(abilitydetails) do
		table.insert(namesArray, ability.name)
		self.abilityNames[ability.name] = true
	end
	table.sort(namesArray)

	self.buttonTab.AbilitiesList:SetItems(namesArray)
end

function MereHealingFrames.Config.UI.Spells:ChangeSpellSetSelection(newSetName)
	if not MereHealingFrames.SpellSets[newSetName] then
		return
	end

	self.currentSpellSet = MereHealingFrames.SpellSets[newSetName]
	local selection = self.buttonTab.buttonList:GetSelectedIndex() or 1
	self.buttonTab.buttonList:ClearSelection(true)
	self.buttonTab.buttonList:SetSelectedIndex(selection)

	if not self.currentSpellSet.SoulSpecs then
		self.currentSpellSet.SoulSpecs = MereHealingFrames.Spells.createDefaultSoulSpec()
	end
	self.SoulSpecs:SetSpecs(self.currentSpellSet.SoulSpecs)
end

function MereHealingFrames.Config.UI.Spells:ChangeButtonSelection(view, item, index)
	self.currentbuttonSetup = self.currentSpellSet[index]

	if self.currentbuttonSetup == nil then
		self.currentbuttonSetup = {}
		self.currentSpellSet[index] = self.currentbuttonSetup
	end

	for i, modname in ipairs(modifiers) do
		if self.currentbuttonSetup[modname] == nil then
			self.currentbuttonSetup[modname] = {}
		end
		self.buttonTab.ModifierFrames[modname]:SetButtonConfig(self.currentbuttonSetup[modname])
	end
end

function MereHealingFrames.Config.UI.Spells:ChangeAbilitySelection(view, item, index)
	if self.CurrentSpellField then
		self.CurrentSpellField:SetText(item)
		self.CurrentSpellField.Event.KeyUp()
	end
end


function MereHealingFrames.Config.UI.Spells:CreateButtonTab()
	self.buttonTab = {}
	self.buttonTab.Frame = UI.CreateFrame("Frame", "SpellsButtonsTab", self:GetContent())
	self.buttonTab.Frame:SetWidth(400)
	self.buttonTab.Frame:SetHeight(500)

	self.buttonTab.spellsRefreshButton = UI.CreateFrame("RiftButton", "SpellsRefreshButton", self.buttonTab.Frame)
	self.buttonTab.spellsRefreshButton:SetPoint("BOTTOMRIGHT", self.buttonTab.Frame, "BOTTOMRIGHT")
	self.buttonTab.spellsRefreshButton:SetText("Refresh Abilities")
	self.buttonTab.spellsRefreshButton.Event.LeftClick = function() self:RefreshAbilities() end

	self.buttonTab.buttonScrollView = UI.CreateFrame("SimpleScrollView", "SpellButtonsScrollView", self.buttonTab.Frame)
	self.buttonTab.buttonScrollView:SetPoint("TOPLEFT", self.buttonTab.Frame, "TOPLEFT", 5, 5)
	self.buttonTab.buttonScrollView:SetPoint("BOTTOMLEFT", self.buttonTab.Frame, "BOTTOMLEFT", 5, -5)
	self.buttonTab.buttonScrollView:SetBorder(1, 1, 1, 1, 1)
	self.buttonTab.buttonScrollView:SetWidth(100)

	self.buttonTab.buttonList = UI.CreateFrame("SimpleList", "SpellButtonList", self.buttonTab.buttonScrollView)
	self.buttonTab.buttonList.Event.ItemSelect = function(...) MereHealingFrames.Config.UI.Spells.ChangeButtonSelection(self, ...) end
	self.buttonTab.buttonScrollView:SetContent(self.buttonTab.buttonList)

	local ButtonInternalValues = {
		"LeftClick",
		"RightClick",
		"MiddleClick",
		"Mouse4Click",
		"Mouse5Click",
		"WheelForward",
		"WheelBack"
	}

	local ButtonItems = {
		"Left Button",
		"Right Button",
		"Middle Button",
		"Fourth Button",
		"Fifth Button",
		"Wheel Up",
		"Wheel Down"
	}

	self.buttonTab.buttonList:SetItems(ButtonItems, ButtonInternalValues)
	self.buttonTab.buttonList:SetSelectionMode("single")

	-- add a frame on the right for spells list and refresh abilities button

	self.buttonTab.AbilitiesScrollView = UI.CreateFrame("SimpleScrollView", "SpellButtonsScrollView", self.buttonTab.Frame)
	self.buttonTab.AbilitiesScrollView:SetPoint("TOPRIGHT", self.buttonTab.Frame, "TOPRIGHT", -5, 5)
	self.buttonTab.AbilitiesScrollView:SetBorder(1, 1, 1, 1, 1)

	self.buttonTab.AbilitiesScrollView:SetPoint("BOTTOMLEFT", self.buttonTab.spellsRefreshButton, "TOPLEFT")

	self.buttonTab.AbilitiesList = UI.CreateFrame("SimpleList", "AbilitiesList", self.buttonTab.AbilitiesScrollView)
	self.buttonTab.AbilitiesList:SetItems({})
	self.buttonTab.AbilitiesList.Event.ItemSelect = function(...) MereHealingFrames.Config.UI.Spells.ChangeAbilitySelection(self, ...) end


	self.buttonTab.AbilitiesScrollView:SetContent(self.buttonTab.AbilitiesList)


	self.buttonTab.ButtonFrame = UI.CreateFrame("Frame", "SpellButtonConfigFrame", self.buttonTab.Frame)

	self.buttonTab.ButtonFrame:SetPoint("TOPLEFT", self.buttonTab.buttonScrollView, "TOPRIGHT", 5, 0)
	self.buttonTab.ButtonFrame:SetPoint("BOTTOMRIGHT", self.buttonTab.spellsRefreshButton, "BOTTOMLEFT", 5, 0)

	self.buttonTab.ModifierFrames = {}

	local modOffsetY = 5
	local lastFrame = nil
	for i, modname in ipairs(modifiers) do
		local modFrame = MereHealingFrames.Config.UI.ButtonConfig:new()
		modFrame:CreateUI(self.buttonTab.ButtonFrame, modname, self)
		self.buttonTab.ModifierFrames[modname] = modFrame
		if not lastFrame then
			modFrame:GetContent():SetPoint("TOPLEFT", self.buttonTab.ButtonFrame, "TOPLEFT")
		else
			modFrame:GetContent():SetPoint("TOPLEFT", lastFrame, "BOTTOMLEFT", 0, modOffsetY)
		end
		lastFrame = modFrame:GetContent()
	end

	return self.buttonTab.Frame
end


function MereHealingFrames.Config.UI.ButtonConfig:new()
	local this = {}
	this.initialized = false

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

function MereHealingFrames.Config.UI.ButtonConfig:CreateUI (ParentFrame, ModifierName, SpellsTab)
	self.Frame = UI.CreateFrame("Frame", "ButtonFrame"..ModifierName, ParentFrame)

	self.Label = UI.CreateFrame("Text", "ButtonLabel"..ModifierName, self.Frame)
	self.Label:SetText(ModifierName)
	self.Label:SetPoint("TOPLEFT", self.Frame, "TOPLEFT", 5, 5)

	self.radioButtonGroup = Library.LibSimpleWidgets.RadioButtonGroup("ModifierRadioGroup"..ModifierName)

	self.radioButtonSpell = UI.CreateFrame("SimpleRadioButton", "spell"..ModifierName, self.Frame)
	self.radioButtonSpell:SetText("Spell")
	self.radioButtonSpell:SetPoint("TOPLEFT", self.Frame, "TOPLEFT", 50, 5)


	self.SpellText = MereHealingFrames.Config.UI.Utilities.CreateTextField(self.Frame)
	self.SpellText:SetPoint("TOPLEFT", self.Frame, "TOPLEFT", 125, 5)


	self.radioButtonTarget = UI.CreateFrame("SimpleRadioButton", "target"..ModifierName, self.Frame)
	self.radioButtonTarget:SetPoint("TOPLEFT", self.radioButtonSpell, "BOTTOMLEFT", 0, 5)
	self.radioButtonTarget:SetText("Target")

	self.radioButtonCustom = UI.CreateFrame("SimpleRadioButton", "custom"..ModifierName, self.Frame)
	self.radioButtonCustom:SetPoint("TOPLEFT", self.radioButtonTarget, "BOTTOMLEFT", 0, 5)
	self.radioButtonCustom:SetText("Custom")
	self.radioButtonCustom:SetEnabled(false)

	self.radioButtonNothing = UI.CreateFrame("SimpleRadioButton", "nothing"..ModifierName, self.Frame)
	self.radioButtonNothing:SetPoint("TOPLEFT", self.radioButtonCustom, "BOTTOMLEFT", 0, 5)
	self.radioButtonNothing:SetText("Nothing")

	self.radioButtonGroup:AddRadioButton(self.radioButtonSpell)
	self.radioButtonGroup:AddRadioButton(self.radioButtonTarget)
	self.radioButtonGroup:AddRadioButton(self.radioButtonCustom)
	self.radioButtonGroup:AddRadioButton(self.radioButtonNothing)
	self.Frame:SetHeight(self.radioButtonNothing:GetBottom())

	self.radioButtonTarget.Event.RadioButtonSelect = function()
		self.config.spell = nil
		self.config.target = true
		self.config.custom = nil
		MereHealingFrames.Spells.UpdateCurrentSpells()
	end

	self.radioButtonCustom.Event.RadioButtonSelect = function()
	  	self.config.spell = nil
		self.config.target = nil
		self.config.custom = ""
		MereHealingFrames.Spells.UpdateCurrentSpells()
	end

	self.radioButtonSpell.Event.RadioButtonSelect = function()
		self.config.spell = self.SpellText:GetText()
		self.config.target = nil
		self.config.custom = nil
		MereHealingFrames.Spells.UpdateCurrentSpells()
	end

	self.radioButtonNothing.Event.RadioButtonSelect = function()
	  	self.config.spell = nil
		self.config.target = nil
		self.config.custom = nil
		MereHealingFrames.Spells.UpdateCurrentSpells()
	end

	self.SpellText.Event.KeyFocusGain = function ()
		SpellsTab.CurrentSpellField = self.SpellText
		self.radioButtonGroup:SetSelectedIndex(1, true)
	end

	self.SpellText.Event.KeyUp = function ()
		local spellName = self.SpellText:GetText()

		if SpellsTab.abilityNames then
			if SpellsTab.abilityNames[spellName] then
				self.SpellText:SetBorderColor(0,1,0,1)
			else
				self.SpellText:SetBorderColor(1,1,0,1)
			end
		end

		if self.radioButtonSpell:GetSelected() then
			self.config.spell = spellName
			self.config.target = nil
			self.config.custom = nil
			MereHealingFrames.Spells.UpdateCurrentSpells()
		end
	end

end

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

function MereHealingFrames.Config.UI.ButtonConfig:SetButtonConfig(cast)
	self.config = cast
	self.SpellText:SetText("")
	self.SpellText:SetBorderColor(1,1,1,1)

	if cast == nil then
		error("cast must be a table, otherwise it can't be updated")
		return
	end

	if cast.spell then
		self.radioButtonGroup:SetSelectedIndex(1, true)
		self.SpellText:SetText(cast.spell)
	elseif cast.target then
		self.radioButtonGroup:SetSelectedIndex(2, true)
	elseif cast.custom then
		self.radioButtonGroup:SetSelectedIndex(3, true)
	else
		self.radioButtonGroup:SetSelectedIndex(4, true)
	end
end