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

local entropius = BB["Entropius"]
local boss = BB["M'uru"]
local L = AceLibrary("AceLocale-2.2"):new("BigWigs"..boss)

local db = nil
local started = nil
local phase = nil
local voidcount = 1
local humanoidcount = 1
local pName = UnitName("player")
local fmt = string.format

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

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

	darkness = "Darkness timers",
	darkness_desc = "Warn for Darkness timers and countdowns.",
	darknessplayers = "Darkness players",
	darknessplayers_desc = "List players who have Darkness.",
	darkness_message = "Darkness: %s",
	darkness_next = "Next Darkness",
	darkness_soon = "Darkness in 5sec!",
	darkness15 = "Darkness 15 sec warning",
	darkness15_desc = "Warn 15 seconds before Darkness.",
	darkness_15 = "Darkness in 15sec!",
	darkness10 = "Darkness 10 sec warning",
	darkness10_desc = "Warn 10 seconds before Darkness.",
	darkness_10 = "Darkness in 10sec!",
	darkness10audio = "Darkness 10 Audio",
	darkness10audio_desc = "Play a local-only sound 11.5 seconds before Darkness.",
	darkness5audio = "Darkness 5 Audio",
	darkness5audio_desc = "Play a local-only sound 6.5 seconds before Darkness.",
	twominuteaudio = "Two-Minute Audio",
	twominuteaudio_desc = "Play a local-only two-minute cooldown reminder every 2 minutes, starting 2:10 after engage.",
	darknesscountdown = "Darkness countdown",
	darknesscountdown_desc = "Countdown 5-1 before Darkness.",
	darkness_countdown = "Darkness in %d",

	void = "Void Sentinel",
	void_desc = "Warn when the Void Sentinel spawns.",
	void_next = "Next Void Sentinel (%d)",
	void_soon = "Sentinel (%d) in 5 sec!",

	humanoid = "Humanoid Adds",
	humanoid_desc = "Warn when the Humanoid Adds spawn.",
	humanoid_next = "Next Humanoids (%d)",
	humanoid_soon = "Humanoids (%d) in 5sec!",

	fiends = "Dark Fiends",
	fiends_desc = "Warn for Dark Fiends spawning.",
	fiends_message = "Dark Fiends Inc!",

	phase = "Phases",
	phase_desc = "Warn for phase changes.",
	phase2_message = "Phase 2",

	gravity = "Gravity Balls",
	gravity_desc = "Warn for Gravity Balls.",
	gravity_next = "Next Gravity Ball Timer",
	gravity_soon = "Gravity Ball soon!",
	gravity_spawned = "Gravity Ball spawned!",

	voidzone = "Void Zone targets",
	voidzone_desc = "Warn who has a Void Zone spawning under them.",
	voidzone_message = "Void Zone: %s (Group %d)",
	voidzone_message_nogroup = "Void Zone: %s",
	voidzonesay = "Void Zone /say",
	voidzonesay_desc = "Say when a Void Zone is spawning under you.",
	voidzonesay_message = "Void Zone under me soon!",
	voidzoneicon = "Void Zone icon",
	voidzoneicon_desc = "Mark Void Zone targets with a diamond.",

	orb = "Orb targets",
	orb_desc = "Warn who has an Orb spawning under them.",
	orb_message = "Orb: %s (Group %d)",
	orb_message_nogroup = "Orb: %s",
	orbsay = "Orb /say",
	orbsay_desc = "Say when an Orb is spawning under you.",
	orbsay_message = "Orb spawning here soon!",
	orbicon = "Orb icon",
	orbicon_desc = "Mark Orb targets with a square.",
} end )

