local _G = _G
local IRF3 = _G[...]
local ipairs = _G.ipairs
local UnitAura = _G.UnitAura
local name, icon, count, expirationTime, noSpell
local spells = {}
local delimiter = ","

local numberFont = IRF3:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
local numberFontWidth = {}
for i = 1, 5 do
	numberFont:SetText(strrep("0", i))
	numberFontWidth[i] = ceil(numberFont:GetWidth())
end

local function onUpdateIconTimer(self)
	self.timeLeft = self.expirationTime - GetTime()
	if self.timeLeft > 100 then
		if self.noIcon then
			self.timer:SetText("●")
		else
			self.timer:SetText("")
		end
	else
		self.timer:SetFormattedText("%d", self.timeLeft + 0.5)
	end
	self:SetWidth((numberFontWidth[(self.timer:GetText() or ""):len()] or 0) + (self.noIcon and 0 or 13))
end

local function setIcon(self, index, duration, expirationTime)
	if index then
		self:SetScale(InvenRaidFrames3CharDB.spellTimer[index].scale)
		if InvenRaidFrames3CharDB.spellTimer[index].display == 1 then
			self.noIcon = nil
			self.icon:SetWidth(13)
			self.icon:SetTexture(icon)
			self.count:SetText(count and count > 1 and count or "")
		elseif InvenRaidFrames3CharDB.spellTimer[index].display == 2 then
			self.noIcon = nil
			self:SetWidth(13)
			self.icon:SetWidth(13)
			self.icon:SetTexture(icon)
			self.count:SetText(count and count > 1 and count or "")
			duration = nil
		else
			self.noIcon = true
			self.icon:SetWidth(0.001)
			self.icon:SetTexture(nil)
			self.count:SetText(nil)
		end
		if duration and duration > 0 and expirationTime then
			self.expirationTime = expirationTime
			self:SetScript("OnUpdate", onUpdateIconTimer)
			onUpdateIconTimer(self)
		else
			self.expirationTime, self.timeLeft = nil
			self:SetScript("OnUpdate", nil)
			if self.noIcon then
				self.timer:SetText("●")
			else
				self.timer:SetText("")
			end
		end
		self:Show()
	elseif self:IsShown() then
		self.expirationTime, self.timeLeft, self.noIcon = nil
		self:SetScript("OnUpdate", nil)
		self:Hide()
	end
end

local filter = { "HELPFUL|PLAYER", "HARMFUL|PLAYER", "HELPFUL", "HARMFUL" }

function InvenRaidFrames3Member_UpdateSpellTimer(self)
	for i = 1, 8 do
		noSpell = true
		if filter[InvenRaidFrames3CharDB.spellTimer[i].use] and InvenRaidFrames3CharDB.spellTimer[i].name then
			spells[1], spells[2], spells[3], spells[4], spells[5] = delimiter:split(InvenRaidFrames3CharDB.spellTimer[i].name)
			for _, spell in ipairs(spells) do
				name, _, icon, count, _, duration, expirationTime = UnitAura(self.displayedUnit, spell:trim(), nil, filter[InvenRaidFrames3CharDB.spellTimer[i].use])
				if name then
					setIcon(self["spellTimer"..i], i, duration, expirationTime)
					noSpell = nil
					break
				end
			end
		end
		if noSpell then
			setIcon(self["spellTimer"..i])
		end
	end
end

local pos = { "BOTTOMLEFT", "BOTTOM", "BOTTOMRIGHT", "LEFT", "RIGHT", "TOPLEFT", "TOP", "TOPRIGHT" }
local SL = IRF3.GetSpellName

