local VERSION = tonumber(("$Revision: 31377 $"):match("%d+"))

local QuestsFu = QuestsFu
local QuestsFu_Tooltips = QuestsFu:NewModule("Tooltips", "AceEvent-2.0", "AceHook-2.1", "AceConsole-2.0")
if QuestsFu.revision < VERSION then
	QuestsFu.version = "r" .. VERSION
	QuestsFu.revision = VERSION
	QuestsFu.date = ("$Date: 2007-03-30 07:58:47 -0400 (Fri, 30 Mar 2007) $"):match("%d%d%d%d%-%d%d%-%d%d")
end

local quixote = AceLibrary("Quixote-1.0")
local crayon = AceLibrary("Crayon-2.0")

local L = AceLibrary("AceLocale-2.2"):new("QuestsFu_Tooltips")
L:RegisterTranslations("enUS", function() return {
	["Tooltips"] = true,
	["Show quest objectives in item/mob tooltips"] = true,
} end)

L:RegisterTranslations("deDE", function() return {
	["Tooltips"] = "Tooltips",
	["Show quest objectives in item/mob tooltips"] = "Zeige Quest-Ziele im Item/Mob Tooltip.",
} end)

L:RegisterTranslations("koKR", function() return {
	["Tooltips"] = "툴팁",
	["Show quest objectives in item/mob tooltips"] = "툴팁에 퀘스트 몹/아이템을 표시합니다.",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Tooltips"] = "提示框",
	["Show quest objectives in item/mob tooltips"] = "在物品/怪物資訊提示框中顯示任務目標",
} end)

function QuestsFu_Tooltips:OnInitialize()
	self.db = QuestsFu:AcquireDBNamespace("Tooltips")
	QuestsFu:RegisterDefaults("Tooltips", "profile", {
	})
	QuestsFu.options.args.Tooltips = {
		name = L["Tooltips"],
		desc = L["Show quest objectives in item/mob tooltips"],
		type = "group",
		args = {
			toggle = {
				name = AceLibrary("AceLocale-2.2"):new("QuestsFu")["Enabled"],
				desc = AceLibrary("AceLocale-2.2"):new("QuestsFu")["Suspend/resume this module."],
				type = 'toggle',
				order = -1,
				get = function() return QuestsFu:IsModuleActive(self) end,
				set = function() QuestsFu:ToggleModuleActive(self) end
			},
		},
	}
end

function QuestsFu_Tooltips:OnEnable()
	self:HookScript(GameTooltip, "OnTooltipSetItem")
	self:RegisterEvent("UPDATE_MOUSEOVER_UNIT", "OnMouseOverUnit")
end

function QuestsFu_Tooltips:OnMouseOverUnit()
	if quixote:IsQuestMob(UnitName('mouseover')) then
		for _,q,i,n in quixote:IterateQuestsForMob(UnitName('mouseover')) do
			GameTooltip:AddDoubleLine(q, i..'/'..n, 1, 1, 1, crayon:GetThresholdColor(i/n))
		end
		GameTooltip:Show()
	end
end

function QuestsFu_Tooltips:OnTooltipSetItem(tooltip, ...)
	local t = tooltip:GetItem()
	if t then
		local q,i,n = quixote:IsQuestItem(t)
		if q then
			tooltip:AddDoubleLine(q, i..'/'..n, 1, 1, 1, crayon:GetThresholdColor(i/n))
			tooltip:Show()
		end
	end
	return self.hooks[tooltip].OnTooltipSetItem(tooltip, ...)
end
