local MAJOR_VERSION, MINOR_VERSION = "LibItemTooltip-2.0", 4
local lib = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
if not lib then return end

local hookableTooltips = {
	"^GameTooltip$",
	"^ItemRefTooltip$",
	"^ShoppingTooltip(%d+)$",
	"^LinksTooltip(%d+)$",
	"^LinkWranglerCompare(%d+)$",
	"^ToolTipTabs(%d+)$",
	"^LH_TOOLTIP_(%d+)$",
}

lib.handlers = lib.handlers or {}
lib.tooltips = lib.tooltips or {}

local _G = getfenv(0)
local pairs = _G.pairs
local select = _G.select
local tonumber = _G.tonumber
local GetItemInfo = _G.GetItemInfo
local EnumerateFrames = _G.EnumerateFrames
local name, rank, link, id

function lib:Register(method, handler)
	if type(method) == "string" then
		method = _G[method]
	end
	if type(method) == "function" then
		lib.handlers[method] = true
	elseif type(method) == "table" and type(method[handler]) == "function" then
		lib.handlers[method] = handler
	end
end

function lib:Unregister(method)
	if type(method) == "string" then
		method = _G[method]
	end
	lib.handlers[method] = nil
end

function lib:OnTooltip(tooltip, name, link, id)
	for method, handler in pairs(lib.handlers) do
		if handler == true then
			method(tooltip, name, link, id)
		elseif method ~= BlueItemInfo3 then
			method[handler](method, tooltip, name, link, id)
		end
	end
	if BlueItemInfo3 and lib.handlers[BlueItemInfo3] then
		BlueItemInfo3[lib.handlers[BlueItemInfo3]](BlueItemInfo3, tooltip, name, link, id)
	end
	if not(Blizzard_CombatLog_Filters and Blizzard_CombatLog_Filters.noTooltipID) then
		if id > 0 then
			if GetItemInfo(id) then
				tooltip:AddDoubleLine("아이템ID: "..id, "아이템LV: "..select(4, GetItemInfo(id)))
			else
				tooltip:AddLine("아이템ID: "..id)
			end
		elseif id < 0 then
			tooltip:AddLine("주문ID: "..-id)
		end
	end
	tooltip:Show()
end

local function onTooltipSetItem(tooltip)
	local name, link = tooltip:GetItem()
	if link and link:find("item:(%d+)") then
		lib:OnTooltip(tooltip, name, link, tonumber(link:match("item:(%d+)")))
	end
end

local function onTooltipSetSpell(tooltip)
	local name, rank, id = tooltip:GetSpell()
	if name then
		lib:OnTooltip(tooltip, name..(rank == "" and rank or ("("..rank..")")), GetSpellLink(id), -id)
	end
end

local function tooltipCreated(obj, name)
	if obj == "GameTooltip" and type(name) == "string" and not lib.tooltips[name] then
		for _, v in pairs(hookableTooltips) do
			if name:find(v) then
				lib.tooltips[name] = true
				name = _G[name]
				--name:HookScript("OnTooltipSetItem", onTooltipSetItem)
				hooksecurefunc(name, "SetAction", onTooltipSetItem)
				hooksecurefunc(name, "SetAuctionItem", onTooltipSetItem)
				hooksecurefunc(name, "SetAuctionSellItem", onTooltipSetItem)
				hooksecurefunc(name, "SetBackpackToken", onTooltipSetItem)
				hooksecurefunc(name, "SetBagItem", onTooltipSetItem)
				hooksecurefunc(name, "SetBuybackItem", onTooltipSetItem)
				hooksecurefunc(name, "SetCurrencyToken", onTooltipSetItem)
				hooksecurefunc(name, "SetGuildBankItem", onTooltipSetItem)
				hooksecurefunc(name, "SetHyperlink", onTooltipSetItem)
				hooksecurefunc(name, "SetHyperlinkCompareItem", onTooltipSetItem)
				hooksecurefunc(name, "SetInboxItem", onTooltipSetItem)
				hooksecurefunc(name, "SetInventoryItem", onTooltipSetItem)
				hooksecurefunc(name, "SetLootItem", onTooltipSetItem)
				hooksecurefunc(name, "SetLootRollItem", onTooltipSetItem)
				hooksecurefunc(name, "SetMerchantItem", onTooltipSetItem)
				hooksecurefunc(name, "SetMerchantCostItem", onTooltipSetItem)
				hooksecurefunc(name, "SetQuestItem", onTooltipSetItem)
				hooksecurefunc(name, "SetQuestLogItem", onTooltipSetItem)
				hooksecurefunc(name, "SetSendMailItem", onTooltipSetItem)
				hooksecurefunc(name, "SetTradePlayerItem", onTooltipSetItem)
				hooksecurefunc(name, "SetTradeSkillItem", onTooltipSetItem)
				hooksecurefunc(name, "SetTradeTargetItem", onTooltipSetItem)
				name:HookScript("OnTooltipSetSpell", onTooltipSetSpell)
				return
			end
		end
	end
end

local frame = EnumerateFrames()
while frame do
	tooltipCreated(frame:GetObjectType(), frame:GetName())
	frame = EnumerateFrames(frame)
end
hooksecurefunc("CreateFrame", tooltipCreated)