--[[ ---------------------------------------------------------------------------
Created:		7/30/2010 6:17:28 PM
Last Modified:	2011-04-29T08:44:39Z
Author:			orgevo
Version:		v1.3a

Options:	Contains all static options data for the addon.

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 NAMESEARCH_DEBUG			= true;
local ENABLE_LOGGING			= alas_addon.ENABLE_LOGGING and NAMESEARCH_DEBUG;
local ALLOW_SLOW_FUNCS			= alas_addon.ALLOW_SLOW_FUNCS;

-- 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 = string.len, string.match, string.find, string.sub, string.format, string.lower, string.upper
local pairs, next, type = pairs, next, type
local tostring = tostring
local AtlasLootAdvancedSearch_GetLootItemMetadata = AtlasLootAdvancedSearch_GetLootItemMetadata
local AtlasLoot_TableNames

-- 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
local function funcEnterSlow(obj, funcName, formatStr, ...)
	if ALLOW_SLOW_FUNCS then funcEnter(obj, funcName, formatStr, ...); end
end
local function funcExitSlow(obj, funcName, formatStr, ...)
	if ALLOW_SLOW_FUNCS then funcExit(obj, funcName, formatStr, ...); end
end
local function t_printSlow(str)			if alas_addon.ALLOW_SLOW_LOGS then t_print(str)		end end
local function t_printfSlow(str,...)	if alas_addon.ALLOW_SLOW_LOGS then t_printf(str,...)	end end


function AtlasLootAdvancedSearch:Search_ItemNames(lootTableID, item_data, completingCombinationSearch)
--[===[@debug@
funcEnterSlow(alas_addon,"["..tostring(lootTableID).."] Search_ItemNames","[2]:%s,  [4]:%s,  [5]:%s",tostring(item_data[2]),tostring(item_data[4]),tostring(item_data[5]));
--@end-debug@]===]
	
	--[[
		- for items found in dungeons:
				lootTableID will be an encounter key from the AtlasLoot_Data table, such as Naxx80Maexxna25Man
				item_data will be a table containing data for one item that drops in that encounter
				
		- for mounts:
				lootTableID will the key from the AtlasLoot_TableNames array, such as MountsHorde1 (this key also corresponds)
				to a single page of loot as displayed in AtlasLoot
				
				item_data will be a table containing data for an item associated with that loot page
			


	Examples:
		{ 1,                  0, "inv_misc_tournaments_symbol_orc", "=q6="..BabbleFaction["Orgrimmar"].." #j30#",                          "=ec1=#m6#"};
		{ 7,              47517,                                "",         "=q4=Blade of the Unbroken Covenant",                            "=ds=#h3#,    #w10#"};
		{ 2,"EMBLEMOFFROSTMENU",         "inv_misc_frostemblem_01",        "=ds="..AL["Emblem of Frost Rewards"], "=q5="..AL["Wrath of the Lich King"]};
		{ 2,              18796,                                "",           "=q4=Horn of the Swift Brown Wolf",                          "=ds=#e12#"};
		{ 27,                 0,                      "INV_Box_01",                     "=q6="..AL["Bonus Loot"],      "=q5="..AL["Three Drakes Left"]};
		1                 2                                 3                                               4                                      5          6          7
      
		So as you can, the value for the "ID" of the table row can be one of three types:
			- 0 (to indicate a category header),
			- a number WoW 'ItemID' (for actual items data)
			- a string which can be used as the key into the (once again) AltasLoot_Data table
	--]]
	
    local isMatch, itemID, displayText = false, item_data[2];
    local remainingMatchPhrase = nil;
    
	-- item_data[2] is the spell or item ID; if a spell ID, will be prefixed with the letter s
	-- @todo: add exact match option, and if enabled, use only this value
	local itemName, formattedItemName, description = AtlasLootAdvancedSearch_GetFormattedName(item_data);
	if itemName then
		--@TODO: re-implement support for partial matches
		local parser = searchParms.SearchParser;
		if parser then
			local partialMatch;
			isMatch, partialMatch, remainingMatchPhrase = parser:IsMatch(itemName,completingCombinationSearch ~= true,searchParms.pendingMatches);
			if isMatch or partialMatch then
				if strsub(tostring(itemName), 1, 1) == "s" then
					-- spell
					displayText = itemName;
					t_print("spellName:" .. displayText);
				else
					-- item
					displayText = formattedItemName;
				end
			end
		end
	end

--[===[@debug@
funcExitSlow(alas_addon,"["..tostring(lootTableID).."] Search_ItemNames","itemID:%s  displayText:%s   description:%s   isMatch:%s   isPartialMatchOnly:%s",
	tostring(itemID), tostring(displayText), tostring(description), tostring(isMatch), tostring(remainingMatchPhrase ~= nil and #remainingMatchPhrase > 0));
--@end-debug@]===]

	return isMatch, itemID, displayText, description, remainingMatchPhrase;
end







do
	if not _G["AL_SEARCHMODULES"] then _G["AL_SEARCHMODULES"] = {} end
	if not _G["AL_SEARCHMODULES"]["ALSEARCH_ITEMNAME"] then
		_G["AL_SEARCHMODULES"]["ALSEARCH_ITEMNAME"] = {
			ModuleName="ItemNames",
			Priority = 0,
			Func=alas_addon.Search_ItemNames,
			LocKey=L["OPT_ITEMNAMES_NAME"],
			LocValue=L["OPT_ITEMNAMES_DESC"],
			foundMatch=nil,
--			PreSearchFunc = alas_addon.PrepareZoneNameSearch,
--			PostSortFunc = alas_addon.CleanupZoneNameSearch,
			
			-- allow the module to optionally specify whether this search is enabled by default or not.
			-- if not specified, module will be enabled by default.
			defaultValue = true,	
		}
	end
end













