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

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

local started = nil
local pName = UnitName("player")
local db = nil
local previous = nil
local kiteStart = nil
local UnitName = UnitName
local fmt = string.format
local GetTime = GetTime

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

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

        phase = "Phases",
        phase_desc = "Warn about the different phases.",
        normal_phase_trigger = "Supremus punches the ground in anger!",
        kite_phase_trigger = "The ground begins to crack open!",
        next_phase_bar = "Next phase",
        next_phase_message = "Phase change in 10sec!",
        phase_change_5 = "Phase change in 5sec!",
        phase_change_4 = "Phase change in 4sec!",
        phase_change_3 = "Phase change in 3sec!",
        phase_change_2 = "Phase change in 2sec!",
        phase_change_1 = "Phase change in 1sec!",

	punch = "Molten Punch",
	punch_desc = "Alert when he does Molten Punch, and display a countdown bar.",
	punch_message = "Molten Punch!",
	punch_bar = "~Possible Punch!",

	target = "Target",
	target_desc = "Warn who he targets during the kite phase, and put a raid icon on them.",
	target_message = "%s being chased!",
	target_you = "YOU are being chased!",
	target_message_nounit = "New target!",

	icon = "Raid Target Icon",
	icon_desc = "Place a Raid Target Icon on the player being chased(requires promoted or higher).",
} end )

L:RegisterTranslations("esES", function() return {
        phase = "Fases",
        phase_desc = "Avisar sobre cambios de fase.",
        normal_phase_trigger = "¡Supremus golpea el suelo enfadado!",
        kite_phase_trigger = "El suelo comienza a abrirse.",
        next_phase_bar = "~Siguiente fase",
        next_phase_message = "Cambio de fase en 10 seg",
        phase_change_3 = "Cambio de fase en 3 seg",
        phase_change_2 = "Cambio de fase en 2 seg",
        phase_change_1 = "Cambio de fase en 1 seg",

	punch = "Puñetazo de arrabio (Molten Punch)",
	punch_desc = "Avisar cuando lanza Puñetazo de arrabio y mostrar una barra de reutilización.",
	punch_message = "¡Puñetazo de arrabio!",
	punch_bar = "~Puñetazo de arrabio",

	target = "Objetivo",
	target_desc = "Avisar a quién selecciona durante la fase de persecución y poner un icono sobre su cabeza.",
	target_message = "¡Persiguiendo a %s!",
	target_you = "¡Te está persiguiendo a TI!",
	target_message_nounit = "¡Nuevo objetivo!",

	icon = "Icono de banda",
	icon_desc = "Poner un icono sobre el jugador que está siendo perseguido. (Requiere derechos de banda).",
} end )

L:RegisterTranslations("deDE", function() return {
        phase = "Phasen",
        phase_desc = "Warnung wenn Supremus zwischen Tank und Kitephase wechselt.",
        normal_phase_trigger = "Supremus schlägt wütend auf den Boden!",
        kite_phase_trigger = "Der Boden beginnt aufzubrechen!",
        next_phase_bar = "Nächste Phase",
        next_phase_message = "Phasenwechsel in 10sec!",
        phase_change_3 = "Phasenwechsel in 3sek!",
        phase_change_2 = "Phasenwechsel in 2sek!",
        phase_change_1 = "Phasenwechsel in 1sek!",

	punch = "Glühender Hieb",
	punch_desc = "Warnt, wenn Supremus Glühender Hieb benutzt und zeigt einen Countdown an.",
	punch_message = "Glühender Hieb!",
	punch_bar = "~Möglicher Hieb!",

	target = "Verfolgtes Ziel",
	target_desc = "Warnt wer wärend der Kitephase verfolgt wird.",
	target_message = "%s wird verfolgt!",
	target_you = "DU wirst verfolgt!",
	target_message_nounit = "Neues Ziel!",

	icon = "Schlachtzug Symbol",
	icon_desc = "Plaziert ein Schlachtzug Symbol auf dem Spieler der verfolgt wird (benötigt Assistent oder höher).",
} end )

