--[[

	Scorpio's AddOns

	By Scorpio of <Sisu> http://sisuguild.fi

]]


-- Local variables
local SAD_Saved_ContainerFrameItemButton_OnEnter = nil;

-- Inventory slot ID's
INV_AMMO = 0;
INV_HEAD = 1;
INV_NECK = 2;
INV_SHOULDER = 3;
INV_SHIRT = 4;
INV_CHEST= 5;
INV_WAIST = 6;
INV_LEGS = 7;
INV_FEET = 8;
INV_WRIST = 9;
INV_HANDS = 10;
INV_FINGER = 11; -- 12 is finger too
INV_TRINKET = 13; -- 14 is trinket too
INV_BACK = 15;
INV_MAINHAND = 16;
INV_OFFHAND = 17;
INV_RANGED = 18;
INV_TABARD = 19;

-- OnLoad
function SAD_CompareTooltip_OnLoad()

	SlashCmdList["SAD_COMPARE_TOOLTIP"] = SAD_CompareTooltip_SlashCommandHandler;
	SLASH_SAD_COMPARE_TOOLTIP1 = "/sadcompare";
	this:RegisterEvent("ADDON_LOADED");

end

-- OnEvent
function SAD_CompareTooltip_OnEvent(event)
	
	if((event == "ADDON_LOADED") and (arg1 == "SAD_CompareTooltip")) then

		SAD_CompareTooltip_Enable(SAD_COMPARE_TOOLTIP);

	end

end

-- Slash command handler
function SAD_CompareTooltip_SlashCommandHandler(msg)

	-- make all lowercase to ease checking
	msg = string.lower(msg);

	if((string.find(msg, 'enable')) or (string.find(msg, 'true'))) then
	
		SAD_CompareTooltip_Enable(1);
		
	elseif((string.find(msg, 'disable')) or (string.find(msg, 'false'))) then
	
		SAD_ComparetTooltip_Enable(0);
	
	else

		local helptext = "/sadcompare enable|true|disable|false";
		
		SAD_Output(helptext);
	
	end

end

-- Enable or Disable Feature
function SAD_CompareTooltip_Enable(toggle)

	if(toggle == 1) then
		
		if(SAD_Saved_ContainerFrameItemButton_OnEnter == nil) then
			SAD_Saved_ContainerFrameItemButton_OnEnter = ContainerFrameItemButton_OnEnter;
			ContainerFrameItemButton_OnEnter = SAD_CompareTooltip_ContainerFrameItemButton_OnEnter;
		end
			
		SAD_Trace("CompareTooltip Enabled");
			
	else
		
		if(SAD_Saved_ContainerFrameItemButton_OnEnter ~= nil) then
			ContainerFrameItemButton_OnEnter = SAD_Saved_ContainerFrameItemButton_OnEnter;
			SAD_Saved_ContainerFrameItemButton_OnEnter = nil;
		end
			
		SAD_Trace("CompareTooltip Disabled");
			
	end

	SAD_COMPARE_TOOLTIP = toggle;
	
end

-- Returns Slot ID Number by Name or -1 if Invalid Slot Name
function SAD_GetSlotIDByName(slotname)

	if(slotname == "Projectile") then
		return INV_AMMO;
	end
	
	if(slotname == INVTYPE_HEAD) then
		return INV_HEAD;
	end
	
	if(slotname == INVTYPE_NECK) then
		return INV_NECK;
	end
	
	if(slotname == INVTYPE_SHOULDER) then
		return INV_SHOULDER;
	end
	
	if(slotname == INVTYPE_BODY) then
		return INV_SHIRT;
	end
	
	if((slotname == INVTYPE_CHEST) or (slotname == INVTYPE_ROBE)) then
		return INV_CHEST;
	end
	
	if(slotname == INVTYPE_WAIST) then
		return INV_WAIST;
	end
	
	if(slotname == INVTYPE_LEGS) then
		return INV_LEGS;
	end
	
	if(slotname == INVTYPE_FEET) then
		return INV_FEET;
	end
	
	if(slotname == INVTYPE_WRIST) then
		return INV_WRIST;
	end
	
	if(slotname == INVTYPE_HAND) then
		return INV_HANDS;
	end
	
	if(slotname == INVTYPE_FINGER) then
		return INV_FINGER;
	end
	
	if(slotname == INVTYPE_TRINKET) then
		return INV_TRINKET;
	end
	
	if(slotname == INVTYPE_CLOAK) then
		return INV_BACK;
	end
	
	if((slotname == INVTYPE_WEAPONMAINHAND) or (slotname == INVTYPE_WEAPON) or (slotname == INVTYPE_2HWEAPON)) then
		return INV_MAINHAND;
	end
	
	if((slotname == INVTYPE_HOLDABLE) or (slotname == INVTYPE_WEAPONOFFHAND)) then
		return INV_OFFHAND;
	end
	
	if((slotname == INVTYPE_RANGED) or (slotname == "Wand") or (slotname == "Gun") or (slotname == "Thrown")) then
		return INV_RANGED;
	end
	
	if(slotname == INVTYPE_TABARD) then
		return INV_TABARD;
	end
	
	return -1;
	
end

-- Helper Function That Actually Shows the Compare Tooltip, Called from All the Places We Want to Expand the Tooltip to
function SAD_ExpandCompareTooltip()

	local slotname, inventoryid;
	
	if(not GameTooltip:IsVisible()) then
	
		ShoppingTooltip1:Hide();
		ShoppingTooltip2:Hide();
		return;
		
	end
	
	for ti = 1, GameTooltip:NumLines(), 1 do
	
		slotname = getglobal("GameTooltipTextLeft"..ti);
		
		if((slotname) and (slotname:IsVisible()) and (slotname:GetText() ~= nil)) then
		
			inventoryid = SAD_GetSlotIDByName(slotname:GetText());

			if(inventoryid ~= -1) then
			
				ShoppingTooltip1:SetOwner(GameTooltip, "ANCHOR_NONE");
				ShoppingTooltip1:SetPoint("TOPRIGHT", "GameTooltip", "TOPLEFT", 0, -10);
				if(ShoppingTooltip1:SetInventoryItem("player", inventoryid)) then
					ShoppingTooltip1:AddLine("Currently Equipped");
					ShoppingTooltip1:Show();
				end
			
				if((inventoryid == INV_FINGER) or (inventoryid == INV_TRINKET)) then
				
					ShoppingTooltip2:SetOwner(ShoppingTooltip1, "ANCHOR_NONE");
					ShoppingTooltip2:SetPoint("TOPRIGHT", "ShoppingTooltip1", "TOPLEFT", 0, 0);
					if(ShoppingTooltip2:SetInventoryItem("player", inventoryid+1)) then
						ShoppingTooltip2:AddLine("Currently Equipped");
						ShoppingTooltip2:Show();
					end
					
				end
				
				return;
				
			end
			
		end
	
	end
		
end

-- Shopping Tooltip for Bag Items
function SAD_CompareTooltip_ContainerFrameItemButton_OnEnter()

	if(SAD_Saved_ContainerFrameItemButton_OnEnter ~= nil) then
		SAD_Saved_ContainerFrameItemButton_OnEnter();
	end

	-- Fixes the issue with empty moneyframe
	SAD_GameTooltip_ClearMoney_Fix();
	SAD_ExpandCompareTooltip();
	-- Return old handler
	SAD_GameTooltip_ClearMoney_Fix();
		
end