L:RegisterTranslations("esES", function() return {
	darkness = "Oscuridad (Darkness)",
	darkness_desc = "Avisar quién tiene Oscuridad.",
	darkness_message = "Oscuridad: %s",
	darkness_next = "~Oscuridad",
	darkness_soon = "Oscuridad en 5 seg",

	void = "Centinela del vacío (Void Sentinel)",
	void_desc = "Avisar cuando aparece un centinela del vacío.",
	void_next = "~Centinela (%d)",
	void_soon = "Centinela (%d) en 5 seg",

	humanoid = "Añadidos humanoides",
	humanoid_desc = "Avisar cuando aparecen los humanoides.",
	humanoid_next = "~Humanoides (%d)",
	humanoid_soon = "Humanoides (%d) en 5 seg",

	fiends = "Malignos oscuros (Dark Fiends)",
	fiends_desc = "Avisar cuando aparecen Malignos oscuros.",
	fiends_message = "¡Malignos oscuros!",

	phase = "Fases",
	phase_desc = "Avisar sobre las distintas fases del encuentro.",
	phase2_message = "¡Fase 2!",

	--gravity = "Gravity Balls",
	--gravity_desc = "Warn for Gravity Balls.",
	--gravity_next = "Next Gravity Ball Timer",
	--gravity_soon = "Gravity Ball soon!",
	--gravity_spawned = "Gravity Ball spawned!",
} end )

L:RegisterTranslations("frFR", function() return {
	darkness = "Ténèbres",
	darkness_desc = "Prévient quand quelqu'un subit les effets des Ténèbres.",
	darkness_message = "Ténèbres : %s",
	darkness_next = "Prochaines Ténèbres",
	darkness_soon = "Ténèbres dans 5 sec. !",

	void = "Sentinelles du Vide",
	void_desc = "Prévient quand les Sentinelles du Vide apparaissent.",
	void_next = "Prochaine Sentinelle (%d)",
	void_soon = "Sentinelle (%d) dans 5 sec. !",

	humanoid = "Renforts humanoïdes",
	humanoid_desc = "Prévient quand les renforts humanoïdes apparaissent.",
	humanoid_next = "Prochains humanoïdes (%d)",
	humanoid_soon = "Humanoïdes (%d) dans 5 sec. !",

	fiends = "Sombres fiels",
	fiends_desc = "Prévient quand les Sombres fiels apparaissent.",
	fiends_message = "Arrivée des Sombres fiels !",

	phase = "Phases",
	phase_desc = "Prévient quand la rencontre entre dans une nouvelle phase.",
	phase2_message = "Phase 2 !",

	gravity = "Singularité",
	gravity_desc = "Prévient de l'arrivée des Singularités.",
	gravity_next = "Prochaine Singularité",
	gravity_soon = "Singularité imminente !",
	gravity_spawned = "Singularité apparue !",
} end )

L:RegisterTranslations("koKR", function() return {
	darkness = "어둠",
	darkness_desc = "어둠에 걸린 플레이어를 알립니다.",
	darkness_message = "어둠: %s",
	darkness_next = "다음 어둠",
	darkness_soon = "5초 후 어둠!",

	void = "공허의 파수병",
	void_desc = "공허의 파수병의 소환을 알립니다.",
	void_next = "다음 공허의 파수병(%d)",
	void_soon = "5초 이내 파수병(%d)!",

	humanoid = "타락한 엘프",
	humanoid_desc = "타락한 엘프 등장을 알립니다.",
	humanoid_next = "다음 타락한 엘프(%d)",
	humanoid_soon = "5초 이내 타락한 엘프(%d)!",

	fiends = "어둠 마귀",
	fiends_desc = "어둠 마귀 소환을 알립니다.",
	fiends_message = "잠시 후 어둠 마귀!",

	phase = "단계",
	phase_desc = "단계 변경을 알립니다.",
	phase2_message = "2 단계!",

	gravity = "중력 구체",
	gravity_desc = "중력 구체를 알립니다.",
	gravity_next = "다음 중력 구체",
	gravity_soon = "잠시 후 중력 구체!",
	gravity_spawned = "중력 구체 소환!",
} end )