L:RegisterTranslations("koKR", function() return {
        phase = "단계",
        phase_desc = "다른 단계에 대한 경고입니다.",
        normal_phase_trigger =  "궁극의 심연이 분노하여 땅을 내리찍습니다!",
        kite_phase_trigger = "땅이 갈라져서 열리기 시작합니다!",
        next_phase_bar = "다음 형상",
        next_phase_message = "10초 이내 형상 변경!",
        phase_change_3 = "3초 이내 형상 변경!",
        phase_change_2 = "2초 이내 형상 변경!",
        phase_change_1 = "1초 이내 형상 변경!",

	punch = "이글거리는 주먹",
	punch_desc = "이글거리는 주먹의 경고와 대기시간 바를 표시합니다.",
	punch_message = "이글거리는 주먹!",
	punch_bar = "~주먹 가능!",

	target = "대상",
	target_desc = "솔개 형상에서 대상을 알리고 전술 표시를 지정합니다.",
	target_message = "%s 추적 중!",
	target_you = "당신을 추적 중!",
	target_message_nounit = "새로운 대상!",

	icon = "전술 표시",
	icon_desc = "추적 중인 플레이어에게 전술 표시를 지정합니다. (승급자 이상 권한 요구)",
} end )

L:RegisterTranslations("frFR", function() return {
        phase = "Phase",
        phase_desc = "Prévient quand la rencontre entre dans une nouvelle phase.",
        normal_phase_trigger = "De rage, Supremus frappe le sol !",
        kite_phase_trigger = "Le sol commence à se fissurer !",
        next_phase_bar = "Prochaine phase",
        next_phase_message = "Changement de phase dans 10 sec. !",
        phase_change_3 = "Changement de phase dans 3 sec. !",
        phase_change_2 = "Changement de phase dans 2 sec. !",
        phase_change_1 = "Changement de phase dans 1 sec. !",

	punch = "Punch de la fournaise",
	punch_desc = "Prévient quand Supremus utilise son Punch de la fournaise, et affiche une barre de cooldown.",
	punch_message = "Punch de la fournaise !",
	punch_bar = "~Punch probable",

	target = "Cible",
	target_desc = "Indique la personne pourchassée pendant la phase de kitting.",
	target_message = "%s est poursuivi(e) !",
	target_you = "Vous êtes poursuivi(e) !",
	target_message_nounit = "Nouvelle cible !",

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

L:RegisterTranslations("zhTW", function() return {
        phase = "階段",
        phase_desc = "提示階段",
        normal_phase_trigger = "瑟普莫斯憤怒的捶擊地面!",
        kite_phase_trigger = "地上開始裂開!",
        next_phase_bar = "下一個階段",
        next_phase_message = "10 秒內改變階段!",
        phase_change_3 = "3 秒內改變階段!",
        phase_change_2 = "2 秒內改變階段!",
        phase_change_1 = "1 秒內改變階段!",

	punch = "熔火之擊",
	punch_desc = "警報在施放熔火之擊，顯示冷卻條",
	punch_message = "熔火之擊!",
	punch_bar = "<熔火之擊冷卻>",

	target = "目標",
	target_desc = "警報在風箏階段誰是主要目標，並在他頭上放團隊標記。 (需要權限)",
	target_message = "被盯上: [%s]",
	target_you = "被盯上: [你]",
	target_message_nounit = "新目標!",

	icon = "目標標記",
	icon_desc = "在被盯上的隊友頭上標記 (需要權限)",
} end )

L:RegisterTranslations("zhCN", function() return {
        phase = "阶段",
        phase_desc = "当不同阶段时发出警报。",
        normal_phase_trigger = "苏普雷姆斯愤怒地击打着地面！",
        kite_phase_trigger = "地面崩裂了！",
        next_phase_bar = "<下一阶段>",
        next_phase_message = "10秒后，阶段改变！",
        phase_change_3 = "3秒后，阶段改变！",
        phase_change_2 = "2秒后，阶段改变！",
        phase_change_1 = "1秒后，阶段改变！",

	punch = "熔岩打击",
	punch_desc = "当施放熔岩打击时发出警报并显示冷却记时条。",
	punch_message = "熔岩打击！",
	punch_bar = "<可能 熔岩打击>",

	target = "目标",
	target_desc = "当玩家被凝视时发出警报并被打上团队标记。",
	target_message = "凝视：>%s <！",
	target_you = ">你< 被凝视！",
	target_message_nounit = "新目标！",

	icon = "团队标记",
	icon_desc = "给被凝视的玩家打上团队标记。（需要权限）",
} end )

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

local mod = BigWigs:NewModule(boss)
mod.zonename = BZ["Black Temple"]
mod.enabletrigger = boss
mod.guid = 22898
mod.toggleoptions = {"punch", "target", "icon", "phase", "enrage", "bosskill"}
mod.revision = tonumber(("$Revision: 79400 $"):sub(12, -3))

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

function mod:OnEnable()
	self:AddCombatListener("SPELL_CAST_SUCCESS", "Punch", 40126)
	self:AddCombatListener("UNIT_DIED", "BossDeath")

	started = nil
	previous = nil
	kiteStart = nil

	self:RegisterEvent("CHAT_MSG_RAID_BOSS_EMOTE")

	self:RegisterEvent("PLAYER_REGEN_DISABLED", "CheckForEngage")
	self:RegisterEvent("PLAYER_REGEN_ENABLED", "CheckForWipe")
	self:RegisterEvent("BigWigs_RecvSync")

	db = self.db.profile
end

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

function mod:Punch(_, spellID)
	if db.punch then
		self:IfMessage(L["punch_message"], "Attention", spellID)
		self:Bar(L["punch_bar"], 10, spellID)
	end
end

function mod:TargetCheck()
	if kiteStart and (GetTime() - kiteStart) < 2 then
		return
	end
	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 ~= previous then
		if target then
			local other = fmt(L["target_message"], target)
			local otherColored = fmt(L["target_message"], self:ColorName(target))
			if target == pName then
				self:LocalMessage(L["target_you"], "Personal", nil, "Alarm")
				self:WideMessage(otherColored)
			else
				self:Message(otherColored, "Attention")
			end
			self:Icon(target, "icon")
			previous = target
		else
			previous = nil
		end
	end
end

function mod:CHAT_MSG_RAID_BOSS_EMOTE(msg)
        if msg == L["normal_phase_trigger"] then
                if db.phase then
                        self:Bar(L["next_phase_bar"], 60, "INV_Helmet_08")
                        self:DelayedMessage(50, L["next_phase_message"], "Attention")
                        self:DelayedMessage(55, L["phase_change_5"], "Attention")
                        self:DelayedMessage(56, L["phase_change_4"], "Attention")
                        self:DelayedMessage(57, L["phase_change_3"], "Attention")
                        self:DelayedMessage(58, L["phase_change_2"], "Attention")
                        self:DelayedMessage(59, L["phase_change_1"], "Attention")
                end
                if db.target then
                        self:CancelScheduledEvent("BWSupremusToTScan")
                        kiteStart = nil
                        self:TriggerEvent("BigWigs_RemoveRaidIcon")
                end
        elseif msg == L["kite_phase_trigger"] then
                if db.phase then
                        self:Bar(L["next_phase_bar"], 60, "Spell_Fire_MoltenBlood")
                        self:DelayedMessage(50, L["next_phase_message"], "Attention")
                        self:DelayedMessage(55, L["phase_change_5"], "Attention")
                        self:DelayedMessage(56, L["phase_change_4"], "Attention")
                        self:DelayedMessage(57, L["phase_change_3"], "Attention")
                        self:DelayedMessage(58, L["phase_change_2"], "Attention")
                        self:DelayedMessage(59, L["phase_change_1"], "Attention")
                end
                if db.target then
                        kiteStart = GetTime()
                        previous = nil
                        self:ScheduleRepeatingEvent("BWSupremusToTScan", self.TargetCheck, 1, self)
                end
        end
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
                if db.phase then
                        self:Bar(L["next_phase_bar"], 60, "Spell_Fire_MoltenBlood")
                        self:DelayedMessage(50, L["next_phase_message"], "Attention")
                        self:DelayedMessage(55, L["phase_change_5"], "Attention")
                        self:DelayedMessage(56, L["phase_change_4"], "Attention")
                        self:DelayedMessage(57, L["phase_change_3"], "Attention")
                        self:DelayedMessage(58, L["phase_change_2"], "Attention")
                        self:DelayedMessage(59, L["phase_change_1"], "Attention")
                end
                if db.enrage then
                        self:Enrage(600)
                end
        end
end

