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

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

local fmt = string.format
local UnitName = UnitName
local db = nil

local explosionIcon = "Spell_Shadow_MindBomb"
local addsIcon = "INV_Misc_Head_Troll_01"
local enrageIcon = "Spell_Shadow_UnholyFrenzy"

local bombSoonEvent = "BWJanalaiExplosionSoon"
local bombCountdown3Event = "BWJanalaiExplosionCountdown3"
local bombCountdown2Event = "BWJanalaiExplosionCountdown2"
local bombCountdown1Event = "BWJanalaiExplosionCountdown1"
local softEnrageEvent = "BWJanalaiSoftEnrage"
local hardEnrageEvent = "BWJanalaiHardEnrage"

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

L:RegisterTranslations("enUS", function() return {
	cmd = "Jan'alai",

	engage_trigger = "Spirits of da wind be your doom!",

	flame = "Flame Breath",
	flame_desc = "Warn who Jan'alai casts Flame Strike on.",
	flame_message = "Flame Breath on %s!",

	icon = "Raid Icon",
	icon_desc = "Place a Raid Target Icon on the player targetted by Flame Breath. (requires promoted or higher)",

	bomb = "Explosion",
	bomb_desc = "Show timers for Explosion.",
	bomb_trigger = "I burn ya now!",
	bomb_message = "Explosion in 2 Seconds!",
	bomb_countdown = "Explosion in %d Seconds!",

	adds = "Adds",
	adds_desc = "Warn for Incoming Adds.",
	adds_trigger = "Where ma hatcha? Get to work on dem eggs!",
	adds_message = "Hatcher Spawning",
	adds_over_trigger = "I show you strength... in numbers.",

	soft_enrage = "Soft Enrage",
	soft_enrage_warning = "Soft Enrage: Overheal Tank",
	hard_enrage = "Hard Enrage",
	hard_enrage_warning = "Hard Enrage!",
} end )

L:RegisterTranslations("frFR", function() return {
	engage_trigger = "Les esprits du vent, ils vont être votre fin !",

	flame = "Souffle de flammes",
	flame_desc = "Prévient sur qui Jan'alai incante son Souffle de flammes.",
	flame_message = "Souffle de flammes sur %s !",

	icon = "Icône",
	icon_desc = "Place une icône de raid sur le dernier joueur affecté par le Souffle de flammes (nécessite d'être promu ou mieux).",

	bomb = "Explosion",
	bomb_desc = "Affiche les délais concernant l'explosion.",
	bomb_trigger = "J'vais vous cramer !",
	bomb_message = "Explosion dans 2 sec !",

	adds = "Perce-coques",
	adds_desc = "Prévient de l'arrivée des perce-coques.",
	adds_trigger = "Sont où mes perce-coque ? Au boulot ! Faut qu'ça éclose !",
	adds_message = "Arrivée des perce-coques !",
	adds_over_trigger = "I show you strength... in numbers.",

	soft_enrage = "Enrage progressif",
	soft_enrage_warning = "Enrage progressif : soignez le tank",
	hard_enrage = "Enrage total",
	hard_enrage_warning = "Enrage total !",
} end )

L:RegisterTranslations("koKR", function() return {
	engage_trigger = "바람의 혼이 너희를 쓸어내리라!",

	flame = "화염 숨결",
	flame_desc = "잔알라이가 대상자방향으로 화염 숨결을 시전하는지 알립니다.",
	flame_message = "%s에게 화염 숨결!",

	icon = "전술 표시",
	icon_desc = "화염 숨결 대상이된 플레이어에게 전술 표시를 지정합니다. (승급자 이상 권한 필요)",

	bomb = "폭발",
	bomb_desc = "폭발에 대한 타이머를 표시합니다.",
	bomb_trigger = "태워버리겠다!",
	bomb_message = "2초 후 폭발!",

	adds = "부화사 등장",
	adds_desc = "부화사 등장에 대해 경고합니다.",
	adds_trigger = "다 어디 갔지? 당장 알을 부화시켜!",
	adds_message = "잠시후 부화사 등장!",
	adds_over_trigger = "I show you strength... in numbers.",

	soft_enrage = "연화 격노",
	soft_enrage_warning = "연화 격노: 탱커를 집중 치유!",
	hard_enrage = "완전 격노",
	hard_enrage_warning = "완전 격노!",
} end )

