local mod	= DBM:NewMod(2657, "DBM-Party-Midnight", 1, 1299)
--local L		= mod:GetLocalizedStrings()--Nothing to localize for blank mods

mod:SetRevision("20260523021914")
mod:SetCreatureID(231631)
mod:SetEncounterID(3058)
--mod:SetHotfixNoticeRev(20250823000000)
--mod:SetMinSyncRevision(20250823000000)
mod:SetZone(2805)
mod.respawnTime = 29

mod:RegisterCombat("combat")

local warnRecklessLeap				= mod:NewCountAnnounce(1283247, 2)
local warnBladestorm				= mod:NewCountAnnounce(470966, 2, nil, false)

local specWarnRampage				= mod:NewSpecialWarningCount(467620, nil, nil, nil, 1, 2, nil, nil, "defensive")
local specWarnIntimidatingShout		= mod:NewSpecialWarningCount(1253026, nil, nil, nil, 2, 2, nil, nil, "gathershare")
local specWarnRallyingBellow		= mod:NewSpecialWarningSwitchCount(472043, nil, nil, DBM_COMMON_L.ADDS, 2, 3, nil, nil, "mobsoon")
local specWarnRecklessLeap			= mod:NewSpecialWarningBlizzYou(1283247, nil, nil, nil, 1, 2, nil, nil, "runout")

local timerRampageCD				= mod:NewCDCountTimer("d30", 467620, nil, "Tank", nil, 5, nil, DBM_COMMON_L.TANK_ICON)
local timerIntimidatingShoutCD		= mod:NewCDCountTimer("d45", 1253026, nil, nil, nil, 2, nil, DBM_COMMON_L.MYTHIC_ICON)
local timerRecklessLeapCD			= mod:NewCDCountTimer("d37", 1283247, nil, nil, nil, 3)
local timerBladestormCD				= mod:NewCDCountTimer(8, 470966, nil, nil, nil, 2)

mod:AddPrivateAuraSoundOption(470966, true, 470966, 4, 1, "justrun", 2)--Bladestorm target
--mod:AddPrivateAuraSoundOption(468924, true, 470966, 1, 2, "watchfeet", 8)--Bladestorm GTFO
--mod:AddPrivateAuraSoundOption(1283247, true, 1283247, 1, 1, "runout", 2)--Reckless Leap target

mod.vb.rampageCount = 0
mod.vb.intimidatingShoutCount = 0
mod.vb.recklessLeapCount = 0
mod.vb.bladestormCount = 0
mod.vb.rallyingBellowCount = 0

local badStateDetected = false
local activeEventTypes = {}
local lastRallyingBellow = 0

---@param self DBMMod
---@param dontSetAlerts boolean? Called on engage when we only want to set timeline parameters and not touch encounter alerts
local function setFallback(self, dontSetAlerts)
	if not dontSetAlerts then
		if self:IsTank() then
			specWarnRampage:SetAlert({210, 556}, "defensive", 2)
		end
		specWarnIntimidatingShout:SetAlert({211, 213}, "gathershare", 2)
		specWarnRallyingBellow:SetAlert(215, "mobsoon", 2, 3, 0)
		specWarnRecklessLeap:SetAlert({212, 214}, "runout", 2, 2, 0)
	end
	local onlyColor = not DBM.Options.HideDBMBars
	timerRampageCD:SetTimeline({210, 556}, onlyColor)
	timerIntimidatingShoutCD:SetTimeline({211, 213}, onlyColor)
	timerRecklessLeapCD:SetTimeline({212, 214}, onlyColor)
	timerBladestormCD:SetTimeline(216, onlyColor)
end

function mod:OnLimitedCombatStart()
	self:TLCountReset()
	self.vb.rampageCount = 1
	self.vb.intimidatingShoutCount = 1
	self.vb.recklessLeapCount = 1
	self.vb.bladestormCount = 1
	self.vb.rallyingBellowCount = 0
	badStateDetected = false
	activeEventTypes = {}
	lastRallyingBellow = 0
	if DBM.Options.HardcodedTimer and not badStateDetected then
		self:IgnoreBlizzardAPI()
		self:RegisterShortTermEvents(
			"ENCOUNTER_TIMELINE_EVENT_ADDED",
			"ENCOUNTER_TIMELINE_EVENT_STATE_CHANGED"
		)
		setFallback(self, true)
	else
		setFallback(self)
	end