L:RegisterTranslations("zhCN", function() return {
	darkness = "黑暗",
	darkness_desc = "当玩家受到黑暗时发出警报。",
	darkness_message = "黑暗：>%s<！ ",
	darkness_next = "<下一黑暗>",
	darkness_soon = "5秒后，黑暗！",

	void = "虚空戒卫",
	void_desc = "当虚空戒卫刷新时发出警报。",
	void_next = "<下一虚空戒卫：%d>",
	void_soon = "5秒后，虚空戒卫刷新：>%d<！",

	humanoid = "暗誓精灵",
	humanoid_desc = "当暗誓精灵刷新时发出警报。",
	humanoid_next = "<下一暗誓精灵：%d>",
	humanoid_soon = "5秒后，暗誓精灵刷新：>%d<！",

	fiends = "黑暗魔",
	fiends_desc = "当黑暗魔刷新时发出警报。",
	fiends_message = "黑暗魔 出现！",

	phase = "阶段",
	phase_desc = "当进入不同阶段发出警报。",
	phase2_message = "第二阶段！",

	gravity = "黑洞",
	gravity_desc = "当施放黑洞时发出警报。",
	gravity_next = "<下一黑洞>",
	gravity_soon = "即将 黑洞！",
	gravity_spawned = "黑洞 出现！",
} end )

L:RegisterTranslations("zhTW", function() return {
	darkness = "黑暗",
	darkness_desc = "警報誰受到黑暗效果",
	darkness_message = "黑暗: [%s]",
	darkness_next = "下一次黑暗",
	darkness_soon = "約 5 秒內施放黑暗!",

	void = "虛無哨兵",
	void_desc = "當虛無哨兵出現時發出警報",
	void_next = "下一波虛無哨兵：%d",
	void_soon = "約 5 秒內虛無哨兵出現：>%d<!",

	humanoid = "虛無哨兵召喚者",
	humanoid_desc = "當虛無哨兵召喚者出現時發出警報",
	humanoid_next = "下一波召喚者：%d",
	humanoid_soon = "約 5 秒內召喚者出現：>%d<!",

	--fiends = "Dark Fiends",
	--fiends_desc = "Warn for Dark Fiends spawning.",
	--fiends_message = "Dark Fiends Inc!",

	--phase = "Phases",
	--phase_desc = "Warn for phase changes.",
	--phase2_message = "Phase 2!",

	--gravity = "Gravity Balls",
	--gravity_desc = "Warn for Gravity Balls.",
	--gravity_next = "Next Gravity Ball Timer",
	--gravity_soon = "Gravity Ball soon!",
	--gravity_spawned = "Gravity Ball spawned!",
} end )

L:RegisterTranslations("deDE", function() return {
	darkness = "Dunkelheit",
	darkness_desc = "Warnung wer von Dunkelheit betroffen ist.",
	darkness_message = "Dunkelheit: %s",
	darkness_next = "Nächste Dunkelheit",
	darkness_soon = "Dunkelheit in 5sek!",

	void = "Leerenwache",
	void_desc = "Warnung wenn eine Leerenwache erscheint.",
	void_next = "Nächste Leerenwache (%d)",
	void_soon = "Leerenwache (%d) in 5 sek!",

	humanoid = "Menschliche Wache",
	humanoid_desc = "Warnung wenn Menschliche Wachen erscheinen.",
	humanoid_next = "Nächste Wachen (%d)",
	humanoid_soon = "Wachen (%d) in 5sec!",

	fiends = "Finsteres Scheusal",
	fiends_desc = "Warnung wenn Finsteres Scheusale erscheinen.",
	fiends_message = "Finsteres Scheusale Inc!",

	phase = "Phasen",
	phase_desc = "Warnung bei Phasenänderrungen.",
	phase2_message = "Phase 2",

	gravity = "Schwerkraftkugel",
	gravity_desc = "Warnung wann die Schwerkraftkugel erscheint.",
	gravity_next = "Nächste Schwerkraftkugel",
	gravity_soon = "Schwerkraftkugel bald!",
	--gravity_spawned = "Schwerkraftkugel spawned!",
} end )

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

local mod = BigWigs:NewModule(boss)
mod.zonename = BZ["Sunwell Plateau"]
mod.enabletrigger = boss
mod.guid = 25840
mod.toggleoptions = {"phase", -1, "darkness", "darknessplayers", "darkness15", "darkness10", "darkness10audio", "darkness5audio", "twominuteaudio", "darknesscountdown", "void", "humanoid", "fiends", "gravity", -1, "voidzone", "voidzonesay", "voidzoneicon", "orb", "orbsay", "orbicon", "enrage", "bosskill"}
mod.revision = tonumber(("$Revision: 76755 $"):sub(12, -3))
mod.defaultDB = {
	darkness10audio = false,
	darkness5audio = false,
	twominuteaudio = false,
	voidzonesay = false,
	orbsay = false,
}

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

