--[[ ---------------------------------------------------------------------------
Created:		8/16/2010 5:12:08 PM
Last Modified:	2011-04-29T08:44:39Z
Author:			orgevo
Version:		v1.3a

WeaponType:	Search module for matching based on the type of weapon or equipment.

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 TYPESEARCH_DEBUG			= false;
local ENABLE_LOGGING			= alas_addon.ENABLE_LOGGING and TYPESEARCH_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 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

-- 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 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_WeaponTypes(lootTableID, item_data, completingCombinationSearch)
	local isMatch, itemID, displayText, description = false, item_data[2];
	local isPartialMatch, remainingMatch = false, nil;
	
--	{ 2,"EMBLEMOFFROSTMENU", "inv_misc_frostemblem_01", "=ds="..AL["Emblem of Frost Rewards"], "=q5="..AL["Wrath of the Lich King"]};
--	  1                   2                          3                                      4                                    5         6
	local encoded_metadata, extraData = AtlasLootAdvancedSearch_GetLootItemMetadata(item_data);
	if encoded_metadata == nil then
		return false;
	end
	local weaponTypeCode = strmatch(encoded_metadata, "#w%d+#");
	if not weaponTypeCode and extraData then
		weaponTypeCode = strmatch(extraData, "#w%d+#");
	end
	if weaponTypeCode then
		local weaponType = al_addon:FixText(weaponTypeCode);
		if weaponType then
			local parser = searchParms.SearchParser;
			if parser then
				isMatch, isPartialMatch, remainingMatch = parser:IsMatch(weaponType, completingCombinationSearch ~= true, searchParms.pendingMatches);
				if isMatch or isPartialMatch then
					local itemName, formattedItemName;
					itemName, formattedItemName, description = AtlasLootAdvancedSearch_GetFormattedName(item_data);
					displayText = formattedItemName;
				end
			end
		end
	end

	return isMatch, item_data[2], displayText, description, remainingMatch;
end



do
	if not _G["AL_SEARCHMODULES"] then _G["AL_SEARCHMODULES"] = {} end
	if not _G["AL_SEARCHMODULES"]["ALSEARCH_WEAPONTYPE"] then
		_G["AL_SEARCHMODULES"]["ALSEARCH_WEAPONTYPE"] = {
			ModuleName="WeaponTypes",
			Priority = 1,
			Func=alas_addon.Search_WeaponTypes,
			LocKey=L["OPT_WEAPONTYPES_NAME"],
			LocValue=L["OPT_WEAPONTYPES_DESC"],
			foundMatch=nil,
			ValidValues = {
				BabbleInventory["Axe"],
				BabbleInventory["Bow"],
				BabbleInventory["Crossbow"],
				BabbleInventory["Dagger"],
				BabbleInventory["Gun"],
				BabbleInventory["Mace"],
				BabbleInventory["Polearm"],
				BabbleInventory["Shield"],
				BabbleInventory["Hands"],
				BabbleInventory["Staff"],
				BabbleInventory["Sword"],
				BabbleInventory["Thrown"],
				BabbleInventory["Wand"],
				BabbleInventory["Fist Weapon"],
			},
			LocChoices=L["OPT_WEAPONTYPES_LIST"],
		}
	end
end


