local SSA, Auras = unpack(select(2,...))

-- Cache Global WoW Functions
local GetTalentInfo = GetTalentInfo

-- Cache Global Addon Variables
local WindfuryTotem = SSA.WindfuryTotem

-- Initialize Data Variables
WindfuryTotem.spellID = 8512
WindfuryTotem.pulseTime = 0
WindfuryTotem.elapsed = 0
WindfuryTotem.condition = function()
	return IsSpellKnown(8512)
end

WindfuryTotem:SetScript('OnUpdate',function(self,elapsed)
	if (not Auras.db.char.isFirstEverLoad) then
		if (Auras:RefreshRateHandler(0.5,self.elapsed)) then
			self.elapsed = 0
			
			if ((Auras:CharacterCheck(self,2) and self.condition()) or Auras:IsPreviewingAura(self)) then
				local groupID = Auras:GetAuraGroupID(self,self:GetName())
				local start,duration = 0,0
				local isTotem,isRefresh = false,false
				local buff = Auras:RetrieveAuraInfo("player", 327942)

				if (buff) then
					Auras.db.char.auras[2].auras[self:GetName()].glow.triggers[2].start = 0
				elseif (not buff and start == 0) then
					Auras.db.char.auras[2].auras[self:GetName()].glow.triggers[2].start = GetTime()
				end

				for i=1,5 do
					local _,name = GetTotemInfo(i)
	
					if (name == Auras:GetSpellName(8512)) then
						_,_,start,duration = GetTotemInfo(i)
						--print("Totem: "..(start or 0)..", "..(duration or 0))
						isTotem = true
						--remaining = (start + duration) - GetTime()
						break
					end
				end
	
				--[[if ((remaining or 0) <= db.settings[1].totemMastery) then
					isRefresh = true
				end]]

				Auras:SetGlowStartTime(self,start,duration,8512,"totem")
				Auras:GlowHandler(self,groupID)
				Auras:ToggleAuraVisibility(self,true,'showhide')
				Auras:CooldownHandler(self,groupID,start,duration)
					
				if (Auras:IsPlayerInCombat()) then
					if (isTotem) then
						self:SetAlpha(1)
						--Auras:ToggleOverlayGlow(self.glow,true,false)
					else
						self:SetAlpha(0.5)
						--Auras:ToggleOverlayGlow(self.glow,false)
					end
				else
					Auras:NoCombatDisplay(self,groupID)
					
					--[[if (buff) then
						Auras:ToggleOverlayGlow(self.glow,true,false)
					else
						Auras:ToggleOverlayGlow(self.glow,false)
					end]]
				end
			else
				Auras:ToggleAuraVisibility(self,false,'showhide')
			end
		else
			self.elapsed = self.elapsed + elapsed
		end
	end
end)

--[[WindfuryTotem:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
WindfuryTotem:SetScript("OnEvent",function(self,event)
	local spec = SSA.spec or GetSpecialization()
	
	if (event ~= "COMBAT_LOG_EVENT_UNFILTERED" or Auras.db.char.isFirstEverLoad) then
		return
	end
	
	local glow = Auras.db.char.auras[spec].auras[self:GetName()].glow
	local _,subevent,_,srcGUID,_,_,_,destGUID,_,_,_,spellID = CombatLogGetCurrentEventInfo()
	
	if (subevent == "SPELL_SUMMON" and srcGUID == UnitGUID("player") and spellID == glow.triggers[1].spellID) then
			glow.triggers[1].start = GetTime()
			glow.triggers[1].duration = duration
	end
end)]]