---@diagnostic disable: undefined-global
local Brutallus = DBM:NewBossMod("Brutallus", DBM_BRUTALLUS_NAME, DBM_BRUTALLUS_DESCRIPTION, DBM_SUNWELL, DBM_SW_TAB, 2)

Brutallus.Version		= "1.0"
Brutallus.Author		= "Tandanu"
Brutallus.MinRevision	= 954

Brutallus:RegisterCombat("YELL", DBM_BRUTALLUS_YELL_PULL)

Brutallus:RegisterEvents(
	"SPELL_AURA_APPLIED",
	"SPELL_CAST_START",
	"CHAT_MSG_ADDON"
)

Brutallus:AddOption("WarnBurn", true, DBM_BRUTALLUS_OPTION_BURN)
Brutallus:AddOption("WarnBurn2", false, DBM_BRUTALLUS_OPTION_BURN2)
--Brutallus:AddOption("BurnIcon", false, DBM_BRUTALLUS_OPTION_BURN3)
Brutallus:AddOption("BurnSpecWarn", true, DBM_BRUTALLUS_OPTION_BURN4)
Brutallus:AddOption("WarnStomp", true, DBM_BRUTALLUS_OPTION_STOMP)
Brutallus:AddOption("WarnArmageddon", true, "Announce Armageddon")
Brutallus:AddOption("WarnDoomfire", true, "Announce Doomfire")
Brutallus:AddOption("PreWarnStomp", false, DBM_BRUTALLUS_OPTION_PRESTOMP)
Brutallus:AddOption("WarnMeteor", false, "Announce Meteor Slash")
Brutallus:AddOption("DelayedBurnTimer", false, DBM_BRUTALLUS_OPTION_DEL_BURN)
Brutallus:AddOption("DelayedBurnWarn", false, DBM_BRUTALLUS_OPTION_DEL_BURN2)
Brutallus:AddOption("ArmageddonIcon", false, "Set icon on Armageddon target")
Brutallus:AddOption("DoomfireIcon", false, "Set icon on Doomfire target")

Brutallus:AddBarOption("Enrage")
Brutallus:AddBarOption("Burn: (.*)")
Brutallus:AddBarOption("Jumped Burn: (.*)", false)
Brutallus:AddBarOption("Next Burn")
Brutallus:AddBarOption("Next Meteor Slash")
Brutallus:AddBarOption("Next Stomp")
Brutallus:AddBarOption("Next Armageddon")
Brutallus:AddBarOption("Armageddon lands")
Brutallus:AddBarOption("Next Doomfire")

local icons = {}
local lastBurn = 0

local function clearIcon(id)
	icons[id] = nil
end

function Brutallus:OnCombatStart(delay)
	for i, v in pairs(icons) do
		icons[i] = nil
	end

	self:StartStatusBarTimer(360 - delay, "Enrage", "Interface\\Icons\\Spell_Shadow_UnholyFrenzy")
	self:ScheduleAnnounce(180 - delay, DBM_GENERIC_ENRAGE_WARN:format(3, DBM_MIN), 1)
	self:ScheduleAnnounce(300 - delay, DBM_GENERIC_ENRAGE_WARN:format(1, DBM_MIN), 2)
	self:ScheduleAnnounce(330 - delay, DBM_GENERIC_ENRAGE_WARN:format(30, DBM_SEC), 3)
	self:ScheduleAnnounce(350 - delay, DBM_GENERIC_ENRAGE_WARN:format(10, DBM_SEC), 4)
	self:StartStatusBarTimer(31 - delay, "Next Stomp", 45185)
	self:StartStatusBarTimer(19 - delay, "Next Doomfire", "Interface\\Icons\\Spell_Fire_Incinerate")
	self:StartStatusBarTimer(39 - delay, "Next Armageddon", "Interface\\Icons\\Spell_Fire_SelfDestruct")
end

