local _G = _G
local IRF3 = _G[...]
local GetTime = _G.GetTime
local UnitDebuff = _G.UnitDebuff
local PlaySoundFile = _G.PlaySoundFile
local SM = LibStub("LibSharedMedia-3.0")

local SL = IRF3.GetSpellName
local ignoreDebuffs = {
	[SL( 6788)] = true, [SL( 8326)] = true, [SL(11196)] = true, [SL(15822)] = true, [SL(21163)] = true,
	[SL(24360)] = true, [SL(24755)] = true, [SL(25771)] = true, [SL(26004)] = true, [SL(26013)] = true,
	[SL(26680)] = true, [SL(28169)] = true, [SL(28504)] = true, [SL(29232)] = true, [SL(30108)] = true,
	[SL(30451)] = true, [SL(30529)] = true, [SL(36893)] = true, [SL(36900)] = true, [SL(36901)] = true,
	[SL(40880)] = true, [SL(40882)] = true, [SL(40883)] = true, [SL(40891)] = true, [SL(40896)] = true,
	[SL(40897)] = true, [SL(41292)] = true, [SL(41337)] = true, [SL(41350)] = true, [SL(41425)] = true,
	[SL(42164)] = true, [SL(43681)] = true, [SL(55711)] = true, [SL(57723)] = true, [SL(57724)] = true,
	[SL(64805)] = true, [SL(64808)] = true, [SL(64809)] = true, [SL(64810)] = true, [SL(64811)] = true,
	[SL(64812)] = true, [SL(64813)] = true, [SL(64814)] = true, [SL(64815)] = true, [SL(64816)] = true,
	[SL(66233)] = true, [SL(69127)] = true, [SL(69438)] = true, [SL(70402)] = true, [SL(71328)] = true,
	[SL(72144)] = true, [SL(72145)] = true, [SL(72369)] = true,
}
ignoreDebuffs[""] = nil
IRF3.ignoreDebuffs = ignoreDebuffs
local bossDebuffs = {
	[SL(  605)] = true, [SL( 8399)] = true, [SL(11433)] = true, [SL(12294)] = true, [SL(17140)] = true,
	[SL(29879)] = true, [SL(30115)] = true, [SL(30756)] = true, [SL(31306)] = true, [SL(31344)] = true,
	[SL(31347)] = true, [SL(31943)] = true, [SL(31972)] = true, [SL(32779)] = true, [SL(37006)] = true,
	[SL(37027)] = true, [SL(37676)] = true, [SL(38049)] = true, [SL(38246)] = true, [SL(39837)] = true,
	[SL(40239)] = true, [SL(40251)] = true, [SL(40327)] = true, [SL(40481)] = true, [SL(40508)] = true,
	[SL(40585)] = true, [SL(40594)] = true, [SL(40869)] = true, [SL(40932)] = true, [SL(41032)] = true,
	[SL(41472)] = true, [SL(41917)] = true, [SL(42005)] = true, [SL(42783)] = true, [SL(43095)] = true,
	[SL(43149)] = true, [SL(43657)] = true, [SL(44811)] = true, [SL(44867)] = true, [SL(45141)] = true,
	[SL(45150)] = true, [SL(45230)] = true, [SL(45256)] = true, [SL(45324)] = true, [SL(45348)] = true,
	[SL(45641)] = true, [SL(45661)] = true, [SL(45737)] = true, [SL(45996)] = true, [SL(46469)] = true,
	[SL(46771)] = true, [SL(51121)] = true, [SL(55249)] = true, [SL(55550)] = true, [SL(56112)] = true,
	[SL(57975)] = true, [SL(58517)] = true, [SL(59265)] = true, [SL(59847)] = true, [SL(61888)] = true,
	[SL(61903)] = true, [SL(61968)] = true, [SL(61969)] = true, [SL(62130)] = true, [SL(62331)] = true,
	[SL(62526)] = true, [SL(62532)] = true, [SL(62589)] = true, [SL(62661)] = true, [SL(62717)] = true,
	[SL(63018)] = true, [SL(63276)] = true, [SL(63355)] = true, [SL(63498)] = true, [SL(63666)] = true,
	[SL(63830)] = true, [SL(64234)] = true, [SL(64292)] = true, [SL(64396)] = true, [SL(64705)] = true,
	[SL(64771)] = true, [SL(65121)] = true, [SL(65292)] = true, [SL(65598)] = true, [SL(66013)] = true,
	[SL(66869)] = true, [SL(67049)] = true, [SL(67283)] = true, [SL(67298)] = true, [SL(67472)] = true,
	[SL(67478)] = true, [SL(67574)] = true, [SL(67618)] = true, [SL(68125)] = true, [SL(69065)] = true,
	[SL(69200)] = true, [SL(69240)] = true, [SL(69278)] = true, [SL(69409)] = true, [SL(69483)] = true,
	[SL(69674)] = true, [SL(70126)] = true, [SL(70337)] = true, [SL(70447)] = true, [SL(70541)] = true,
	[SL(70672)] = true, [SL(70867)] = true, [SL(71204)] = true, [SL(71289)] = true, [SL(71330)] = true,
	[SL(71340)] = true, [SL(72004)] = true, [SL(72219)] = true, [SL(72293)] = true, [SL(72385)] = true,
	[SL(72408)] = true, [SL(72451)] = true, [SL(72650)] = true,
}
bossDebuffs[""] = nil
IRF3.bossDebuffs = bossDebuffs

