------------------------------
--      Are you local?      --
------------------------------

local boss = BB["Maexxna"]
local L = AceLibrary("AceLocale-2.2"):new("BigWigs"..boss)

local inCocoon = {}
local started
local enrageannounced

----------------------------
--      Localization      --
----------------------------

L:RegisterTranslations("enUS", function() return {
	cmd = "Maexxna",

	spray = "Web Spray",
	spray_desc = "Warn for webspray and spiders",

	cocoon = "Cocoon",
	cocoon_desc = "Warn for Cocooned players",

	cocoonwarn = "%s Cocooned!",

	enragetrigger = "%s becomes enraged!",

	webspraywarn30sec = "Wall Cocoons in 10 seconds",
	webspraywarn20sec = "Wall Cocoons! 10 seconds until Spiders spawn!",
	webspraywarn10sec = "Spiders Spawn. 10 seconds until Web Spray!",
	webspraywarn5sec = "WEB SPRAY in 5 seconds!",
	webspraywarn = "Web Spray! 40 seconds until next!",
	enragewarn = "Enrage - SQUISH SQUISH SQUISH!",
	enragesoonwarn = "Enrage Soon - Bugsquatters out!",

	webspraybar = "Web Spray",
	cocoonbar = "Cocoons",
	spiderbar = "Spiders",
} end )

L:RegisterTranslations("deDE", function() return {
	spray = "Gespinstschauer",
	spray_desc = "Warnung vor Gespinstschauer und Spinnen.",

	cocoon = "Fangnetz",
	cocoon_desc = "Warnung, wenn Spieler von Fangnetz betroffen sind.",

	cocoonwarn = "%s im Fangnetz!",

	enragetrigger = "%s wird w\195\188tend!",

	webspraywarn30sec = "Fangnetze in 10 Sekunden!",
	webspraywarn20sec = "Fangnetze! Spinnen in 10 Sekunden!",
	webspraywarn10sec = "Spinnen! Gespinstschauer in 10 Sekunden!",
	webspraywarn5sec = "Gespinstschauer in 5 Sekunden!",
	webspraywarn = "Gespinstschauer! N\195\164chster in 40 Sekunden!",
	enragewarn = "Wutanfall!",
	enragesoonwarn = "Wutanfall in K\195\188rze!",

	webspraybar = "Gespinstschauer",
	cocoonbar = "Fangnetze",
	spiderbar = "Spinnen",
} end )

L:RegisterTranslations("koKR", function() return {
	spray = "거미줄 뿌리기",
	spray_desc = "거미줄 뿌리기와 거미 소환을 알립니다.",

	cocoon = "거미줄 감싸기",
	cocoon_desc = "거미줄 감싸기에 걸린 플레이어를 알립니다.",

	cocoonwarn = "%s 거미줄 감싸기!",

	enragetrigger = "%s|1이;가; 분노에 휩싸입니다!",

	webspraywarn30sec = "10초 이내 거미줄 감싸기",
	webspraywarn20sec = "거미줄 감싸기. 10초 후 거미 소환!",
	webspraywarn10sec = "거미 소환. 10초 후 거미줄 뿌리기!",
	webspraywarn5sec = "5초 후 거미줄 뿌리기!",
	webspraywarn = "거미줄 뿌리기! 다음은 40초 후!",
	enragewarn = "격노!!",
	enragesoonwarn = "잠시 후 격노!",

	webspraybar = "거미줄 뿌리기",
	cocoonbar = "거미줄 감싸기",
	spiderbar = "거미 소환",
} end )

L:RegisterTranslations("zhCN", function() return {
	spray = "蛛网喷射",
	spray_desc = "当施放蛛网喷射时发出警报。",

	cocoon = "蛛网裹体",
	cocoon_desc = "当玩家受到蛛网裹体效果时发出警报。",

	cocoonwarn = ">%s< 被蛛网裹体了！",

	enragetrigger = "%s变得愤怒了！",

	webspraywarn30sec = "10秒后，蛛网裹体！",
	webspraywarn20sec = "蛛网裹体！10秒后蜘蛛出现！",
	webspraywarn10sec = "蜘蛛出现！10秒后蛛网喷射！",
	webspraywarn5sec = "蛛网喷射5秒！",
	webspraywarn = "40秒后，蛛网裹体！",
	enragewarn = "激怒！",
	enragesoonwarn = "即将激怒！",

	webspraybar = "<蛛网喷射>",
	cocoonbar = "<蛛网裹体>",
	spiderbar = "<出现 小蜘蛛>",
} end )

L:RegisterTranslations("zhTW", function() return {
	spray = "撒網警報",
	spray_desc = "當梅克絲娜撒網及小蜘蛛出現時發出警報",

	cocoon = "纏繞的蜘蛛網警報",
	cocoon_desc = "玩家受到蜘蛛網纏繞發出警報",

	cocoonwarn = "%s 被蛛網纏繞了！",

	enragetrigger = "變得憤怒了！",

	webspraywarn30sec = "10 秒後發動纏繞的蜘蛛網",
	webspraywarn20sec = "纏繞的蜘蛛網！ 10 秒後小蜘蛛出現！",
	webspraywarn10sec = "小蜘蛛出現！ 10 秒後撒網！",
	webspraywarn5sec = "5 秒後撒網！",
	webspraywarn = "撒網！ 40 秒後再次發動",
	enragewarn = "狂怒！全力攻擊！",
	enragesoonwarn = "即將狂怒！",

	webspraybar = "撒網",
	cocoonbar = "纏繞的蜘蛛網",
	spiderbar = "小蜘蛛",
} end )