function mod:OnEnable()
	self:AddCombatListener("SPELL_AURA_APPLIED", "Darkness", 45996)
	self:AddCombatListener("SPELL_CAST_SUCCESS", "Fiends", 45934)
	self:AddCombatListener("SPELL_CAST_SUCCESS", "Portals", 46177)
	self:AddCombatListener("SPELL_CAST_SUCCESS", "VoidZone", 46269)
	self:AddCombatListener("SPELL_CAST_SUCCESS", "GravityBall", 46282)
	self:AddCombatListener("UNIT_DIED", "BossDeath")

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

	db = self.db.profile
	started = nil
	phase = 0
end

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

function mod:Darkness(unit, spellID)
	if db.darknessplayers then
		self:IfMessage(L["darkness_message"]:format(self:ColorName(unit)), "Positive", spellID)
	end
end

function mod:GetGroupNumber(player)
	for i = 1, GetNumRaidMembers() do
		local name, _, subGroup = GetRaidRosterInfo(i)
		if name == player then return subGroup end
	end
end

function mod:FormatTargetMessage(key, player)
	local group = self:GetGroupNumber(player)
	local colored = self:ColorName(player)
	if group then
		return fmt(L[key.."_message"], colored, group)
	end
	return fmt(L[key.."_message_nogroup"], colored)
end

function mod:VoidZone(player, spellID)
	if not player then return end
	if db.voidzonesay and player == pName then
		SendChatMessage(L["voidzonesay_message"], "SAY")
	end
	if db.voidzone then
		self:IfMessage(self:FormatTargetMessage("voidzone", player), "Important", spellID, "Alert")
	end
	if db.voidzoneicon then
		SetRaidTarget(player, 3) -- Diamond
	end
end

local last = 0
function mod:Fiends()
	if db.fiends then
		if phase == 1 then
			local time = GetTime()
			if (time - last) > 5 then
				last = time
				self:Message(L["fiends_message"], "Important", true, nil, nil, 45934)
			end
		elseif phase == 2 then
			self:Message(L["fiends_message"], "Important", true, nil, nil, 45934)
		end
	end
end

function mod:Portals()
	phase = 2

	self:CancelScheduledEvent("VoidWarn")
	self:CancelScheduledEvent("HumanoidWarn")
	self:CancelScheduledEvent("Void")
	self:CancelScheduledEvent("Humanoid")
	self:CancelScheduledEvent("DarknessWarn")
	self:CancelScheduledEvent("DarknessTimer")
	self:CancelScheduledEvent("Darkness15")
	self:CancelScheduledEvent("Darkness10")
	self:CancelScheduledEvent("Darkness10Audio")
	self:CancelScheduledEvent("Darkness5Audio")
	for i = 1, 5 do
		self:CancelScheduledEvent("DarknessCD"..i)
	end
	self:TriggerEvent("BigWigs_StopBar", self, L["void_next"])
	self:TriggerEvent("BigWigs_StopBar", self, L["humanoid_next"])
	self:TriggerEvent("BigWigs_StopBar", self, L["darkness_next"])
	if db.phase then
		self:Message(L["phase2_message"], "Attention")
		self:Bar(entropius, 10, 46087)
	end
	if db.gravity then
		self:Bar(L["gravity_next"], 27, 44218)
	end
end

function mod:GravityBall(player, spellID)
	if not player then return end
	if db.orbsay and player == pName then
		SendChatMessage(L["orbsay_message"], "SAY")
	end
	if db.orb then
		self:IfMessage(self:FormatTargetMessage("orb", player), "Urgent", spellID, "Alert")
	end
	if db.orbicon then
		SetRaidTarget(player, 6) -- Square
	end
	if db.gravity then
		--44218 , looks like a Gravity Balls :p
		self:Bar(L["gravity_next"], 15, 44218)
		self:DelayedMessage(5, L["gravity_soon"], "Urgent")
		self:Bar(L["gravity_spawned"], 10, 44218)
	end
