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

ZoneSearchModule:
	Search module for AtlasLoot_AdvancedSearch - matches against zone names.

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

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

-- 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_ZoneNames(lootTableID, item_data, completingCombinationSearch)
	local isMatch, itemID = false, item_data[2];
	local displayText, zoneText, subHeaderDesc;
	local isPartialMatch, remainingMatchPhrase = false, nil;

	local zoneDB = alas_addon:BuildZoneTable();
	local lootTbl = zoneDB:GetLootTable(lootTableID);
	if lootTbl then
		--@todo, add some error handling here....
		zoneText = lootTbl:GetZoneCaption();
	else
		local encoded_data, extraInfo = AtlasLootAdvancedSearch_GetLootItemMetadata(item_data)
		if encoded_metadata == nil then
			return false;
		end
		local zoneCode = strmatch(encoded_data, "#z%d+#");
		if (not zoneCode) and (extraInfo ~= nil) then 
			zoneCode = strmatch(extraInfo, "#z%d+#");
		end
		
		if zoneCode then
			zoneText = al_addon:FixText(zoneCode);
		end
	end

	if zoneText then
t_printfSlow("Search_ZoneNames lootTableID:%s   zoneText:%s", tostring(lootTableID), tostring(zoneText));
		--@TODO: re-implement support for partial matches
		--[[ version 1; deprecated but left here for reference		
		for tokenIdx = 1, #searchParms.SearchTokens do
			local searchString = searchParms.SearchTokens[tokenIdx];
			if partial then
				local pos = strfind(strlower(zoneText), searchString);
				if pos and pos > 0 then isMatch = true
				else isMatch = false; end
			else
				isMatch = (strlower(zoneText) == searchString);
			end
			if isMatch then
				local normalName, formattedItemName, desc = AtlasLootAdvancedSearch_GetFormattedName(item_data);
				displayText = formattedItemName or zoneText;
				subHeaderDesc = desc;
				break;
			end
		--]]
		--[[ version 2
		local parser = searchParms.SearchParser;
		if parser and parser:IsMatch(zoneText) then
			local normalName, formattedItemName, desc = AtlasLootAdvancedSearch_GetFormattedName(item_data);
			displayText = formattedItemName or zoneText;
			subHeaderDesc = desc;
			isMatch = true;
		else
			displayText = zoneText;
		end
		--]]
		
		local parser = searchParms.SearchParser;
		if parser then
			isMatch, isPartialMatch, remainingMatchPhrase = parser:IsMatch(zoneText,completingCombinationSearch ~= true,searchParms.pendingMatches);
			if isMatch or isPartialMatch then
				local normalName, formattedItemName, desc = AtlasLootAdvancedSearch_GetFormattedName(item_data);
				displayText = formattedItemName or zoneText;
				subHeaderDesc = desc;
			else
				displayText = zoneText;
			end
		end
	end

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


do
	if not _G["AL_SEARCHMODULES"] then _G["AL_SEARCHMODULES"] = {} end
	if not _G["AL_SEARCHMODULES"]["ALSEARCH_ZONENAME"] then
		_G["AL_SEARCHMODULES"]["ALSEARCH_ZONENAME"] = {
			ModuleName="ZoneNames",
			LocKey=L["OPT_ZONES_NAME"],
			LocValue=L["OPT_ZONES_DESC"],
			Func=alas_addon.Search_ZoneNames,
--			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 = false,
	
			-- for now, until I figure out what's wrong, zone searches are disabled.
			banned = true,
		}
	end
end



