-- 유저 디버프 강조 모듈(구 레이드 디버프)
local IRF2 = InvenRaidFrames2

local _G = _G
local floor = _G.floor
local select = _G.select
local strsplit = _G.string.split
local UnitAura = _G.UnitAura
local GetTime = _G.GetTime
local stack, pos
local L = function(id)
	return GetSpellInfo(id) or "*"
end
local mostDebuff = {
	[L(72293)] = true, [L(69278)] = true, [L(70337)] = true, [L(73701)] = true
}
mostDebuff["*"] = nil

local userDebuffHandler = CreateFrame("Frame")

local function timerUpdateFunc(self)
	self.timeLeft = (self.endTime or 0) - GetTime()
	if self.timeLeft >= 0 then
		self:GetParent().text:SetFormattedText("%d", self.timeLeft)
	else
		self:GetParent().text:SetText("")
		self:Hide()
	end
end

local function CreateUserDebuff(object)
	object.userDebuff = CreateFrame("Frame", nil, object)
	object.userDebuff:SetFrameLevel(IRF2.db.frameLevel.userDebuff)
	pos = IRF2:GetFramePoint(IRF2.db.userDebuff.pos)
	object.userDebuff:SetPoint(pos[1], object, pos[2], pos[3])
	object.userDebuff.icon = object.userDebuff:CreateTexture(nil, "OVERLAY")
	object.userDebuff.icon:SetAllPoints(object.userDebuff)
	object.userDebuff.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
	object.userDebuff.count = object.userDebuff:CreateFontString(nil, "OVERLAY", "NumberFontNormalSmall")
	object.userDebuff.count:SetTextColor(1, 1, 1)
	object.userDebuff.count:SetPoint("CENTER", object.userDebuff, "CENTER", 2, -2)
	object.userDebuff.text = object.userDebuff:CreateFontString(nil, "OVERLAY", "NumberFontNormalSmall")
	object.userDebuff.text:SetTextColor(1, 1, 1)
	if (IRF2.db.userDebuff.pos or ""):find("우측") then
		object.userDebuff.text:SetPoint("RIGHT", object.userDebuff, "LEFT", 3, 0)
	else
		object.userDebuff.text:SetPoint("LEFT", object.userDebuff, "RIGHT", -3, 0)
	end
	object.userDebuff.timerUpdate = CreateFrame("Frame", nil, object.userDebuff)
	object.userDebuff.timerUpdate:Hide()
	object.userDebuff.timerUpdate:SetScript("OnUpdate", timerUpdateFunc)
	object.userDebuff:SetWidth(IRF2.db.userDebuff.size)
	object.userDebuff:SetHeight(IRF2.db.userDebuff.size)
	object.userDebuff:SetAlpha(IRF2.db.userDebuff.alpha)
end

local index, name, icon, count, endTime

local function setUserDebuff(object, debuffName, debuffIcon, debuffCount, debuffEndTime)
	if not object.userDebuff then
		CreateUserDebuff(object)
	end
	object.userDebuff.icon:SetTexture(debuffIcon)
	object.userDebuff.count:SetText(debuffCount > 1 and debuffCount or "")
	if not IRF2.db.userDebuff.hideTimer and debuffEndTime then
		object.userDebuff.timerUpdate.endTime = debuffEndTime
		object.userDebuff.timerUpdate:Show()
	else
		object.userDebuff.text:SetText("")
		object.userDebuff.timerUpdate:Hide()
	end
	object.userDebuff:Show()
	object.userDebuffName = debuffName
end

local function userDebuff(object)
	if IRF2.db.userDebuff.pos ~= "표시 안함" then
		stack = nil
		for _, debuff in pairs(mostDebuff) do
			if IRF2.db._userDebuffs[debuff] then
				name, _, icon, count, _, _, endTime = UnitAura(object.vehicleUnit or object.unit, debuff, nil, "HARMFUL")
				if name then
					stack = true
					setUserDebuff(object, name, icon, count, endTime)
					break
				end
			end
		end
		if not stack then
			index = 1
			while true do
				name, _, icon, count, _, _, endTime = UnitAura(object.vehicleUnit or object.unit, index, "HARMFUL")
				if name then
					if IRF2.db._userDebuffs[name] then
						stack = true
						setUserDebuff(object, name, icon, count, endTime)
						break
					end
					index = index + 1
				else
					break
				end
			end
		end
		if not stack and object.userDebuff then
			object.userDebuffName = nil
			object.userDebuff:Hide()
		end
	elseif object.userDebuff then
		object.userDebuffName = nil
		object.userDebuff:Hide()
	end
end

userDebuffHandler:SetScript("OnEvent", function(self, event, unit)
	IRF2:RunUnitFunction(unit, userDebuff)
end)

local function userDebuffMod(object)
	IRF2.objectModules["UserDebuff"] = nil
	userDebuffHandler:RegisterEvent("UNIT_AURA")
	IRF2:RegisterAuraEvent("UserDebuff", userDebuff)
end

IRF2:RegisterObjectModule("UserDebuff", userDebuffMod)
