--[[
	Utility.lua
		bgn_atBank is a global completely controled by Banknon
--]]

--[[
	Boolean functions
--]]

function IsAddOnEnabled(addon)
	local _, _, _, enabled = GetAddOnInfo(addon);
	return enabled;
end

function Bagnon_IsInventoryBag(bagID)
	return (bagID >= 0 and bagID < 5);
end

function Bagnon_IsBankBag(bagID)
	return ( bagID == -1 or (bagID > 4 and bagID < 11) );
end

function Bagnon_FrameHasBag(frame, bagID)
	if(not frame) then
		return false;
	end
	
	local frameName = frame:GetName();
	for i in BagnonSets[frameName].bags do
		if(BagnonSets[frameName].bags[i] == bagID) then
			return true;
		end
	end
	return false;
end

--why pass the frame? used for an override later
function Bagnon_IsAmmoBag(bagID)
	--bank and the main bag are never ammo bags
	if(bagID == 0 or bagID == -1 or bgn_atBank) then
		return nil;
	end
	
	local link = GetInventoryItemLink("player", ContainerIDToInventoryID(bagID) );
	
	if(link) then
		local _, _, id = string.find(link, "item:(%d+)");
		local _, _, _, _, _, subType = GetItemInfo(id);

		return (subType ~= INVTYPE_BAG and string ~= MISCELLANEOUS);
	end
	
	return nil;
end

--[[
	Detection for Cached Frames
--]]

function Bagnon_IsCachedFrame(frame) 
	if(not BagnonForever) then
		return false;
	end
	return ( UnitName("player") ~= frame.player or (not bgn_atBank and frame:GetName() == "Banknon") );
end

function Bagnon_IsCachedBag(player, bagID) 
	if(not BagnonForever) then
		return false;
	end
	
	return ( UnitName("player") ~= player or (not bgn_atBank and Bagnon_IsBankBag(bagID)) );
end

--returns true if the item is being viewed from a cache 
function Bagnon_IsCachedItem(item)
	if(not BagnonForever) then
		return false;
	end
	--we're not looking at the current player's items
	if( UnitName("player") ~= item:GetParent().player ) then
		return true;
	end
	
	--the bank frame is visible in the normal way
	if( bgn_atBank ) then
		return false;
	end
	
	--we're looking at the player's bank away from the actual bank
	return ( Bagnon_IsBankBag(item.bag) );
end

function Bagnon_AnchorTooltip(frame)
	GameTooltip:ClearAllPoints();
	if( frame:GetLeft() < ( UIParent:GetRight() / 2) ) then
		if(frame:GetTop() < (UIParent:GetTop() / 2) ) then
			GameTooltip:SetPoint("BOTTOMLEFT", frame, "TOPLEFT");
		else
			GameTooltip:SetPoint("TOPLEFT", frame, "BOTTOMRIGHT");
		end
	else
		if(frame:GetTop() < (UIParent:GetTop() / 2) ) then
			GameTooltip:SetPoint("BOTTOMRIGHT", frame, "TOPRIGHT");
		else
			GameTooltip:SetPoint("TOPRIGHT", frame, "BOTTOMLEFT");
		end
	end
end

--[[
	Messaging
--]]

--send a message to the player
function BagnonMsg(msg)
	DEFAULT_CHAT_FRAME:AddMessage(msg, 0, 0.7, 1);
end