-- 디버프 색상 표시 모듈
local IRF2 = InvenRaidFrames2

local _G = _G
local unpack = _G.unpack
local UnitAura = _G.UnitAura
local UnitEXists = _G.UnitExists
local stack, index, name, debuffType

local debuffColorHandler = CreateFrame("Frame")

local function findDebuff(unit, filter)
	if unit and UnitExists(unit) then
		index = 1
		while true do
			name, _, _, _, debuffType = UnitAura(unit, index, filter)
			if name then
				debuffType = debuffType or "Normal"
				if not IRF2.db._ignoreDebuff[name] and InvenRaidFrames2CharDB.dispel[debuffType] then
					return debuffType
				end
				index = index + 1
			else
				break
			end
		end
	end
	return nil
end

local function debuffColor(object)
	stack = findDebuff(object.vehicleUnit or object.unit, "HARMFUL|RAID") or findDebuff(object.vehicleUnit or object.unit, "HARMFUL")
	if stack then
		if object.debuffType ~= stack then
			object.debuffType = stack
			if IRF2.db.debuffColor.changeHealthColor then
				object:UpdateHealthBarColor()
			end
			if IRF2.db.outline == "디버프" then
				object:UpdateOutline()
			end
		end
	else
		object.debuffType = nil
		if IRF2.db.debuffColor.changeHealthColor then
			object:UpdateHealthBarColor()
		end
		if IRF2.db.outline == "디버프" then
			object:UpdateOutline()
		end
	end
end

debuffColorHandler:SetScript("OnEvent", function(self, event, unit)
	IRF2:RunUnitFunction(unit, debuffColor)
end)

local function debuffColorMod(object)
	IRF2.objectModules["DebuffColor"] = nil
	debuffColorHandler:RegisterEvent("UNIT_AURA")
	IRF2:RegisterAuraEvent("DebuffColor", debuffColor)
end

IRF2:RegisterObjectModule("DebuffColor", debuffColorMod)