local dispelTypes = { Magic = "Magic", Curse = "Curse", Disease = "Disease", Poison = "Poison" }
local name, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, spellId, canApplyAura, isBossDebuff
local lastTime, debuffIcon = 0

local function hideIcon(icon)
	icon:SetSize(0.001, 0.001)
	icon:Hide()
end

local function updateDispel(self, debuffType)
	self.dispelType = debuffType
	if self.optionTable.dispelSound ~= "None" then
		if GetTime() > lastTime then
			lastTime = GetTime() + self.optionTable.dispelSoundDelay
			PlaySoundFile(SM:Fetch("sound", self.optionTable.dispelSound))
		end
	end
end

local function isUsableDebuff(debuff)
	if IRF3.db.ignoreDebuffs[debuff] then
		return nil
	elseif ignoreDebuffs[debuff] and IRF3.db.ignoreDebuffs[debuff] ~= false then
		return nil
	end
	return true
end

local function checkBossDebuff(debuff, blizz)
	if IRF3.db.userDebuffs[debuff] then
		return true
	elseif IRF3.db.userDebuffs[debuff] ~= false then
		if bossDebuffs[debuff] then
			return true
		elseif blizz then
			bossDebuffs[debuff] = true
			IRF3.db.userDebuffs[debuff] = true
			IRF3:Message(("새로운 중요 디버프 \"%s\"|1이;가; 발견되어 목록에 추가했습니다."):format(debuff))
			return true
		end
	end
	return nil
end

function InvenRaidFrames3Member_UpdateDebuffs(self)
	self.bossDebuff.index = nil
	self.numDebuffIcons = 0
	for i = 1, 40 do
		name, _, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, _, spellId, canApplyAura, isBossDebuff = UnitDebuff(self.displayedUnit, i)
		if name then
			if isUsableDebuff(name) then
				debuffType = dispelTypes[debuffType] or "none"
				if self.optionTable.useBossDebuff and not self.bossDebuff.index and checkBossDebuff(name, isBossDebuff) then
					-- 사용자 디버프
					self.bossDebuff.index = i
					self.bossDebuff:SetSize(self.optionTable.bossDebuffSize, self.optionTable.bossDebuffSize)
					self.bossDebuff.icon:SetTexture(icon)
					self.bossDebuff.count:SetText(count and count > 1 and count or nil)
					if duration and duration > 0 and self.optionTable.bossDebuffTimer then
						self.bossDebuff.cooldown:SetCooldown(expirationTime - duration, duration)
						self.bossDebuff.cooldown:Show()
					else
						self.bossDebuff.cooldown:Hide()
					end
					self.bossDebuff:Show()
				elseif self.optionTable.debuffIconFilter[debuffType] and self.optionTable.debuffIcon > self.numDebuffIcons then
					-- 디버프 아이콘
					self.numDebuffIcons = self.numDebuffIcons + 1
					debuffIcon = self["debuffIcon"..self.numDebuffIcons]
					debuffIcon:SetSize(self.optionTable.debuffIconSize, self.optionTable.debuffIconSize)
					if IRF3.db.colors[debuffType] then
						debuffIcon.color:SetTexture(IRF3.db.colors[debuffType][1], IRF3.db.colors[debuffType][2], IRF3.db.colors[debuffType][3])
					else
						debuffIcon.color:SetTexture(0, 0, 0)
					end
					debuffIcon.icon:SetTexture(icon)
					debuffIcon.count:SetText(count and count > 1 and count or nil)
					debuffIcon:Show()
					debuffIcon = nil
				end
			end
		else
			break
		end
	end
	if not self.bossDebuff.index then
		hideIcon(self.bossDebuff)
	end
	for i = self.numDebuffIcons + 1, 5 do
		hideIcon(self["debuffIcon"..i])
	end
	InvenRaidFrames3Pet_UpdateDebuffs(self)
end

function InvenRaidFrames3Pet_UpdateDebuffs(self)
	self.dispelType = nil
	for i = 1, 16 do
		name, _, _, _, debuffType = UnitDebuff(self.displayedUnit, i, "RAID")
		if name then
			if isUsableDebuff(name) then
				self.dispelType = dispelTypes[debuffType] or "none"
				if self.optionTable.dispelSound ~= "None" then
					if GetTime() > lastTime then
						lastTime = GetTime() + self.optionTable.dispelSoundDelay
						PlaySoundFile(SM:Fetch("sound", self.optionTable.dispelSound))
					end
				end
				break
			end
		else
			break
		end
	end
end

function InvenRaidFrames3Member_BossDebuffOnLoad(self)
	self.bg:SetTexture(0, 0, 0)
	self.cooldown.noCooldownCount = true
end