L:RegisterTranslations("deDE", function() return {
	engage_trigger = "Die Geister der Winde besiegeln Euer Schicksal!",

	flame = "Flammenatem",
	flame_desc = "Warnt auf wen Flammenatem von Jan'alai gewirkt wird.",
	flame_message = "Flammenatem auf %s!",

	icon = "Schlachtzug Symbol",
	icon_desc = "Plaziert ein Schlachtgruppen Symbol auf Spielern auf die Flammenatem gewirkt wird(benötigt Assistent oder höher).",

	bomb = "Explosion",
	bomb_desc = "Zeigt Timer für die Explosion an.",
	bomb_trigger = "Jetzt sollt Ihr brennen!",
	bomb_message = "Explosion in 2 Sek!",

	adds = "Brutwächter",
	adds_desc = "Warnt wenn Brutwächter gerufen werden.",
	adds_trigger = "Wo is' meine Brut? Was ist mit den Eiern?",
	adds_message = "Brutwächter wurden gerufen!",
	adds_over_trigger = "I show you strength... in numbers.",

	soft_enrage = "Weiche Wut",
	soft_enrage_warning = "Weiche Wut: Tank hochheilen",
	hard_enrage = "Harte Wut",
	hard_enrage_warning = "Harte Wut!",
} end )


L:RegisterTranslations("zhCN", function() return {
	engage_trigger = "风之圣魂将是你的梦魇！",

	flame = "烈焰吐息",
	flame_desc = "当施放烈焰吐息时发出警报。",
	flame_message = "烈焰吐息：>%s<！",

	icon = "团队标记",
	icon_desc = "给中了烈焰吐息的玩家打上团队标记。（需要权限）",

	bomb = "爆炸",
	bomb_desc = "显示爆炸计时条。",
	bomb_trigger = "烧死你们！",
	bomb_message = "2秒后爆炸！",

	adds = "援兵",
	adds_desc = "当援兵即将到来时发出警报。",
	adds_trigger = "雌鹰哪里去了？快去孵蛋！",
	adds_message = "即将 出现救援！",
	adds_over_trigger = "I show you strength... in numbers.",

	soft_enrage = "软性狂暴",
	soft_enrage_warning = "软性狂暴：治疗坦克",
	hard_enrage = "硬性狂暴",
	hard_enrage_warning = "硬性狂暴！",
} end )

L:RegisterTranslations("zhTW", function() return {
	engage_trigger = "風之聖魂將是你的夢魘!",

	flame = "火息術",
	flame_desc = "警告賈納雷施放火息術",
	flame_message = "火息術: [%s] - 散開",

	icon = "標記圖示",
	icon_desc = "為被火息術的玩家設置團隊標記（需要權限）",

	bomb = "爆炸",
	bomb_desc = "顯示爆炸計時條",
	bomb_trigger = "燒死你們!",
	bomb_message = "2秒後爆炸!",

	adds = "救援",
	adds_desc = "警告救援即將出現",
	adds_trigger = "雌鷹哪裡去啦?快去孵蛋!",
	adds_message = "救援即將出現!",
	adds_over_trigger = "I show you strength... in numbers.",

	soft_enrage = "軟性狂怒",
	soft_enrage_warning = "軟性狂怒：治療坦克",
	hard_enrage = "硬性狂怒",
	hard_enrage_warning = "硬性狂怒!",
} end )

L:RegisterTranslations("esES", function() return {
	engage_trigger = "¡Los espíritus del viento serán vuestra maldición!",

	flame = "Aliento de llamas (Flame Breath)",
	flame_desc = "Avisar quién recibe Aliento de llamas.",
	flame_message = "¡Aliento de llamas en %s!",

	icon = "Icono de banda",
	icon_desc = "Poner un icono de banda sobre jugadores afectados por Aliento de llamas. (Requiere derechos de banda)",

	bomb = "Explosión",
	bomb_desc = "Mostrar barra de tiempo para Explosión.",
	bomb_trigger = "¡Ahora os quemaré!",
	bomb_message = "¡Explosión en 2 segundos!",

	adds = "Añadidos",
	adds_desc = "Avisar cuando entran añadidos.",
	adds_trigger = "¿Dónde está mi criador? ¡A por los huevos!",
	adds_message = "¡Vienen añadidos!",
	adds_over_trigger = "I show you strength... in numbers.",

	soft_enrage = "Furia suave",
	soft_enrage_warning = "Furia suave: curad al tanque",
	hard_enrage = "Furia total",
	hard_enrage_warning = "¡Furia total!",
} end )

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