function IRF3:SetupSpellTimer(reset)
	if not reset and InvenRaidFrames3CharDB.spellTimer and #InvenRaidFrames3CharDB.spellTimer == 8 then return end
	InvenRaidFrames3CharDB.spellTimer = InvenRaidFrames3CharDB.spellTimer or {}
	for i = 1, 8 do
		InvenRaidFrames3CharDB.spellTimer[i] = InvenRaidFrames3CharDB.spellTimer[i] or {}
		InvenRaidFrames3CharDB.spellTimer[i].use = 0		-- 1:내가 시전한 버프 2:내가 시전한 디버프 3:모든 버프 4:모든 디버프 0:사용 안함
		InvenRaidFrames3CharDB.spellTimer[i].display = 1	-- 1:아이콘 + 남은 시간 2:아이콘 3:남은 시간
		InvenRaidFrames3CharDB.spellTimer[i].scale = 1
		InvenRaidFrames3CharDB.spellTimer[i].pos = pos[i]
	end
	if self.playerClass == "WARRIOR" then
		InvenRaidFrames3CharDB.spellTimer[1].use = 1
		InvenRaidFrames3CharDB.spellTimer[1].name = SL(50720)	-- 경계
	elseif self.playerClass == "WARRIOR" then
		InvenRaidFrames3CharDB.spellTimer[1].use = 1
		InvenRaidFrames3CharDB.spellTimer[1].name = SL(57934)	-- 속임수 거래
	elseif self.playerClass == "PRIEST" then
		InvenRaidFrames3CharDB.spellTimer[1].use = 1
		InvenRaidFrames3CharDB.spellTimer[1].name = SL(139)	-- 소생
		InvenRaidFrames3CharDB.spellTimer[2].use = 1
		InvenRaidFrames3CharDB.spellTimer[2].name = SL(33076)	-- 회복의 기원
		InvenRaidFrames3CharDB.spellTimer[3].use = 4
		InvenRaidFrames3CharDB.spellTimer[3].name = SL(6788)	-- 약화된 영혼
		InvenRaidFrames3CharDB.spellTimer[2].use = 3
		InvenRaidFrames3CharDB.spellTimer[2].name = SL(17)	-- 신의 권능: 보호막
	elseif self.playerClass == "HUNTER" then
		InvenRaidFrames3CharDB.spellTimer[1].use = 1
		InvenRaidFrames3CharDB.spellTimer[1].name = SL(34477)	-- 눈속임
	elseif self.playerClass == "DRUID" then
		InvenRaidFrames3CharDB.spellTimer[1].use = 1
		InvenRaidFrames3CharDB.spellTimer[1].name = SL(33763)	-- 피어나는 생명
		InvenRaidFrames3CharDB.spellTimer[2].use = 1
		InvenRaidFrames3CharDB.spellTimer[2].name = SL(774)	-- 회복
		InvenRaidFrames3CharDB.spellTimer[3].use = 1
		InvenRaidFrames3CharDB.spellTimer[3].name = SL(8936)	-- 재생
		InvenRaidFrames3CharDB.spellTimer[4].use = 1
		InvenRaidFrames3CharDB.spellTimer[4].name = SL(48438)	-- 급속 성장
	elseif self.playerClass == "SHAMAN" then
		InvenRaidFrames3CharDB.spellTimer[1].use = 1
		InvenRaidFrames3CharDB.spellTimer[1].name = SL(61295)	-- 성난 해일
		InvenRaidFrames3CharDB.spellTimer[2].use = 1
		InvenRaidFrames3CharDB.spellTimer[2].name = SL(52127)..","..SL(324)	-- 물의 보호막, 번개 보호막
		InvenRaidFrames3CharDB.spellTimer[3].use = 1
		InvenRaidFrames3CharDB.spellTimer[3].name = SL(974)	-- 대지의 보호막
	elseif self.playerClass == "PALADIN" then
		InvenRaidFrames3CharDB.spellTimer[1].use = 3
		InvenRaidFrames3CharDB.spellTimer[1].name = SL(53563)	-- 빛의 봉화
		InvenRaidFrames3CharDB.spellTimer[2].use = 1
		InvenRaidFrames3CharDB.spellTimer[2].name = SL(1022)..","..SL(1044)..","..SL(6940)..","..SL(1038)	-- 보호의 손길, 자유의 손길, 희생의 손길, 구원의 손길
	end
end