--~ Warcraft Plugin for Cyborg MMO7
--~ Filename: CalbackFactory.lua
--~ Description: Creates lua callbacks that can be executed from a user keycombination
--~ Copyright (C) 2012 Mad Catz Inc.
--~ Author: Christopher Hooks

--~ This program is free software; you can redistribute it and/or
--~ modify it under the terms of the GNU General Public License
--~ as published by the Free Software Foundation; either version 2
--~ of the License, or (at your option) any later version.

--~ This program is distributed in the hope that it will be useful,
--~ but WITHOUT ANY WARRANTY; without even the implied warranty of
--~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--~ GNU General Public License for more details.

--~ You should have received a copy of the GNU General Public License
--~ along with this program; if not, write to the Free Software
--~ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

CyborgMMO_CallbackFactory = {
	new = function()
		local self = {}
		self.Frame = CreateFrame("Frame", "CallbackFactoryFrame", UIParent)
		self.Callbacks = {}
		self.Id = 1

		self.AddCallback = function(fn)
			local name = "Button"..self.Id
			self.Callbacks[name] = CreateFrame("Button", name, self.Frame)
			self.Callbacks[name]:SetScript("OnClick", fn)
			self.Id = self.Id + 1
			return self.Callbacks[name],self.Frame,name
		end

		self.RemoveCallback = function(name)
			self.Callbacks[name] = nil
		end

		self.GetCallback = function(callbackName)
			local callback = nil
			if callbackName == "Map" then
				callback = self.ToggleMap
			elseif callbackName == "CharacterPage" then
				callback = self.ToggleCharacterPage
			elseif callbackName == "Spellbook" then
				callback = self.ToggleSpellbook
			elseif callbackName == "Macros" then
				callback = self.ToggleMacros
			elseif callbackName == "QuestLog" then
				callback = self.ToggleQuests
			elseif callbackName == "Achievement" then
				callback = self.ToggleAchievements
			elseif callbackName == "Inventory" then
				callback = self.ToggleBags
			end
			return callback
		end


		self.ToggleMap = function()
			ToggleFrame(WorldMapFrame)
		end

		self.ToggleCharacterPage = function()
			ToggleCharacter("PaperDollFrame")
		end

		self.ToggleSpellbook = function()
			ToggleFrame(SpellBookFrame)
			if SpellBookFrame:IsShown() then
				SpellbookMicroButton:SetButtonState("PUSHED", 1)
			else
				SpellbookMicroButton:SetButtonState("NORMAL")
			end
		end

		self.ToggleMacros = function()
			if MacroFrame:IsShown() and MacroFrame:IsVisible() then
				HideUIPanel(MacroFrame)
			else
				ShowMacroFrame()
			end
		end

		self.ToggleQuests = function()
			ToggleFrame(QuestLogFrame)
			if QuestLogFrame:IsShown() then
				QuestLogMicroButton:SetButtonState("PUSHED", 1)
			else
				QuestLogMicroButton:SetButtonState("NORMAL")
			end
		end

		self.ToggleAchievements = function()
			ToggleAchievementFrame()
			if AchievementFrame and AchievementFrame:IsShown() then
				AchievementMicroButton:SetButtonState("PUSHED", 1)
			else
				if (HasCompletedAnyAchievement() or IsInGuild()) and CanShowAchievementUI() then
					AchievementMicroButton:Enable()
					AchievementMicroButton:SetButtonState("NORMAL")
				else
					AchievementMicroButton:Disable()
				end
			end
		end

		self.ToggleBags = function()
			ToggleAllBags()
		end
		return self
	end,

	m_Instance = nil,

	Instance = function()
		if not CyborgMMO_CallbackFactory.m_Instance then
			CyborgMMO_CallbackFactory.m_Instance = CyborgMMO_CallbackFactory.new()
		end
		return CyborgMMO_CallbackFactory.m_Instance
	end,
}


