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

local QuestsFu = QuestsFu
local QuestsFu_Givers = QuestsFu:NewModule("Givers", "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 L = AceLibrary("AceLocale-2.2"):new("QuestsFu_Givers")
L:RegisterTranslations("enUS", function() return {
	["Givers"] = true,
	["Remember Questgiver"] = true,
	["Record who initiated a quest."] = true,
	["%s in %s (%d,%d)"] = true,
	["Item or other player"] = true,
} end)

L:RegisterTranslations("deDE", function() return {
	["Givers"] = "Questgeber",
	["Remember Questgiver"] = "Merke Questgeber",
	["Record who initiated a quest."] = "Aufzeichnen, bei wem eine Quest begann.",
	["%s in %s (%d,%d)"] = "%s in %s (%d,%d)",
	["Item or other player"] = "Gegenstand oder anderer Spieler",
} end)

L:RegisterTranslations("koKR", function() return {
	["Givers"] = "제공자",
	["Remember Questgiver"] = "퀘스트 제공자 기억",
	["Record who initiated a quest."] = "퀘스트 제공자를 기록합니다.",
	["%s in %s (%d,%d)"] = "%s (%s) (%d,%d)",
	["Item or other player"] = "아이템 또는 다른 플레이어",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Givers"] = "託付者",
	["Remember Questgiver"] = "紀錄託付者",
	["Record who initiated a quest."] = "紀錄是誰給了任務",
--	["%s in %s (%d,%d)"] = true,
	["Item or other player"] = "物品或其他玩家",
} end)

function QuestsFu_Givers:OnInitialize()
	self.db = QuestsFu:AcquireDBNamespace("Givers")
	QuestsFu:RegisterDefaults("Givers", "char", {
		questgivers = {},
	})
	QuestsFu.options.args.Givers = {
		name = L["Remember Questgiver"],
		desc = L["Record who initiated a quest."],
		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_Givers:OnEnable()
	if quixote.firstScanDone then
		self:Hook("AcceptQuest", true)
		
		--Check various bits of saved info for continuing validity
		-- Make sure that we haven't lost a quest whose questgiver we're remembering.
		for name,text in pairs(self.db.char.questgivers) do
			if not quixote:GetQuest(name) then
				self.db.char.questgivers[name] = nil
			end
		end
	else
		self:RegisterEvent("Quixote_Ready", function() self:OnEnable() end, true)
	end
end

function QuestsFu_Givers:AcceptQuest()
	if (not UnitName("npc")) or UnitIsPlayer("npc") then
		self.db.char.questgivers[GetTitleText()] = L["Item or other player"]
	else
		local x,y = GetPlayerMapPosition("player")
		self.db.char.questgivers[GetTitleText()] = L["%s in %s (%d,%d)"]:format(UnitName('npc'), GetZoneText(), floor(x*100), floor(y*100))
	end
	return self.hooks["AcceptQuest"]()
end

function QuestsFu_Givers:Quest_Lost(name, id)
	self.db.char.questgivers[name] = nil
end
