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

local QuestsFu = QuestsFu
local QuestsFu_Announce = QuestsFu:NewModule("Announce", "AceEvent-2.0", "AceConsole-2.0")
if QuestsFu.revision < VERSION then
	QuestsFu.version = "r" .. VERSION
	QuestsFu.revision = VERSION
	QuestsFu.date = ("$Date: 2007-03-27 03:03:52 -0700 (Tue, 27 Mar 2007) $"):match("%d%d%d%d%-%d%d%-%d%d")
end

local L = AceLibrary("AceLocale-2.2"):new("QuestsFu_Announce")
L:RegisterTranslations("enUS", function() return {
	["Announce"] = true,
	["Announce quest status"] = true,
	["Announce completion"] = true,
	["Announce when all a quests' objectives are completed"] = true,
	["Announce objectives"] = true,
	["Announce when a quest objective is advanced"] = true,
	["completed"] = true,
} end)

L:RegisterTranslations("deDE", function() return {
	["Announce"] = "Ankündigen",
	["Announce quest status"] = "Ankündigen des Quest Status.",
	["Announce completion"] = "Ankündigen bei Quest Fertigstellung",
	["Announce when all a quests' objectives are completed"] = "Ankündigen wenn alle Quest-Ziele erfüllt worden sind.",
	["Announce objectives"] = "Akündigen von Quest-Zielen",
	["Announce when a quest objective is advanced"] = "Ankündigen wenn ein Quest-Ziel erfüllt wurde.",
	["completed"] = "Vollendet",
} end)

L:RegisterTranslations("koKR", function() return {
	["Announce"] = "알림",
	["Announce quest status"] = "퀘스트 상세 알림을 사용합니다.",
	["Announce completion"] = "완료 알림",
	["Announce when all a quests' objectives are completed"] = "퀘스트를 완료하면 알림니다.",
	["Announce objectives"] = "목표 알림",
	["Announce when a quest objective is advanced"] = "퀘스트 목표를 알림니다.",
	["completed"] = "완료",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Announce"] = "任務通告",
	["Announce quest status"] = "通告任務狀態",
	["Announce completion"] = "通告完成",
	["Announce when all a quests' objectives are completed"] = "當所有任務目標皆完成時通告",
	["Announce objectives"] = "通告進度",
	["Announce when a quest objective is advanced"] = "當有任務目標正在進行時通告",
	["completed"] = "已完成",
} end)

function QuestsFu_Announce:OnInitialize()
	self.db = QuestsFu:AcquireDBNamespace("Announce")
	QuestsFu:RegisterDefaults("Announce", "profile", {
		["objective"] = false,
		["complete"] = false,
	})
	QuestsFu.options.args.Announce = {
		name = L["Announce"],
		desc = L["Announce quest status"],
		type = "group",
		args = {
			complete = {
				type = 'toggle', name = L["Announce completion"],
				desc = L["Announce when all a quests' objectives are completed"],
				get = function() return self.db.profile.complete end,
				set = function(t) self.db.profile.complete = t end,
			},
			objective = {
				type = 'toggle', name = L["Announce objectives"],
				desc = L["Announce when a quest objective is advanced"],
				get = function() return self.db.profile.objective end,
				set = function(t) self.db.profile.objective = t end,
			},
			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_Announce:OnEnable()
	self:RegisterEvent("Quixote_Leaderboard_Update")
	self:RegisterEvent("Quixote_Quest_Complete")
end

function QuestsFu_Announce:Quixote_Leaderboard_Update(name, id, lid, desc, numHad, numGot, numNeeded)
	if self.db.profile.objective and GetNumPartyMembers() > 0 then
		SendChatMessage(("%s - %s: %d/%d"):format(name, desc, numGot, numNeeded), "PARTY")
	end
end

function QuestsFu_Announce:Quixote_Quest_Complete(name, id)
	if self.db.profile.complete and GetNumPartyMembers() > 0 then
		SendChatMessage(("%s %s"):format(name, L["completed"]), "PARTY")
	end
end