-- 해제 가능 디버프시 소리 재생
local IRF2 = InvenRaidFrames2

local SM = LibStub("LibSharedMedia-3.0")
local _G = _G
local UnitGUID = _G.UnitGUID
local GetTime = _G.GetTime
local UnitAura = _G.UnitAura
local UnitIsVisible = _G.UnitIsVisible
local UnitIsUnit = _G.UnitIsUnit
local UnitPlayerOrPetInParty = _G.UnitPlayerOrPetInParty
local UnitPlayerOrPetInRaid = _G.UnitPlayerOrPetInRaid
local PlaySoundFile = _G.PlaySoundFile

local debuffTimer, debuff, guid = 0
local unitLastDebuff = {}

local debuffSoundHandler = CreateFrame("Frame")

debuffSoundHandler:SetScript("OnEvent", function(self, event, unit)
	if InvenRaidFrames2CharDB.sound.debuffGain ~= "None" and IRF2.units[unit] and IRF2:RaidFrameIsShown() and debuffTimer <= GetTime() then
		debuff = UnitAura(unit, 1, "HARMFUL|RAID")
		guid = UnitGUID(unit)
		if debuff ~= unitLastDebuff[guid] then
			unitLastDebuff[guid] = debuff
			if debuff and IRF2.db._ignoreDebuff[debuff] == nil then
				debuffTimer = GetTime() + 3
				PlaySoundFile(SM:Fetch("sound", InvenRaidFrames2CharDB.sound.debuffGain))
			end
		end
	end
end)

local function debuffSoundMod(object)
	IRF2.objectModules["DebuffSound"] = nil
	debuffSoundHandler:RegisterEvent("UNIT_AURA")
end

IRF2:RegisterObjectModule("DebuffSound", debuffSoundMod)