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

local QuestsFu = QuestsFu
local QuestsFu_PartyNotify = QuestsFu:NewModule("PartyNotify", "AceEvent-2.0", "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 L = AceLibrary("AceLocale-2.2"):new("QuestsFu_PartyNotify")
L:RegisterTranslations("enUS", function() return {
	["Party notifications"] = true,
	["Notify when party members advance their quests"] = true,
	["Messageframe"] = true,
	["Show in the messageframe (like normal quest notifications)"] = true,
	["Chat"] = true,
	["Show in the chatframe"] = true,
	["completed"] = true,
} end)

L:RegisterTranslations("deDE", function() return {
	["Party notifications"] = "Gruppen Benachrichtigung",
	["Notify when party members advance their quests"] = "Benachrichtige wenn Spieler aus der Gruppe, Ziele ihrer Quests erfüllen.",
	["Messageframe"] = "Nachrichtenfenster",
	["Show in the messageframe (like normal quest notifications)"] = "Zeige im Nachrichtenfenster (wie normale Quest Benachrichtigungen).",
	["Chat"] = "Chat",
	["Show in the chatframe"] = "Zeige im Chatfenster",
	["completed"] = "Vollendet",
} end)

L:RegisterTranslations("koKR", function() return {
	["Party notifications"] = "파티 알림이",
	["Notify when party members advance their quests"] = "파티원들에게 퀘스트 진행 상황을 알림니다.",
	["Messageframe"] = "메시지창",
	["Show in the messageframe (like normal quest notifications)"] = "퀘스트 알림을 메시지창에 표시합니다.",
	["Chat"] = "대화창",
	["Show in the chatframe"] = "퀘스트 알림을 대화창에 표시합니다.",
	["completed"] = "완료",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Party notifications"] = "小隊通知",
	["Notify when party members advance their quests"] = "小隊成員有任務進度時通知",
	["Messageframe"] = "訊息框",
	["Show in the messageframe (like normal quest notifications)"] = "顯示在訊息框中",
	["Chat"] = "聊天框",
	["Show in the chatframe"] = "顯示在聊天框架中",
	["completed"] = "已完成",
} end)

function QuestsFu_PartyNotify:OnInitialize()
	self.db = QuestsFu:AcquireDBNamespace("PartyNotify")
	QuestsFu:RegisterDefaults("PartyNotify", "profile", {
		message = true,
		chat = false,
	})
	QuestsFu.options.args.PartyNotify = {
		name = L["Party notifications"],
		desc = L["Notify when party members advance their quests"],
		type = "group",
		args = {
			message = {
				type = "toggle", name = L["Messageframe"],
				desc = L["Show in the messageframe (like normal quest notifications)"],
				get = function() return self.db.profile.message end,
				set = function(t) self.db.profile.message = t end,
			},
			chat = {
				type = "toggle", name = L["Chat"],
				desc = L["Show in the chatframe"],
				get = function() return self.db.profile.chat end,
				set = function(t) self.db.profile.chat = 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_PartyNotify:OnEnable()
	self:RegisterEvent("Quixote_Party_Leaderboard_Update")
end

function QuestsFu_PartyNotify:Quixote_Party_Leaderboard_Update(sender, title, description, numHad, numGot, numNeeded)
	local message
	if numGot == numNeeded then
		message = ("(%s:%s) %s"):format(sender,title,L["completed"])
	else
		message = ("(%s:%s) %s: %d/%d"):format(sender,title,description,numGot,numNeeded)
	end
	if self.db.profile.message then
		UIErrorsFrame:AddMessage(message, 1.0, 1.0, 0.0, 1.0)
	end
	if self.db.profile.chat then
		self:Print(message)
	end
end
