--[[ ---------------------------------------------------------------------------
Created:		7/25/2010 3:32:29 AM
Last Modified:	2010-12-11T20:28:16Z
Author:			orgevo
Version:		v1.3a

DebugHelper:	Debugging functions and data structures

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

AtlasLoot_AdvancedSearch 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 AtlasLoot_AdvancedSearch.	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 ENABLE_LOGGING			= alas_addon.ENABLE_LOGGING

-- localization
local L						= LibStub("AceLocale-3.0"):GetLocale(ADDON_TAG, true)
local AL					= LibStub("AceLocale-3.0"):GetLocale("AtlasLoot");
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 tinsert, tconcat = table.insert, table.concat
local strlen, strmatch, strfind, strsub, strformat, strlower, strupper, spad = string.len, string.match, string.find, string.sub, string.format, string.lower, string.upper, string.rep
local pairs, next, type = pairs, next, type
local tostring = tostring

-- 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 isDevMode()			return alas_addon and alas_addon.dev_mode and alas_addon.dev_mode == true; end
local function isInternal()			return oe_internal and oe_internal == true; end

local function print(str)			alas_addon:Print(str); end
local function printf(str,...)		alas_addon:Print(strformat(str,...)); end

local function t_print(str)			if isInternal() then alas_addon:Print(str) end end
local function t_printf(str,...)	if isInternal() then alas_addon:Print(strformat(str, ...)) end end
local function dev_print(str)		if isDevMode() then alas_addon:Print(str) end end
local function dev_printf(str, ...)	if isDevMode() then alas_addon:Print(strformat(str,...)) end end
local function d_print(str)			if isInternal() or isDevMode() then alas_addon:Print(str) end end
local function d_printf(str,...)	if isInternal() or isDevMode() then alas_addon:Print(strformat(str,...)) end end

local function pad(s, num)
	if not s or s == "" then
		s = "---"
	end
	s = tostring(s);
	if not num or type(num) ~= "number" or num < 1 or num < strlen(s) then num = strlen(s); end
	
	return s .. spad(" ", num - strlen(s));
end
local function max(val, val2)
	if val > val2 then return val;
	else return val2; end
end

-- 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 print(str) alas_addon:Print(str); end
local function printf(str,...) alas_addon:Print(strformat(str,...)); end


-- builds a table containing all the zones that we will be searching
function AtlasLootAdvancedSearch.DebugProxy:DumpZoneTable()
	local idx = 1;
	
	local BabbleZone			= AtlasLoot_GetLocaleLibBabble("LibBabble-Zone-3.0")
	for k, v in pairs(BabbleZone) do
		t_printf("%i: %s   =   %s", idx, k, v);
		idx = idx + 1;
	end
end


function AtlasLootAdvancedSearch.DebugProxy:dump_atlasdata(tableNamme)
	if not tableName or (type(tableName) ~= "string"  and tableName == "") then
		tableName = "MountsRare1";
	end
	AtlasLoot:LoadModule("all");
	
	local p = { 1, 1, 1, 1, 1, 1, 1, 1 }
	if AtlasLoot_Data and AtlasLoot_Data[tableName] then
        for tableIdx, d in ipairs(AtlasLoot_Data[tableName]) do
--			local pattern = "#z(%d+)#";
--			local zone = strmatch(d[5], pattern);
			t_printf("[1]:%s[2]:%s[3]:%s[4]:%s[5]:%s[6]:%s[7]:%s[8]:%s",
				pad(d[1], p[1]), pad(d[2], p[2]), pad(d[3], p[3]), pad(d[4], p[4]),
				pad(d[5], p[5]), pad(d[6], p[6]), pad(d[7], p[7]), pad(d[8], p[8]));
			
		end
	else
	
		t_printf("Couldn't find a table using the key " .. tostring(tableName));
	end
end

function AtlasLootAdvancedSearch.DebugProxy:show_tables_with8()
	AtlasLoot:LoadModule("all");
	for lootTableID, lootTable in pairs(AtlasLoot_Data) do
		for rowIdx, lootDataRow in ipairs(lootTable) do
			if #lootDataRow > 7 and lootDataRow[8] ~= nil then
				local d, p = lootDataRow, { 1, 1, 1, 1, 1, 1, 1, 1 };
				t_printf("[1]:%s[2]:%s[3]:%s[4]:%s[5]:%s[6]:%s[7]:%s[8]:%s",
					pad(d[1], p[1]), pad(d[2], p[2]), pad(d[3], p[3]), pad(d[4], p[4]),
					pad(d[5], p[5]), pad(d[6], p[6]), pad(d[7], p[7]), pad(d[8], p[8]));
			end
		end
	end
end


function AtlasLootAdvancedSearch.DebugProxy:DumpAtlasRefreshTable( tableIdx )
	-- AtlasLootItemsFrame.refresh is table containing 4 members, which is used to cache various
	-- information about the loot tables currently being displayed in the frame.
	
	-- this table is apparently manipulated by the AtlasData formatting/data retrieval code and can change
	-- or become outdated without notice
	-- the table AtlasLootItemsFrame.refreshOri is  essentially a read-only version of whatever value
	-- AtlasLootItemsFrame.refresh was originally assigned
	if AtlasLootItemsFrame and AtlasLootItemsFrame.refresh then
		t_print("AtlasLootItemsFrame.refresh = {");
		t_printf("    [1] = %s", tostring(AtlasLootItemsFrame.refresh[1]));
		t_printf("    [2] = %s", tostring(AtlasLootItemsFrame.refresh[2]));
		t_printf("    [3] = %s", tostring(AtlasLootItemsFrame.refresh[3]));
		t_printf("    [4] = %s", tostring(AtlasLootItemsFrame.refresh[4]));
		t_print("}");
	else
		t_print("AtlasLootItemsFrame.refresh = nil");
	end
end

function AtlasLootAdvancedSearch.DebugProxy:DumpAtlasRefreshOriTable( tableIdx )
	if AtlasLootItemsFrame and AtlasLootItemsFrame.refresh then
		t_print("AtlasLootItemsFrame.refreshOri = {");
		t_printf("\t[1] = %s", tostring(AtlasLootItemsFrame.refreshOri[1]));
		t_printf("\t[2] = %s", tostring(AtlasLootItemsFrame.refreshOri[2]));
		t_printf("\t[3] = %s", tostring(AtlasLootItemsFrame.refreshOri[3]));
		t_printf("\t[4] = %s", tostring(AtlasLootItemsFrame.refreshOri[4]));
		t_print("}");
	else
		t_print("AtlasLootItemsFrame.refreshOri = nil");
	end
end

function AtlasLootAdvancedSearch.DebugProxy:DumpAtlasRefreshTables()
	self:DumpAtlasRefreshTable();
	self:DumpAtlasRefreshOriTable();
end

function AtlasLootAdvancedSearch.DebugProxy:TraceEnter(SourceObj, FunctionName, ExtraMsg)
	if not isInternal() and not isDevMode() then return end;
	if not self.funcTraceIndent then self.funcTraceIndent = 0; end
	if FunctionName and type(FunctionName) == "string" and SourceObj and type(SourceObj) == "table" then
		local outputStr = spad("-", self.funcTraceIndent) .. ">>" .. FunctionName;
		if ExtraMsg and type(ExtraMsg) == "string" then
			outputStr = outputStr .. ": " .. ExtraMsg;
		end
		AtlasLootAdvancedSearch.Print(SourceObj, outputStr);
		self.funcTraceIndent = self.funcTraceIndent + 2;
	end
end

function AtlasLootAdvancedSearch.DebugProxy:TraceExit(SourceObj, FunctionName, ExtraMsg)
	if not isInternal() and not isDevMode() then return end;
	if not self.funcTraceIndent then self.funcTraceIndent = 0; end
	if FunctionName and type(FunctionName) == "string" then
		self.funcTraceIndent = self.funcTraceIndent - 2;
		
		local outputStr = spad("-", self.funcTraceIndent) .. "<<" .. FunctionName;
		if ExtraMsg and type(ExtraMsg) == "string" then
			outputStr = outputStr .. ": " .. ExtraMsg;
		end
		AtlasLootAdvancedSearch.Print(SourceObj, outputStr);
		if self.funcTraceIndent < 0 then self.funcTraceIndent = 0; end;
	end
end







alas_addon.DebugProxy.DebugOptionsTable = {
	name = ADDON_TAG .. "Debug",
	handler	= alas_addon.DebugProxy,
	type = "group",
	args = {
		dev_mode = {
			order = 500,
			name = "Developer Mode",
			type = "toggle",
			get = function(info)
				return alas_addon.dev_mode;
			end,
			set = function(info, value)
				d_print("Toggling dev mode to " .. tostring(value or false));
				alas_addon.dev_mode = value;
				if alas_addon.dev_mode then
					AtlasLootAdvancedSearch:CleanupZoneNameSearch();
				end
			end,
			hidden = true,
		},
		DumpAtlasData = {
			type	= "input",
			name	= "Dump AtlasLoot_Data",
			get		= false,
			set		= "dump_atlasdata",
			guiHidden = true,
		},
		DumpRefreshTable = {
			type	= "input",
			name	= "Dump AltasLoot.refresh",
			get		= false,
			set		= "DumpAtlasRefreshTable",
			guiHidden = true,
		},
		DumpOriTable = {
			type	= "input",
			name	= "Dump AltasLoot.refreshOri",
			get		= false,
			set		= "DumpAtlasRefreshOriTable",
			guiHidden = true,
		},
		DumpAtlasTables = {
			type	= "execute",
			name	= "Dump both AltasLoot.refresh & AltasLoot.refreshOri",
			func	= "DumpAtlasRefreshTables",
			guiHidden = true,
		},
		dumpzones = {
			order = 501,
			handler = alas_addon,
			name = "Dump Cached Zone Table Tree",
			type = "input",
			set = "ACE_DumpZoneTableTree",
			get = false,
			hidden = true,
		},
		clearzones = {
			type = "execute",
			name = "Clear Zone Table Tree",
			type = "execute",
			func = function()
				AtlasLootAdvancedSearch:Print("Clearing cached zone tables.");
				AtlasLootAdvancedSearch:CleanupZoneNameSearch();
			end,
		},
	},
}





function AtlasLootAdvancedSearch.DebugProxy:isDevMode()		return self.isEnabled and alas_addon and alas_addon.dev_mode and alas_addon.dev_mode == true end
function AtlasLootAdvancedSearch.DebugProxy:isInternal()	return oe_internal and oe_internal == true; end

function AtlasLootAdvancedSearch.DebugProxy:t_print(str)
	if self.isEnabled and self:isInternal() then 
		alas_addon:Print(spad(" ", self.funcTraceIndent) .. str); 
	end 
end
function AtlasLootAdvancedSearch.DebugProxy:t_printf(str, ...)	
	if self.isEnabled and self:isInternal() then
		alas_addon:Print(spad(" ", self.funcTraceIndent) .. strformat(str, ...)); 
	end
end
function AtlasLootAdvancedSearch.DebugProxy:dev_print(str)		if self.isEnabled and self:isDevMode() then
	alas_addon:Print(spad(" ", self.funcTraceIndent) .. str);
	end
end
function AtlasLootAdvancedSearch.DebugProxy:dev_printf(str, ...)
	if self.isEnabled and self:isDevMode() then
		alas_addon:Print(spad(" ", self.funcTraceIndent) .. strformat(str, ...));
	end
end
function AtlasLootAdvancedSearch.DebugProxy:d_print(str)
	if self.isEnabled and self:isInternal() or self:isDevMode() then
		alas_addon:Print(spad(" ", self.funcTraceIndent) .. str);
	end
end
function AtlasLootAdvancedSearch.DebugProxy:d_printf(str, ...)
	if self.isEnabled and self:isInternal() or self:isDevMode() then
		alas_addon:Print(spad(" ", self.funcTraceIndent) .. strformat(str, ...));
	end
end



