--[[ ---------------------------------------------------------------------------
Created:		3/4/2011 5:46:08 AM
Last Modified:	2011-04-29T08:44:39Z
Author:			orgevo
Version:		v1.3a

AtlasLootFixers:
This file contains code, written by orgevo, intended to reverse or correct bugs I've found in the AtlasLoot code that are causing problems
for me.  It's possible that some of the original code isn't actually buggy at all, and that I'm *actually* introducing the bug with
my changes to their code.  In fact, I'm sure there is at least one case but probably a few, that I am perhaps misunderstanding, rather
than them being actual bugs in the AtlasLoot code.  That said, since this is my file, I am also free to correct that on a case-by-case
basis as I discover them, or they are pointed out to me by you, my wonderful fans!   :)
Let's just say there's just never enough time to double-check everything, generally speaking, in software development....
and this is simply a collection of "edge-cases" that I wanted to smooth out a little for my own needs.

AtlasLootAdvancedSearch 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 3 of the License, or
(at your option) any later version.

AtlasLootAdvancedSearch is distributed 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 AtlasLootAdvancedSearch.	If not, see <http://www.gnu.org/licenses/>.

----------------------------------------------------------------------------- ]]
local ADDON_TAG					= "AtlasLootAdvancedSearch"
local AtlasLootAdvancedSearch	= LibStub("AceAddon-3.0"):GetAddon(ADDON_TAG);
local alas_addon				= AtlasLootAdvancedSearch;
local al_addon					= AtlasLoot;
local ALBUGSPRAY_DEBUG			= false;
local ENABLE_LOGGING			= alas_addon.ENABLE_LOGGING and ALBUGSPRAY_DEBUG


-- localization
local L						= LibStub("AceLocale-3.0"):GetLocale(ADDON_TAG, true)
local AL					= LibStub("AceLocale-3.0"):GetLocale("AtlasLoot");
local BabbleInventory		= AtlasLoot_GetLocaleLibBabble("LibBabble-Inventory-3.0")
local ADDON_NAME			= L["ADDON_NAME"]

-- local GREY = al_addon.GREY;
-- local RED = al_addon.RED;
-- local WHITE = al_addon.WHITE;
-- local GREEN = al_addon.GREEN;
-- local PURPLE = al_addon.PURPLE;
-- local BLUE = al_addon.BLUE;
-- local ORANGE = al_addon.ORANGE;
-- 
local table, string = table, string;
-- local tinsert, tconcat = table.insert, table.concat
-- local strlen, strmatch, strfind, strsub, strformat, strlower, strupper = string.len, string.match, string.find, string.sub, string.format, string.lower, string.upper
local strgsub, strformat = string.gsub, string.format;
-- local pairs, next, type = pairs, next, type
local tostring = tostring
-- local AtlasLootAdvancedSearch_GetLootItemMetadata = AtlasLootAdvancedSearch_GetLootItemMetadata


-- print wrappers
-- t_print (testing): what I use while developing....most of these will be commented out in the repository versions
-- dev_print (development): outputs only when AtlasLootAdvancedSearch.dev_mode is true
local function r_print(str) alas_addon:Print(str); end
local function r_printf(str,...) alas_addon:Print(strformat(str,...)); end

local dbgProxy = alas_addon.DebugProxy;

local function isDevMode()			return dbgProxy and dbgProxy:isDevMode() end
local function isInternal()			return dbgProxy and dbgProxy:isInternal() end
local function t_print(str)			if dbgProxy and ENABLE_LOGGING then dbgProxy:t_print(str) end end
local function t_printf(str,...)	if dbgProxy and ENABLE_LOGGING then dbgProxy:t_printf(str, ...) end end
local function dev_print(str)		if dbgProxy and ENABLE_LOGGING then dbgProxy:dev_print(str) end end
local function dev_printf(str, ...)	if dbgProxy and ENABLE_LOGGING then dbgProxy:dev_printf(str,...) end end
local function d_print(str)			if dbgProxy and ENABLE_LOGGING then dbgProxy:d_print(str) end end
local function d_printf(str,...)	if dbgProxy and ENABLE_LOGGING then dbgProxy:d_printf(str,...) end end
local function funcEnter(obj, funcName, formatStr, ...)
	if alas_addon.DebugProxy.isEnabled and ENABLE_LOGGING then
		if formatStr then alas_addon.DebugProxy:TraceEnter(obj,funcName,strformat(formatStr,...));
		else alas_addon.DebugProxy:TraceEnter(obj,funcName); end
	end
