local mod	= DBM:NewMod("Rings", "DBM-WorldEvents", 3)
local L		= mod:GetLocalizedStrings()

mod:SetRevision("20260201224435")
mod:SetZone(974)

mod:RegisterCombat("combat")

if DBM:IsPostMidnight() then
	mod:RegisterSafeEvents(
		"UNIT_AURA player"
	)
else
	mod:RegisterEvents(
		"SPELL_AURA_APPLIED 170820 170823",
		"SPELL_AURA_APPLIED_DOSE 170823",
		"SPELL_AURA_REMOVED 170820"
	)
end
mod.noStatistics = true

local warnRings		= mod:NewCountAnnounce(170823, 1, nil, false)--Spammy, so off by default, but requested because blizz bug, ring does not always make sound when passing through so this alert can serve as confirmation sound

local timerGame		= mod:NewBuffActiveTimer(10, 170820, nil, nil, nil, 5, nil, nil, nil, 1, 5)

do
	local function checkBuff()
		local name, _, _, _, _, expires, _, _, _, spellId = DBM:UnitBuff("player", 170820)
		if name and spellId == 170820 then
			local time = expires-GetTime()
			timerGame:Stop()
			timerGame:Start(time)
		end
	end

	function mod:SPELL_AURA_APPLIED(args)
		local spellId = args.spellId
		if spellId == 170823 and args:IsPlayer() then
			warnRings:Show(args.amount or 1)
			self:Unschedule(checkBuff)
			self:Schedule(0.2, checkBuff)
		end
	end
	mod.SPELL_AURA_APPLIED_DOSE = mod.SPELL_AURA_APPLIED

	--"<111.5 20:41:44> [CLEU] SPELL_AURA_REMOVED#false#Player-55-07DC716F#Judgementál#1304#0#Player-55-07DC716F#Judgementál#1304#0#170820#Wings of Flame#1#BUFF", -- [150]
	--"<111.5 20:41:44> [CLEU] SPELL_AURA_APPLIED#false#Player-55-07DC716F#Judgementál#1304#0#Player-55-07DC716F#Judgementál#1304#0#170838#Slow Fall#64#BUFF", -- [151]
	function mod:SPELL_AURA_REMOVED(args)
		if args.spellId == 170820 and args:IsPlayer() then
			self:Unschedule(checkBuff)
			timerGame:Cancel()
		end
	end
end

do
	local gameActive = false
	local lastCount = 0
	function mod:UNIT_AURA()
		local name, _, count, _, _, expires = DBM:UnitBuff("player", 170820)
		if self:issecretvalue(name) then return end
		if name and not gameActive then
			timerGame:Start(expires-GetTime())
			gameActive = true
			lastCount = count or 1
		elseif name and gameActive and lastCount ~= count then
			--Stack count changed
			lastCount = count or 0
			warnRings:Show(lastCount)
			timerGame:Stop()
			timerGame:Start(expires-GetTime())
		elseif not name and gameActive then
			timerGame:Cancel()
			gameActive = false
			lastCount = 0
		end
	end
end
