-- 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.SetsFrame = {
	getSetList = nil,	-- table <- nil
	renameSet = nil,	-- nil <- currentName, newName
	addSet = nil,		-- nil <- setName
	deleteSet = nil,	-- nil <- setName
	setSelected = nil,	-- nil <- setName
	addButtonText = "Add Set",
	deleteButtonText = "Delete Set",
	renameButtonText = "Rename Set",
	}

function MereHealingFrames.Config.UI.SetsFrame:new (settings, parentFrame)
	local this = settings or {}

	setmetatable(this, self)
	self.__index = self

	this.Frame = UI.CreateFrame("Frame", "SetsConfigFrame", parentFrame)
	return this
end

function MereHealingFrames.Config.UI.SetsFrame:CreateUI ()
	self.listScrollView = UI.CreateFrame("SimpleScrollView", "SetsScrollView", self:GetContent())
	self.listScrollView:SetPoint("TOPLEFT", self:GetContent(), "TOPLEFT", 5, 5)
	self.listScrollView:SetBorder(1, 1, 1, 1, 1)

	self.SetsList = UI.CreateFrame("SimpleList", "SpellSetList", self.listScrollView)
	self.SetsList.Event.ItemSelect = function(...) MereHealingFrames.Config.UI.SetsFrame.ChangeSetSelection(self, ...) end

	self:UpdateSetList()
	self.listScrollView:SetContent(self.SetsList)

	self.SetsList:SetSelectionMode("single")

	self.SetRenameText = MereHealingFrames.Config.UI.Utilities.CreateTextField(self:GetContent())
	self.SetRenameButton = UI.CreateFrame("RiftButton", "setsRenameButton", self:GetContent())
	self.SetAddButton = UI.CreateFrame("RiftButton", "setsAddButton", self:GetContent())
	self.SetDeleteButton = UI.CreateFrame("RiftButton", "setsDeleteButton", self:GetContent())

	self.SetDeleteButton:SetPoint("BOTTOMLEFT", self:GetContent(), "BOTTOMLEFT")
	self.SetAddButton:SetPoint("BOTTOMLEFT", self.SetDeleteButton, "TOPLEFT")
	self.SetRenameButton:SetPoint("BOTTOMLEFT", self.SetAddButton, "TOPLEFT")
	self.SetRenameText:SetPoint("BOTTOMLEFT", self.SetRenameButton, "TOPLEFT", 5, 0)
	self.SetRenameText:SetPoint("RIGHT", self.SetRenameButton, "RIGHT")
	self.listScrollView:SetPoint("BOTTOMRIGHT", self.SetRenameText, "TOPRIGHT", 0, -5)

	self.Frame:SetWidth(self.SetRenameText:GetWidth() + 5)

	self.SetAddButton.Event.LeftClick = function () MereHealingFrames.Config.UI.SetsFrame.AddNewSet(self) end
	self.SetRenameButton.Event.LeftClick = function () MereHealingFrames.Config.UI.SetsFrame.RenameSet(self) end
	self.SetDeleteButton.Event.LeftClick = function () MereHealingFrames.Config.UI.SetsFrame.DeleteSet(self) end

	self.SetRenameButton:SetText(self.renameButtonText)
	self.SetAddButton:SetText(self.addButtonText)
	self.SetDeleteButton:SetText(self.deleteButtonText)

	self.SetRenameText.Event.KeyUp = function () MereHealingFrames.Config.UI.SetsFrame.CheckButtonStatus(self) end
end

function MereHealingFrames.Config.UI.SetsFrame:SetSelectedIndex(index)
	return self.SetsList:SetSelectedIndex(index)
end

function MereHealingFrames.Config.UI.SetsFrame:SetSelectedItem(item)
    return self.SetsList:SetSelectedItem(item, false)
end

function MereHealingFrames.Config.UI.SetsFrame:CheckButtonStatus()
	local newSetName = self.SetRenameText:GetText()

	local enabled = (newSetName ~= self.SetsList:GetSelectedItem()) and (newSetName ~= "")
	self.SetRenameButton:SetEnabled(enabled)
	self.SetAddButton:SetEnabled(enabled)
end

function MereHealingFrames.Config.UI.SetsFrame:AddNewSet()
	local newSetName = self.SetRenameText:GetText()

	self.addSet(newSetName)
	self:UpdateSetList()
	self:SetSelectedItem(newSetName)
end

function MereHealingFrames.Config.UI.SetsFrame:DeleteSet()
	local currentSetName = self.SetsList:GetSelectedItem()

	self.deleteSet(currentSetName)
	self:UpdateSetList()
	self:SetSelectedIndex(1)
end

function MereHealingFrames.Config.UI.SetsFrame:RenameSet()
	local currentSetName = self.SetsList:GetSelectedItem()
	local newSetName = self.SetRenameText:GetText()

	self.renameSet(currentSetName, newSetName)
	self:UpdateSetList()
	self:SetSelectedItem(newSetName)
end

function MereHealingFrames.Config.UI.SetsFrame:UpdateSetList()
	local items = self.getSetList() or {}
	table.sort(items)
	self.SetsList:SetItems(items)
end

function MereHealingFrames.Config.UI.SetsFrame:ReloadSetList()
	local currentSetName = self.SetsList:GetSelectedItem()
	self:UpdateSetList()
    if (currentSetName) then
	    self:SetSelectedItem(currentSetName)
    end

	if self.SetsList:GetSelectedItem() == nil then
		self.SetsList:SetSelectedIndex(1)
	end
end

function MereHealingFrames.Config.UI.SetsFrame:ChangeSetSelection(view, item, index)
	self.SetRenameText:SetText(item)
	self:CheckButtonStatus()
	self.setSelected(item)
end
function MereHealingFrames.Config.UI.SetsFrame:GetContent()
	return self.Frame
end