-- 디버프 아이콘 표시 모듈(최대 5개까지 지원)
local IRF2 = InvenRaidFrames2

local _G = _G
local unpack = _G.unpack
local UnitAura = _G.UnitAura

local idx, pos, stack
local reversePos = { TOPLEFT = "TOPRIGHT", TOPRIGHT = "TOPLEFT", LEFT = "RIGHT", RIGHT = "LEFT", BOTTOMLEFT = "BOTTOMRIGHT", BOTTOMRIGHT = "BOTTOMLEFT" }

local debuffHandler = CreateFrame("Frame")

local function createDebuffFrame(object)
	object.debuffFrame = CreateFrame("Frame", nil, object)
	object.debuffFrame:SetFrameLevel(IRF2.db.frameLevel.debuff)
	object.debuffFrame:SetWidth(0.001)
	object.debuffFrame:SetHeight(IRF2.db.debuff.size)
	pos = IRF2:GetFramePoint(IRF2.db.debuff.pos)[1]
	object.debuffFrame:SetPoint(pos, object, pos)
	for i = 1, 5 do
		object.debuffFrame[i] = CreateFrame("Frame", nil, object.debuffFrame)
		object.debuffFrame[i]:SetWidth(0.001)
		object.debuffFrame[i]:SetHeight(IRF2.db.debuff.size)
		object.debuffFrame[i].color = object.debuffFrame[i]:CreateTexture(nil, "ARTWORK")
		object.debuffFrame[i].color:SetTexture(1, 1, 1)
		object.debuffFrame[i].icon = object.debuffFrame[i]:CreateTexture(nil, "OVERLAY")
		object.debuffFrame[i].icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
		object.debuffFrame[i].count = object.debuffFrame[i]:CreateFontString(nil, "OVERLAY", "NumberFontNormalSmall")
		object.debuffFrame[i].count:SetPoint("CENTER", object.debuffFrame[i], "CENTER")
		if IRF2.db.debuff.iconType == "아이콘" then
			object.debuffFrame[i].color:SetAllPoints(object.debuffFrame[i])
			object.debuffFrame[i].color:Hide()
			object.debuffFrame[i].icon:SetAllPoints(object.debuffFrame[i])
			object.debuffFrame[i].icon:Show()
		elseif IRF2.db.debuff.iconType == "색상" then
			object.debuffFrame[i].color:SetPoint("TOPLEFT", object.debuffFrame[i], "TOPLEFT", 1, -1)
			object.debuffFrame[i].color:SetPoint("BOTTOMRIGHT", object.debuffFrame[i], "BOTTOMRIGHT", -1, 1)
			object.debuffFrame[i].color:Show()
			object.debuffFrame[i].icon:SetAllPoints(object.debuffFrame[i])
			object.debuffFrame[i].icon:Hide()
		else
			object.debuffFrame[i].color:SetAllPoints(object.debuffFrame[i])
			object.debuffFrame[i].color:Show()
			object.debuffFrame[i].icon:SetPoint("TOPLEFT", object.debuffFrame[i], "TOPLEFT", 1, -1)
			object.debuffFrame[i].icon:SetPoint("BOTTOMRIGHT", object.debuffFrame[i], "BOTTOMRIGHT", -1, 1)
			object.debuffFrame[i].icon:Show()
		end
		if i == 1 then
			object.debuffFrame[i]:SetPoint("LEFT", object.debuffFrame, "LEFT")
		else
			object.debuffFrame[i]:SetPoint("LEFT", object.debuffFrame[i - 1], "RIGHT")
		end
	end
end

local index, name, icon, count, debuffType

local function setDebuff(object)
	if IRF2.db.debuff.use then
		index = 1
		stack = 0
		while true do
			name, _, icon, count, debuffType = UnitAura(object.vehicleUnit or object.unit, index, "HARMFUL")
			if name and IRF2.db.debuff.num >= (stack + 1) then
				debuffType = debuffType or "Normal"
				if not IRF2.db._ignoreDebuff[name] and InvenRaidFrames2CharDB.debuff[debuffType] then
					if not object.debuffFrame then
						createDebuffFrame(object)
					end
					stack = stack + 1
					object.debuffFrame[stack].icon:SetTexture(icon)
					object.debuffFrame[stack].color:SetVertexColor(unpack(IRF2.db.debuffColor[debuffType]))
					object.debuffFrame[stack].count:SetText(count > 1 and count or " ")
					object.debuffFrame[stack]:SetWidth(IRF2.db.debuff.size)
					object.debuffFrame[stack]:Show()
				end
				index = index + 1
			else
				break
			end
		end
		if object.debuffFrame then
			object.debuffFrame:SetWidth(stack == 0 and 0.001 or (IRF2.db.debuff.size * stack))
			stack = stack + 1
			for i = stack, 5 do
				object.debuffFrame[i]:Hide()
				object.debuffFrame[i]:SetWidth(0.001)
			end
		end
	end
end

debuffHandler:SetScript("OnEvent", function(self, event, unit)
	IRF2:RunUnitFunction(unit, setDebuff)
end)

local function debuffMod(object)
	IRF2.objectModules["Debuff"] = nil
	debuffHandler:RegisterEvent("UNIT_AURA")
	IRF2:RegisterAuraEvent("Debuff", setDebuff)
end

IRF2:RegisterObjectModule("Debuff", debuffMod)