local mod = BigWigs:NewModule(boss)
mod.zonename = BZ["Zul'Aman"]
mod.enabletrigger = boss
mod.guid = 23578
mod.toggleoptions = {"bomb", "adds", -1, "flame", "icon", "enrage", "berserk", "bosskill"}
mod.revision = tonumber(("$Revision: 76590 $"):sub(12, -3))

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

function mod:OnEnable()
        self:AddCombatListener("SPELL_CAST_START", "FlameBreath", 43140)
        self:AddCombatListener("UNIT_DIED", "BossDeath")

        self:RegisterEvent("CHAT_MSG_MONSTER_YELL")
        self:RegisterEvent("PLAYER_REGEN_ENABLED", "CheckForWipe")

        db = self.db.profile
end

function mod:OnDisable()
        self:CancelScheduledEvent(bombSoonEvent)
        self:CancelScheduledEvent(bombCountdown3Event)
        self:CancelScheduledEvent(bombCountdown2Event)
        self:CancelScheduledEvent(bombCountdown1Event)
        self:CancelScheduledEvent(softEnrageEvent)
        self:CancelScheduledEvent(hardEnrageEvent)
end

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

local function ScanTarget()
	local target
	if UnitName("target") == boss then
		target = UnitName("targettarget")
	elseif UnitName("focus") == boss then
		target = UnitName("focustarget")
	else
		local num = GetNumRaidMembers()
		for i = 1, num do
			if UnitName(fmt("%s%d%s", "raid", i, "target")) == boss then
				target = UnitName(fmt("%s%d%s", "raid", i, "targettarget"))
				break
			end
		end
	end
	if target then
		mod:IfMessage(fmt(L["flame_message"], mod:ColorName(target)), "Important", 23461)
		if mod.db.profile.icon then
			mod:Icon(target)
		end
	end
end

function mod:FlameBreath()
	if db.flame then
		self:ScheduleEvent("BWFlameToTScan", ScanTarget, 0.2)
		self:ScheduleEvent("BWRemoveJanIcon", "BigWigs_RemoveRaidIcon", 4, self)
	end
end

function mod:StartSoftEnrageTimer()
        self:Bar(L["soft_enrage"], 300, enrageIcon)
        self:CancelScheduledEvent(softEnrageEvent)
        self:ScheduleEvent(softEnrageEvent, "BigWigs_Message", 300, L["soft_enrage_warning"], "Attention", nil, "Alert")
end

function mod:StartHardEnrageTimer()
        self:Bar(L["hard_enrage"], 600, enrageIcon)
        self:CancelScheduledEvent(hardEnrageEvent)
        self:ScheduleEvent(hardEnrageEvent, "BigWigs_Message", 600, L["hard_enrage_warning"], "Attention", nil, "Alarm")
end

function mod:CHAT_MSG_MONSTER_YELL(msg)
        if db.bomb and msg == L["bomb_trigger"] then
                self:Bar(L["bomb"], 11, explosionIcon)
                self:CancelScheduledEvent(bombSoonEvent)
                self:CancelScheduledEvent(bombCountdown3Event)
                self:CancelScheduledEvent(bombCountdown2Event)
                self:CancelScheduledEvent(bombCountdown1Event)
                self:ScheduleEvent(bombSoonEvent, "BigWigs_Message", 8, L["bomb_countdown"]:format(3), "Urgent")
                self:ScheduleEvent(bombCountdown2Event, "BigWigs_Message", 9, L["bomb_countdown"]:format(2), "Urgent")
                self:ScheduleEvent(bombCountdown1Event, "BigWigs_Message", 10, L["bomb_countdown"]:format(1), "Urgent")
        elseif db.adds and msg == L["adds_trigger"] then
                self:Message(L["adds_message"], "Positive")
                self:Bar(L["adds"], 90, addsIcon)
        elseif db.adds and msg == L["adds_over_trigger"] then
                self:TriggerEvent("BigWigs_StopBar", self, L["adds"])
        elseif msg == L["engage_trigger"] then
                if db.enrage then
                        self:StartSoftEnrageTimer()
                end
                if db.berserk then
                        self:StartHardEnrageTimer()
                end
                if db.adds then
                        self:Bar(L["adds"], 11, addsIcon)
                end
        end
end

