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

-- Cache Global WoW API Functions
local Enum = Enum
local UnitEffectiveLevel = UnitEffectiveLevel
local UnitPower = UnitPower

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

-- Initialize Data Variables
Earthquake.spellID = 61882
Earthquake.pulseTime = 0
Earthquake.powerTime = 0
Earthquake.elapsed = 0
Earthquake.condition = function()
	return UnitEffectiveLevel("player") >= 38
	--return IsSpellKnown(61882)
end

Earthquake:SetScript('OnUpdate', function(self,elapsed)
	if (Auras:RefreshRateHandler(0.5,self.elapsed)) then
		self.elapsed = 0
		
		if ((Auras:CharacterCheck(self,1) and self.condition()) or Auras:IsPreviewingAura(self)) then
			local groupID = Auras:GetAuraGroupID(self,self:GetName())
			local _,_,_,_,duration,expires = Auras:RetrieveAuraInfo("player", 208723)
			local _,_,_,_,dur,exprs = Auras:RetrieveAuraInfo("player", 286976)
			local power = UnitPower('player',Enum.PowerType.Maelstrom)
			local glow = Auras.db.char.auras[1].auras[self:GetName()].glow
			local powerCost = GetSpellPowerCost(self.spellID)
			
			if (not self.TectonicThunder) then
				local Frame = CreateFrame('Frame','TectonicThunder',self:GetParent())
				Frame:SetFrameStrata('MEDIUM')
				Frame:SetWidth(16)
				Frame:SetHeight(16)
				Frame:SetPoint("CENTER",self,"TOPRIGHT",0,0)
				
				Frame.texture = Frame:CreateTexture(nil,'BACKGROUND')
				Frame.texture:SetTexture([[Interface\addons\ShamanAuras2\media\ICONS\elemental\chain_lightning]])
				Frame.texture:SetAllPoints(Frame)
				
				Frame.CD = CreateFrame('Cooldown','TectonicThunderCD',Frame,'CooldownFrameTemplate')
				Frame.CD:SetAllPoints(Frame)
				Frame.CD:SetHideCountdownNumbers(true)
				
				Frame.CD.text = Frame.CD:CreateFontString(nil,'MEDIUM','GameFontHighlightLarge')
				Frame.CD.text:SetAllPoints(Frame.CD)
				--Frame.CD.text:Set
				Frame.CD.text:SetTextColor(1,1,0,1)
				--Frame.CD.text:SetFont(LSM.MediaTable.font['PT Sans Narrow'] or LSM.DefaultMedia.font)
				
				Frame.glow = CreateFrame('Frame','TectonicThunderGlow',Frame)
				Frame.glow:SetPoint("CENTER",0,0)
				Frame.glow:SetWidth(20)
				Frame.glow:SetHeight(20)
				
				
				Frame:Show()
				self.TectonicThunder = Frame
			else
				local size = floor(Auras.db.char.auras[1].groups[groupID].icon * 0.78125)
				
				self.TectonicThunder:SetSize(size,size)
				self.TectonicThunder.glow:SetSize((size+10),(size+10))
				
				if (dur or 0 > 2) then
					local strt = exprs - dur
					--Auras:ExecuteCooldown(self.TectonicThunder,strt,dur,true,true,2)
					--Auras:CooldownHandler(self,2,'primary',1,start,duration)
					self.TectonicThunder.CD:SetCooldown(strt,dur)
					self.TectonicThunder.CD.text:SetText(count)
					--self.TectonicThunder.CD.text:SetText(11)
					Auras:ToggleOverlayGlow(self.TectonicThunder.glow,true)
					self.TectonicThunder:SetAlpha(1)
				else
					Auras:ToggleOverlayGlow(self.TectonicThunder.glow,false)
					self.TectonicThunder:SetAlpha(0)
				end
				
			end
			
			if (powerCost) then
				if (power >= powerCost[1].cost) then
					if (self.powerTime == 0) then
						self.powerTime = GetTime()
						
						for i=1,#glow.triggers do
							local trigger = glow.triggers[i]
							
							if (trigger.spellID == self.spellID) then
								trigger.start = self.powerTime
							end
						end
					end
				else
					if (self.powerTime > 0) then
						self.powerTime = 0
						
						for i=1,#glow.triggers do
							local trigger = glow.triggers[i]
							
							if (trigger.spellID == self.spellID) then
								trigger.start = self.powerTime
							end
						end
					end
				end
			end
			
			Auras:SetGlowStartTime(self,((expires or 0) - (duration or 0)),duration,208723,"buff")
			Auras:GlowHandler(self,groupID)
			Auras:ToggleAuraVisibility(self,true,'showhide')
			Auras:CooldownHandler(self,groupID,start,duration,true)
			
			if (Auras:IsPlayerInCombat(true)) then
				self:SetAlpha(1)
				
				if (power >= powerCost[1].cost or buff) then
					self:SetAlpha(1)
				else
					self:SetAlpha(0.5)
				end
			else
				Auras:NoCombatDisplay(self,groupID)
			end
		else
			Auras:ToggleAuraVisibility(self,false,'showhide')
		end
	else
		self.elapsed = self.elapsed + elapsed
	end
end)