--~ 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.

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

		return self;
	end,

	m_Instance = nil,

	Instance = function()
		if(nil == CallbackFactory.m_Instance) then
			CallbackFactory.m_Instance = CallbackFactory.new();
		end
		return CallbackFactory.m_Instance;
	end

}


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


ToggleMap = function()
	ToggleFrame(WorldMapFrame)
end

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

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

end

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

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

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

ToggleBags = function()
	ToggleAllBags();
end