end

function mod:OnCombatEnd()
	self:TLCountReset()
	activeEventTypes = {}
	self:UnregisterShortTermEvents()
end

do
	---@param self DBMMod
	---@param timer number
	---@param timerExact number
	---@param eventID number
	local function timersAll(self, timer, timerExact, eventID)
		--Confirmed against logged M+ pulls.
		--This fight emits >900 second placeholder bars after real casts; ignore them in hardcode.
		if timer > 900 then
			return
		elseif timerExact < 1 then
			--Bladestorm startup artifacts show up as 0.001s events before the real 8s cadence.
			return
		elseif timer == 3 or timer == 30 then
			activeEventTypes[eventID] = "rampage"
			timerRampageCD:TLStart(timerExact, eventID, self:TLCountStart(eventID, "rampage", "rampageCount"))
		elseif timer == 18 or timer == 45 then
			activeEventTypes[eventID] = "intimidatingShout"
			timerIntimidatingShoutCD:TLStart(timerExact, eventID, self:TLCountStart(eventID, "intimidatingShout", "intimidatingShoutCount"))
		elseif timer == 10 or timer == 37 then
			activeEventTypes[eventID] = "recklessLeap"
			timerRecklessLeapCD:TLStart(timerExact, eventID, self:TLCountStart(eventID, "recklessLeap", "recklessLeapCount"))
		elseif timer == 8 or timer == 5 then
			activeEventTypes[eventID] = "bladestorm"
			timerBladestormCD:TLStart(timerExact, eventID, self:TLCountStart(eventID, "bladestorm", "bladestormCount"))
		else
			badStateDetected = true
			self:ResumeBlizzardAPI()
			self:UnregisterShortTermEvents()
			setFallback(self)
			DBM:Debug("|cffff0000Failed to match encounter timeline events to expected timers, falling back to Blizzard API|r", nil, nil, nil, true)
		end
	end

	function mod:ENCOUNTER_TIMELINE_EVENT_ADDED(eventInfo)
		if eventInfo.source ~= 0 then return end
		local eventID = eventInfo.id
		local timerExact = eventInfo.duration
		local timer = math.floor(timerExact + 0.5)
		if not badStateDetected then
			timersAll(self, timer, timerExact, eventID)
		end
	end

	function mod:ENCOUNTER_TIMELINE_EVENT_STATE_CHANGED(eventID)
		local eventState = C_EncounterTimeline.GetEventState(eventID)
		if not eventID or not eventState then return end
		local eventType = activeEventTypes[eventID]
		if eventState == 1 then
			if eventType and eventType ~= "bladestorm" and GetTime() - lastRallyingBellow > 5 then
				lastRallyingBellow = GetTime()
				self.vb.rallyingBellowCount = self.vb.rallyingBellowCount + 1
				specWarnRallyingBellow:Show(self.vb.rallyingBellowCount)
				specWarnRallyingBellow:Play("mobsoon")
				--Bladestorm always begins 4 seconds after rally
				timerBladestormCD:Start(4, self.vb.bladestormCount)
			end
		elseif eventState == 2 then
			activeEventTypes[eventID] = nil
			local finishedEventType, eventCount = self:TLCountFinish(eventID)
			if finishedEventType and eventCount then
				if finishedEventType == "rampage" then
					if self:IsTank() then
						specWarnRampage:Show(eventCount)
						specWarnRampage:Play("defensive")
					end
				elseif finishedEventType == "intimidatingShout" then
					specWarnIntimidatingShout:Show(eventCount)
					specWarnIntimidatingShout:Play("gathershare")
				elseif finishedEventType == "recklessLeap" then
					warnRecklessLeap:Show(eventCount)
					specWarnRecklessLeap:Show(eventCount, "runout", 5)
				elseif finishedEventType == "bladestorm" then
					warnBladestorm:Show(eventCount)
				end
			end
		elseif eventState == 3 then
			activeEventTypes[eventID] = nil
			self:TLCountCancel(eventID)
		end
	end
end