function Brutallus:OnEvent(event, args)
	if event == "SPELL_AURA_APPLIED" then
		if args.spellId == 46394 then
			self:SendSync("Burn"..tostring(args.destName))
		elseif args.spellId == 45185 then
			self:SendSync("Stomp"..tostring(args.destName))
		elseif args.spellId == 20478 then
			if self.Options.WarnArmageddon then
				self:Announce("Armageddon: "..args.destName, 1)
			end
			self:StartStatusBarTimer(38, "Next Armageddon", "Interface\\Icons\\Spell_Fire_SelfDestruct")
			self:StartStatusBarTimer(9, "Armageddon lands", 45915)
			if self.Options.ArmageddonIcon then
				self:SetIcon(args.destName, 9, 2)
			end
		elseif args.spellId == 31944 then
			--there are two different doomfire debuffs in this fight, but they have the same spellId.
			--only the player's client can figure out which version of the debuff it is by checking the duration of the debuff.
			if args.destName == UnitName("player") then
				--loop through debuffs and check max duration
				for i = 1,10 do
					local name,_,_,_,_,duration = UnitDebuff("player", i)
					if name ~= nil and duration ~= nil then
						if name == "Doomfire" and duration < 35 then
							SendAddonMessage("DBM_BRUT", args.destName, "RAID")
						end
					end
				end
			end
		end
	elseif event == "SPELL_CAST_START" then
		if args.spellId == 45150 then
			self:SendSync("Meteor")
		end
	elseif event == "CHAT_MSG_ADDON" then
		if arg1 ~= "DBM_BRUT" then return end
		local destName = arg2

		if self.Options.WarnDoomfire then
			self:Announce("Doomfire: "..destName, 1)
		end
		self:StartStatusBarTimer(38, "Next Doomfire", "Interface\\Icons\\Spell_Fire_Incinerate")

		if self.Options.DoomfireIcon then
			if icons[8] == nil then
				icons[8] = true
				self:SetIcon(destName, 15, 8)
				self:Schedule(15, clearIcon, 8)
			elseif icons[7] == nil then
				icons[7] = true
				self:SetIcon(destName, 15, 7)
				self:Schedule(15, clearIcon, 7)
			end
		end
	end
end

function Brutallus:OnSync(msg)
	if msg:sub(0, 4) == "Burn" then
		msg = msg:sub(5)
		if msg == UnitName("player") and self.Options.BurnSpecWarn then
			self:AddSpecialWarning(DBM_BRUTALLUS_WHISP_BURN)
		end

		local firstBurn = false
		if (GetTime() - lastBurn) > 19 then
			lastBurn = GetTime()
			firstBurn = true
		end

		if (self.Options.WarnBurn and firstBurn) or self.Options.WarnBurn2 then
			self:Announce(DBM_BRUTALLUS_WARN_BURN:format(msg), 2)
			self:SendHiddenWhisper(DBM_BRUTALLUS_WHISP_BURN, msg)
		end

		self:ScheduleMethod(45, "DelayedBurn", msg)

		if firstBurn then
			if not self.Options.DelayedBurnTimer then
				self:StartStatusBarTimer(60, "Burn: "..msg, 46394, true)
			end
			self:StartStatusBarTimer(20, "Next Burn", "Interface\\Icons\\Spell_Fire_FelFire")
		else
			if not self.Options.DelayedBurnTimer then
				self:StartStatusBarTimer(60, "Jumped Burn: "..msg, 46394, true)
			end
		end
	elseif msg:sub(0, 5) == "Stomp" then
		msg = msg:sub(6)
		if self.Options.WarnStomp then
			self:Announce(DBM_BRUTALLUS_WARN_STOMP:format(msg), 1)
		end
		if self.Options.PreWarnStomp then
			self:ScheduleAnnounce(26, DBM_BRUTALLUS_WARN_STOMP_SOON, 1)
		end
		self:StartStatusBarTimer(31, "Next Stomp", 45185)

	elseif msg == "Meteor" then
		if self.Options.WarnMeteor then
			self:Announce("Meteor Slash", 3)
		end
		self:StartStatusBarTimer(15, "Next Meteor Slash", 45150)
	end
end

function Brutallus:DelayedBurn(target)
	for i = 1, GetNumRaidMembers() do
		if UnitName("raid"..i) == target then
			if not DBM.GetDebuff("raid"..i, GetSpellInfo(46394)) then
				return
			end
			break
		end
	end
	if self.Options.DelayedBurnWarn then
		self:Announce(DBM_BRUTALLUS_WARN_DEL_BURN:format(target), 4)
	end
	if self.Options.DelayedBurnTimer then
		self:StartStatusBarTimer(15, "Burn: "..target, 46394, true)
	end
end