end

function mod:RepeatVoid()
	self:Bar(L["void_next"]:format(voidcount), 30, 46087)
	self:ScheduleEvent("VoidWarn", "BigWigs_Message", 25, L["void_soon"]:format(voidcount), "Attention")
	voidcount = voidcount + 1
	self:ScheduleEvent("Void", self.RepeatVoid, 30, self)
end

function mod:RepeatHumanoid()
	self:Bar(L["humanoid_next"]:format(humanoidcount), 60, 46087)
	self:ScheduleEvent("HumanoidWarn", "BigWigs_Message", 55, L["humanoid_soon"]:format(humanoidcount), "Urgent")
	humanoidcount = humanoidcount + 1
	self:ScheduleEvent("Humanoid", self.RepeatHumanoid, 60, self)
end

function mod:ScheduleDarknessWarnings(delay)
	if db.darkness15 and delay > 15 then
		self:ScheduleEvent("Darkness15", "BigWigs_Message", delay - 15, L["darkness_15"], "Positive")
	end
	if db.darkness10 and delay > 10 then
		self:ScheduleEvent("Darkness10", "BigWigs_Message", delay - 10, L["darkness_10"], "Positive")
	end
	if db.darkness10audio and delay > 11.5 then
		self:ScheduleEvent("Darkness10Audio", "BigWigs_Sound", delay - 11.5, "BigWigs: Muru Darkness 10")
	end
	if db.darkness5audio and delay > 6.5 then
		self:ScheduleEvent("Darkness5Audio", "BigWigs_Sound", delay - 6.5, "BigWigs: Muru Darkness 5")
	end
	if db.darknesscountdown then
		for i = 5, 1, -1 do
			if delay > i then
				self:ScheduleEvent("DarknessCD"..i, "BigWigs_Message", delay - i, L["darkness_countdown"]:format(i), "Positive")
			end
		end
	else
		if delay > 5 then
			self:ScheduleEvent("DarknessWarn", "BigWigs_Message", delay - 5, L["darkness_soon"], "Positive")
		end
	end
end

function mod:RepeatDarkness()
	self:Bar(L["darkness"], 20, 45996)
	self:Bar(L["darkness_next"], 45, 45996)
	self:ScheduleDarknessWarnings(45)
	self:ScheduleEvent("DarknessTimer", self.RepeatDarkness, 45, self)
end

function mod:RepeatTwoMinuteAudio()
	self:TriggerEvent("BigWigs_Sound", "BigWigs: Two Minutes")
	self:ScheduleEvent("BWMuruTwoMinuteAudio", self.RepeatTwoMinuteAudio, 120, self)
end

function mod:BigWigs_RecvSync(sync, rest, nick)
	if self:ValidateEngageSync(sync, rest) and not started then
		started = true
		phase = 1
		voidcount = 1
		humanoidcount = 1
		if self:IsEventRegistered("PLAYER_REGEN_DISABLED") then
			self:UnregisterEvent("PLAYER_REGEN_DISABLED")
		end
		if db.darkness then
			self:Bar(L["darkness_next"], 48, 45996)
			self:ScheduleDarknessWarnings(48)
			self:ScheduleEvent("DarknessTimer", self.RepeatDarkness, 48, self)
		end
		if db.void then
			self:Bar(L["void_next"]:format(voidcount), 30, 46087)
			self:DelayedMessage(25, L["void_soon"]:format(voidcount), "Attention")
			voidcount = voidcount + 1
			self:ScheduleEvent("Void", self.RepeatVoid, 30, self)
		end
		if db.humanoid then
			self:Bar(L["humanoid_next"]:format(humanoidcount), 10, 46087)
			humanoidcount = humanoidcount + 1
			self:ScheduleEvent("Humanoid", self.RepeatHumanoid, 10, self)
		end
		if db.enrage then
			self:Enrage(600)
		end
		if db.twominuteaudio then
			self:ScheduleEvent("BWMuruTwoMinuteAudio", self.RepeatTwoMinuteAudio, 130, self)
		end
	end
end
