--[[
	Auctioneer Advanced - Search UI - Searcher General
	Version: 5.0.0 (BillyGoat)
	Revision: $Id: SearcherGeneral.lua 3277 2008-07-28 12:04:47Z Norganna $
	URL: http://auctioneeraddon.com/

	This is a plugin module for the SearchUI that assists in searching by refined paramaters

	License:
		This program 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 2
		of the License, or (at your option) any later version.

		This program is distributed in the hope that it will be useful,
		but 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 this program(see GPL.txt); if not, write to the Free Software
		Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

	Note:
		This AddOn's source code is specifically designed to work with
		World of Warcraft's interpreted AddOn system.
		You have an implicit licence to use this AddOn with these facilities
		since that is its designated purpose as per:
		http://www.fsf.org/licensing/licenses/gpl-faq.html#InterpreterIncompat
--]]
-- Create a new instance of our lib with our parent
local lib, parent, private = AucSearchUI.NewSearcher("General")
if not lib then return end
local print,decode,_,_,replicate,empty,_,_,_,debugPrint,fill = AucAdvanced.GetModuleLocals()
local get,set,default,Const = AucSearchUI.GetSearchLocals()
lib.tabname = "General parameters"

function private.getTypes()
	if not private.typetable then
		private.typetable = {GetAuctionItemClasses()}
		table.insert(private.typetable,1, "All")
	end
	return private.typetable
end

function private.getSubTypes()
	local itemtype = get("general.type")
	for i, j in pairs(private.typetable) do
		if j == itemtype then
			itemtype = i
			break
		end
	end
	if private.typetable == "All" then
		return {[1]="All"}
	end
	local subtypetable = {GetAuctionItemSubClasses(itemtype-1)}--itemtype - 1 because 1 is the "All" category
	table.insert(subtypetable, 1, "All")
	return subtypetable
end

function private.getTimeLeft()
	return {
			{0, "Any"},
			{1, "less than 30 min"},
			{2, "2 hours"},
			{3, "12 hours"},
			{4, "48 hours"},
		}
end

-- Set our defaults
default("general.name", "")
default("general.name.exact", false)
default("general.name.regexp", false)
default("general.name.invert", false)
default("general.type", "All")
default("general.subtype", "All")
default("general.timeleft", 0)
default("general.ilevel.min", 0)
default("general.ilevel.max", 150)
default("general.ulevel.min", 0)
default("general.ulevel.max", 80)
default("general.seller", "")
default("general.seller.exact", false)
default("general.seller.regexp", false)
default("general.seller.invert", false)
default("general.minbid", 0)
default("general.minbuy", 0)
default("general.maxbid", 999999999)
default("general.maxbuy", 999999999)

-- This function is automatically called when we need to create our search parameters
function lib:MakeGuiConfig(gui)
	-- Get our tab and populate it with our controls
	id = gui:AddTab(lib.tabname, "Searches")

	gui:MakeScrollable(id)
	gui:AddControl(id, "Header",     0,      "General search criteria")

	local last = gui:GetLast(id)
	gui:SetControlWidth(0.35)
	gui:AddControl(id, "Text",       0,   1, "general.name", "Item name")
	local cont = gui:GetLast(id)
	gui:SetLast(id, last)
	gui:AddControl(id, "Checkbox",   0.11, 0, "general.name.exact", "Exact")
	gui:SetLast(id, last)
	gui:AddControl(id, "Checkbox",   0.21, 0, "general.name.regexp", "Regexp")
	gui:SetLast(id, last)
	gui:AddControl(id, "Checkbox",   0.35, 0, "general.name.invert", "Invert")
	
	gui:SetLast(id, cont)
	last = cont
	
	gui:AddControl(id, "Note",       0.0, 1, 100, 14, "Type:")
	gui:AddControl(id, "Selectbox",   0.0, 1, private.getTypes, "general.type", "ItemType")
	gui:SetLast(id, last)
	gui:AddControl(id, "Note",       0.3, 1, 100, 14, "SubType:")
	gui:AddControl(id, "Selectbox",   0.3, 1, private.getSubTypes, "general.subtype", "ItemSubType")
	gui:SetLast(id, last)
	gui:AddControl(id, "Note",       0.7, 1, 100, 14, "TimeLeft:")
	gui:AddControl(id, "Selectbox",  0.7, 1, private.getTimeLeft(), "general.timeleft", "TimeLeft")
	
	last = gui:GetLast(id)
	gui:SetControlWidth(0.37)
	gui:AddControl(id, "NumeriSlider",     0,   1, "general.ilevel.min", 0, 200, 1, "Min item level")
	gui:SetControlWidth(0.37)
	gui:AddControl(id, "NumeriSlider",     0,   1, "general.ilevel.max", 0, 200, 1, "Max item level")
	cont = gui:GetLast(id)

	gui:SetLast(id, last)
	gui:SetControlWidth(0.17)
	gui:AddControl(id, "NumeriSlider",     0.6, 0, "general.ulevel.min", 0, 80, 1, "Min user level")
	gui:SetControlWidth(0.17)
	gui:AddControl(id, "NumeriSlider",     0.6, 0, "general.ulevel.max", 0, 80, 1, "Max user level")

	gui:SetLast(id, cont)
	
	last = gui:GetLast(id)
	gui:SetControlWidth(0.35)
	gui:AddControl(id, "Text",       0,   1, "general.seller", "Seller name")
	cont = gui:GetLast(id)
	gui:SetLast(id, last)
	gui:AddControl(id, "Checkbox",   0.13, 0, "general.seller.exact", "Exact")
	gui:SetLast(id, last)
	gui:AddControl(id, "Checkbox",   0.23, 0, "general.seller.regexp", "Regexp")
	gui:SetLast(id, last)
	gui:AddControl(id, "Checkbox",   0.37, 0, "general.seller.invert", "Invert")
	
	gui:SetLast(id, cont)
	gui:AddControl(id, "MoneyFramePinned", 0, 1, "general.minbid", 0, 999999999, "Minimum Bid")
	gui:SetLast(id, cont)
	gui:AddControl(id, "MoneyFramePinned", 0.5, 1, "general.minbuy", 0, 999999999, "Minimum Buyout")
	last = gui:GetLast(id)
	gui:AddControl(id, "MoneyFramePinned", 0, 1, "general.maxbid", 0, 999999999, "Maximum Bid")
	gui:SetLast(id, last)
	gui:AddControl(id, "MoneyFramePinned", 0.5, 1, "general.maxbuy", 0, 999999999, "Maximum Buyout")