L:RegisterTranslations("frFR", function() return {
	spray = "Jet de rets",
	spray_desc = "Préviens de l'arrivée des Jet de rets et des araignées.",

	cocoon = "Entoilage",
	cocoon_desc = "Préviens quand un joueur subit les effets de l'Entoilage.",

	cocoonwarn = "%s entoilés !",

	enragetrigger = "%s devient folle furieuse !",

	webspraywarn30sec = "Entoilage dans 10 sec.",
	webspraywarn20sec = "Entoilage ! 10 sec. avant les araignées !",
	webspraywarn10sec = "Araignées écloses. 10 sec. avant Jet de rets !",
	webspraywarn5sec = "Jet de rets dans 5 sec. !",
	webspraywarn = "Jet de rets ! 40 sec. avant le suivant !",
	enragewarn = "Enragée - SQUISH SQUISH SQUISH !",
	enragesoonwarn = "Enrager imminent - Bugsquatters out !",

	webspraybar = "Jet de rets",
	cocoonbar = "Entoilage",
	spiderbar = "Araignées",
} end )

----------------------------------
--      Module Declaration      --
----------------------------------

local mod = BigWigs:NewModule(boss)
mod.zonename = BZ["Naxxramas"]
mod.enabletrigger = boss
mod.guid = 15952
mod.toggleoptions = {"spray", "cocoon", "enrage", "bosskill"}
mod.revision = tonumber(("$Revision: 4767 $"):sub(12, -3))

------------------------------
--      Initialization      --
------------------------------

function mod:OnEnable()
	self:AddCombatListener("SPELL_AURA_APPLIED", "Cocoon", 28622)
	self:AddCombatListener("SPELL_CAST_SUCCESS", "Spray", 29484)
	self:AddCombatListener("UNIT_DIED", "BossDeath")

	self:RegisterEvent("CHAT_MSG_MONSTER_EMOTE")
	self:RegisterEvent("PLAYER_REGEN_ENABLED", "CheckForWipe")
	self:RegisterEvent("PLAYER_REGEN_DISABLED", "CheckForEngage")
	self:RegisterEvent("UNIT_HEALTH")

	for k in pairs(inCocoon) do inCocoon[k] = nil end
	started = nil
	self:RegisterEvent("BigWigs_RecvSync")
end

------------------------------
--      Event Handlers      --
------------------------------

function mod:Cocoon(player)
	if self.db.profile.cocoon then
		inCocoon[player] = true
		self:ScheduleEvent("Cocoons", self.CocoonWarn, 0.3, self)
	end
end

function mod:Spray(_, spellID)
	if self.db.profile.spray then
		self:IfMessage(L["webspraywarn"], "Important", spellID)
		self:DelayedMessage(10, L["webspraywarn30sec"], "Attention")
		self:DelayedMessage(20, L["webspraywarn20sec"], "Attention")
		self:DelayedMessage(30, L["webspraywarn10sec"], "Attention")
		self:DelayedMessage(35, L["webspraywarn5sec"], "Attention")
		self:Bar(L["webspraybar"], 40, spellID)
	end
	if self.db.profile.cocoon then
		self:Bar(L["cocoonbar"], 20, 745)
	end
	self:Bar(L["spiderbar"], 30, "INV_Misc_MonsterSpiderCarapace_01")
end

function mod:BigWigs_RecvSync(sync, rest, nick)
	if self:ValidateEngageSync(sync, rest) and not started then
		started = true
		if self:IsEventRegistered("PLAYER_REGEN_DISABLED") then self:UnregisterEvent("PLAYER_REGEN_DISABLED") end
		self:Spray()
	end
end

function mod:CocoonWarn()
	local msg = nil
	for k in pairs(inCocoon) do
		if not msg then
			msg = k
		else
			msg = msg .. ", " .. k
		end
	end
	self:IfMessage(L["cocoonwarn"]:format(msg), "Important", 745, "Alert")
	for k in pairs(inCocoon) do inCocoon[k] = nil end
end

function mod:CHAT_MSG_MONSTER_EMOTE(msg)
	if self.db.profile.enrage and msg == L["enragetrigger"] then
		self:Message(L["enragewarn"], "Important")
	end
end

function mod:UNIT_HEALTH(msg)
	if UnitName(msg) == boss then
		local health = UnitHealth(msg)
		if health > 30 and health <= 33 and not enrageannounced then
			if self.db.profile.enrage then self:Message(L["enragesoonwarn"], "Important") end
			enrageannounced = true
		elseif health > 40 and enrageannounced then
			enrageannounced = nil
		end
	end
end

