IRF_Debuff = LibStub("AceAddon-3.0"):NewAddon("IRF_Debuff", "AceEvent-3.0")
local self = IRF_Debuff
local mother = InvenRaidFrames
local pos = { ["상단"] = "TOP", ["중앙"] = "CENTER", ["하단"] = "BOTTOM", ["좌측"] = "LEFT", ["좌측 상단"] = "TOPLEFT", ["좌측 하단"] = "BOTTOMLEFT", ["우측"] = "RIGHT", ["우측 상단"] = "TOPRIGHT", ["우측 하단"] = "BOTTOMRIGHT" }
self.vars = { frame = nil, unit = {} }

local UnitDebuff = UnitDebuff
local tonumber = tonumber
local pairs = pairs
local unpack = unpack

function IRF_Debuff:OnInitialize()
	local f = CreateFrame("Frame", nil, UIParent)
	f:SetAlpha(mother.db.profile.debuffAlpha2)
	f:Show()
	self.vars.frame = f
	self:RegisterEvent("UNIT_AURA", function(event, unit) self:Update(unit) end)
	self:RegisterMessage("InvenRaidFrames_InitUnits", function() self:Update() end)
	self:RegisterMessage("InvenRaidFrames_UpdateScale", "SetScale")
	self:RegisterMessage("InvenRaidFrames_HideAll", function()
		for u in pairs(self.vars.unit) do
			self:ClearIcon(u)
		end
	end)
	self:SetScale()
	self:SetLocation()
end

do

function IRF_Debuff:Update(unit)
	if unit then
		local f = mother:GetUnit(unit)
		local id =  1
		local index = 1
		local n = tonumber(mother.db.profile.debuffnum)
		if mother.db.profile.usedebuff then
			while true do
				local name, _, icon, count, dtype = UnitDebuff(unit, id)
				if not name or index > n then break end
				dtype = dtype or "Normal"
				if InvenRaidFramesDispelDB[dtype] and not mother.vars.skipdebuff[name] and not(mother.vars.classDB.importantDebuff[name] and mother.db.profile.raiddebuff) then
					if self:SetIcon(unit, index, icon, count, dtype) then
						index = index + 1
					end
				end
				id = id + 1
			end
		end
		for i = index, 4 do
			self:ClearIcon(unit, i)
		end
		self:SetLocation(unit)
	else
		for u in pairs(self.vars.unit) do
			if not mother.vars.unit[u] then
				self:ClearIcon(unit)
			end
		end
		for u in pairs(mother.vars.unit) do
			self:Update(u)
		end
	end
end

function IRF_Debuff:SetIcon(unit, id, icon, count, dtype)
	local f = mother:GetUnit(unit)
	if f and f.raidDebuff:GetTexture() ~= icon then
		local d = self:Create(unit)
		d.update:Show()
		local r, g, b = unpack(mother.db.profile.color[dtype])
		if mother.db.profile.debufftype == "디버프 아이콘" then
			d[id]:SetTexture(icon)
			d[id]:SetVertexColor(1, 1, 1)
			d[id]:Show()
			if mother.db.profile.debuffcount then
				if not count or (count and count < 2) then
					d[id].count:SetText("")
				else
					d[id].count:SetText(count)
				end
				d[id].count:Show()
			else
				d[id].count:SetText("")
				d[id].count:Hide()
			end
			d[id].border:SetBackdropBorderColor(r, g, b, 1)
			d[id].border:Show()
		else
			d[id]:SetTexture("Interface\\AddOns\\InvenRaidFrames\\Texture\\DebuffIcon")
			d[id]:SetVertexColor(r, g, b)
			d[id]:Show()
			d[id].count:SetText("")
			d[id].count:Hide()
			d[id].border:Hide()
		end
		return true
	else
		return false
	end
end

function IRF_Debuff:ClearIcon(unit, id)
	if unit and self.vars.unit[unit] then
		if id then
			self.vars.unit[unit][id]:SetTexture("")
			self.vars.unit[unit][id]:Hide()
			self.vars.unit[unit][id].count:SetText("")
			self.vars.unit[unit][id].count:Hide()
			self.vars.unit[unit][id].border:Hide()
		else
			for i = 1, 4 do
				self.vars.unit[unit][i]:SetTexture("")
				self.vars.unit[unit][i]:Hide()
				self.vars.unit[unit][i].count:SetText("")
				self.vars.unit[unit][i].count:Hide()
				self.vars.unit[unit][i].border:Hide()
			end
			self.vars.unit[unit].update:Hide()
		end
	end
end

function IRF_Debuff:ClearPet()
	for unit in pairs(self.vars.unit) do
		if strfind(unit, "pet") and not UnitExists(unit) then
			self:ClearIcon(unit)
		end
	end
end