end

function lib.Search(item)
	private.debug = ""
	if private.NameSearch("name", item[Const.NAME])
			and private.TypeSearch(item[Const.ITYPE], item[Const.ISUB])
			and private.TimeSearch(item[Const.TLEFT])
			and private.LevelSearch("ilevel", item[Const.ILEVEL])
			and private.LevelSearch("ulevel", item[Const.ULEVEL])
			and private.NameSearch("seller", item[Const.SELLER])
			and private.PriceSearch("Bid", item[Const.PRICE])
			and private.PriceSearch("Buy", item[Const.BUYOUT]) then
		return true
	else
		return false, private.debug
	end
end

function private.LevelSearch(levelType, itemLevel)
	local min = get("general."..levelType..".min")
	local max = get("general."..levelType..".max")

	if itemLevel < min then
		private.debug = levelType.." too low"
		return false
	end
	if itemLevel > max then
		private.debug = levelType.." too high"
		return false
	end
	return true
end

function private.NameSearch(nametype,itemName)
	local name = get("general."..nametype)

	-- If there's no name, then this matches
	if not name or name == "" then
		return true
	end

	-- Lowercase the input
	name = name:lower()
	itemName = itemName:lower()

	-- Get the matching options
	local nameExact = get("general."..nametype..".exact")
	local nameRegexp = get("general."..nametype..".regexp")
	local nameInvert = get("general."..nametype..".invert")

	-- if we need to make a non-regexp, exact match:
	if nameExact and not nameRegexp then
		-- If the name matches or we are inverted
		if name == itemName and not nameInvert then
			return true
		elseif name ~= itemName and nameInvert then
			return true
		end
		private.debug = nametype.." is not exact match"
		return false
	end

	local plain, text
	text = name
	if not nameRegexp then
		plain = 1
	elseif nameExact then
		text = "^"..name.."$"
	end

	local matches = itemName:find(text, 1, plain)
	if matches and not nameInvert then
		return true
	elseif not matches and nameInvert then
		return true
	end
	private.debug = nametype.." does not match critia"
	return false
end

function private.TypeSearch(itype, isubtype)
	local searchtype = get("general.type")
	if searchtype == "All" then
		return true
	elseif searchtype == itype then
		local searchsubtype = get("general.subtype")
		if searchsubtype == "All" then
			return true
		elseif searchsubtype == isubtype then
			return true
		else
			private.debug = "Wrong Subtype"
			return false
		end
	else
		private.debug = "Wrong Type"
		return false
	end
end

function private.TimeSearch(iTleft)
	local tleft = get("general.timeleft")
	if tleft == 0 then
		return true
	elseif tleft == iTleft then
		return true
	else
		private.debug = "Time left wrong"
		return false
	end
end

function private.PriceSearch(buybid, price)
	local minprice, maxprice
	if buybid == "Bid" then
		minprice = get("general.minbid")
		maxprice = get("general.maxbid")
	else
		minprice = get("general.minbuy")
		maxprice = get("general.maxbuy")
	end
	if (price <= maxprice) and (price >= minprice) then
		return true
	elseif price < minprice then
		private.debug = buybid.." price too low"
	else
		private.debug = buybid.." price too high"
	end
	return false
end
AucAdvanced.RegisterRevision("$URL: http://svn.norganna.org/auctioneer/branches/RC_5.0/Auc-Util-SearchUI/SearcherGeneral.lua $", "$Rev: 3277 $")