end
local function funcExit(obj, funcName, formatStr, ...)
	if alas_addon.DebugProxy.isEnabled and ENABLE_LOGGING then
		if formatStr then alas_addon.DebugProxy:TraceExit(obj,funcName,strformat(formatStr,...));
		else alas_addon.DebugProxy:TraceExit(obj,funcName); end
	end
end




--[[==============================================================================
	BEGIN
	The following group of functions is responsible for preventing the code which
	handles modified clicks in the AtlasLoot codebase from breaking just about everything
	cool about the game's handling of key combos.
	==========================================================================--]]

-- this function actually handles resetting the scrips assigned to the individual buttons' "OnClick" handler.  Because of the dynamic way in which they build their frames, we can't rely
-- on the traditional means to hook up our functions during initialization.
local RedirectOnClickScript;
local originalOnClickItem, originalOnClickQAItem;

-- this is our OnClick handler for "item buttons" which do not correspond to achievements or quests (i.e. normal items)
function AtlasLootAdvancedSearch.OnClickAtlasLootItemButton(self, mouseButton, isDown)
	local itemLink = self.par:GetChatLink();
	
	-- we simply allow the game the opportunity to handle the input
	local result = ChatEdit_InsertLink(itemLink);
	
	--t_printf("OnClickAtlasLootItemButton - result:%s   itemLink:%s", tostring(result), strgsub(tostring(itemLink), "\124", "\124\124"));
	
	-- then I send the input right on its way, no changes.
	if not result then
		originalOnClickItem(self, mouseButton, isDown);
	end
end

-- this is our OnClick handler for achievement and quest "item buttons".  Essentially identical to the other function, but a little more fanangling must be performed in
-- order to extract the link in a format the game will understand (presumably)
function AtlasLootAdvancedSearch.OnClickAtlasLootQAButton(self, mouseButton, isDown)
	local itemLink;
	if self.questID then
		local questID = self.questID;
		itemLink = "|cffffff00|Hquest:"..questID.."|h["..AtlasLoot:GetQuestName(questID).."]|h|r";
	elseif self.achievementID then
		itemLink = GetAchievementLink(self.achievementID);
	end
	
	local result = ChatEdit_InsertLink(itemLink);
	--t_printf("OnClickAtlasLootQAButton - result:%s    itemLink:%s", tostring(result), strgsub(tostring(itemLink), "\124", "\124\124"));
	if not result then
		originalOnClickQAItem(self, mouseButton, isDown);
	end
end

-- and here's the function which replaces the script handlers for the buttons' OnClick event.
function AtlasLootAdvancedSearch.RedirectOnClickScript( itemButton )
	if itemButton ~= nil then
		local frm = itemButton.Frame;
		
		frm:RegisterForClicks("LeftButtonUp", "MiddleButtonUp", "RightButtonUp");
		local func = frm:GetScript("OnClick");
		if func and originalOnClickItem == nil then
			originalOnClickItem = func;
		end
		frm:SetScript("OnClick", AtlasLootAdvancedSearch.OnClickAtlasLootItemButton);
		
		frm = itemButton.Frame.QA;
		func = frm:GetScript("OnClick");
		if func and originalOnClickQAItem == nil then
			originalOnClickQAItem = func;
		end
		frm:SetScript("OnClick", AtlasLootAdvancedSearch.OnClickAtlasLootQAButton);
	end
	
end

-- I wrote this just in case, but due to the dynamic way the frames and buttons are generated, as well as *where* in the startup process they're doing this, I'm not
-- able to hook this up in time.
function AtlasLootAdvancedSearch:CreateAtlasLootItemButton(parent, point, name)
	t_printf("CreateAtlasLootItemButton - passing call to original handler.");
	local itemButton = self.hooks.AtlasLoot.CreateItemButton(parent, point, name);
	RedirectOnClickScript(itemButton);
	return itemButton;
end

RedirectOnClickScript = AtlasLootAdvancedSearch.RedirectOnClickScript;
--[[==============================================================================
	END
	==========================================================================--]]