function IRF_Debuff:Create(unit)
	if not self.vars.unit[unit] then
		self.vars.unit[unit] = {}
		for i = 1, 4 do
			local f = self.vars.frame:CreateTexture(nil, "ARTWORK")
			f:SetTexture("")
			f:SetWidth(mother.db.profile.debuffsize2)
			f:SetHeight(mother.db.profile.debuffsize2)
			local c = self.vars.frame:CreateFontString(nil, "OVERLAY", "NumberFontNormalSmall")
			c:SetPoint("CENTER", f, "CENTER")
			local b = CreateFrame("Frame", nil, self.vars.frame)
			b:SetAllPoints(f)
			b:SetBackdrop({
				bgFile = "Interface\\Addons\\InvenRaidFrames\\Texture\\Blink", tile = true, tileSize = 16,
				edgeFile = "Interface\\Addons\\InvenRaidFrames\\Texture\\Blink", edgeSize = 1,
				insets = { left = 1, right = 1, top = 1, bottom = 1 },
			})
			b:SetBackdropColor(0, 0, 0, 0)
			b:SetBackdropBorderColor(0, 0, 0, 0)
			self.vars.unit[unit][i] = f
			self.vars.unit[unit][i].count = c
			self.vars.unit[unit][i].border = b
		end
		local of = CreateFrame("Frame")
		of.unit = unit
		of:SetScript("OnUpdate", function()
			if not (of.unit and UnitExists(of.unit)) then
				self:ClearIcon(unit)
				of:Hide()
			end
		end)
		self.vars.unit[unit].update = of
	end
	return self.vars.unit[unit]
end

function IRF_Debuff:SetLocation(unit)
	if unit then
		local f = mother:GetUnit(unit)
		local d = self.vars.unit[unit]
		if not (f and d) then
			self:ClearIcon(unit)
			return
		end
		self.vars.frame:SetFrameStrata(f:GetFrameStrata())
		self.vars.frame:SetFrameLevel(f.textparent:GetFrameLevel() + 2)
		local p = mother.db.profile.debuffloc2
		local n = self:GetNumIcon(unit)
		if n == 0 then return end
		if p == "중앙" or p == "상단" or p == "하단" then
			for i = 1, n do
				d[i]:ClearAllPoints()
			end
			if n == 1 then
				d[1]:SetPoint(pos[p], f, pos[p])
			elseif n == 2 then
				d[1]:SetPoint((pos[p] == "CENTER" and "" or pos[p]).."RIGHT", f, pos[p])
				d[2]:SetPoint("LEFT", d[1], "RIGHT")
			elseif n == 3 then
				d[2]:SetPoint(pos[p], f, pos[p])
				d[1]:SetPoint("RIGHT", d[2], "LEFT")
				d[3]:SetPoint("LEFT", d[2], "RIGHT")
			elseif n == 4 then
				d[2]:SetPoint((pos[p] == "CENTER" and "" or pos[p]).."RIGHT", f, pos[p])
				d[1]:SetPoint("RIGHT", d[2], "LEFT")
				d[3]:SetPoint("LEFT", d[2], "RIGHT")
				d[4]:SetPoint("LEFT", d[3], "RIGHT")
			end
		elseif p == "좌측" or p == "좌측 상단" or p == "좌측 하단" then
			d[1]:ClearAllPoints()
			d[1]:SetPoint(pos[p], f, pos[p])
			for i = 2, n do
				d[i]:ClearAllPoints()
				d[i]:SetPoint("LEFT", d[i - 1], "RIGHT")
			end
		elseif p == "우측" or p == "우측 상단" or p == "우측 하단" then
			d[1]:ClearAllPoints()
			d[1]:SetPoint(pos[p], f, pos[p])
			for i = 2, n do
				d[i]:ClearAllPoints()
				d[i]:SetPoint("RIGHT", d[i - 1], "LEFT")
			end
		end
	else
		for u in pairs(mother.vars.unit) do
			self:SetLocation(u)
		end
	end
end

function IRF_Debuff:GetNumIcon(unit)
	if self.vars.unit[unit] then
		for i = 1, 4 do
			if not self.vars.unit[unit][i]:IsVisible() then
				return i - 1
			end
		end
		return 4
	else
		return 0
	end
end

end

function IRF_Debuff:SetSize(unit)
	if unit then
		if self.vars.unit[unit] then
			for i = 1, 4 do
				self.vars.unit[unit][i]:SetWidth(mother.db.profile.debuffsize2)
				self.vars.unit[unit][i]:SetHeight(mother.db.profile.debuffsize2)
			end
			self:SetLocation(unit)
		end
	else
		for u in pairs(mother.vars.unit) do
			self:SetSize(u)
		end
	end
end

function IRF_Debuff:SetScale()
	self.vars.frame:SetScale(mother.db.profile.scale)
end