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

-- Cache Global WoW API Functions
local Enum = Enum
local GetSpellCooldown = GetSpellCooldown
local IsSpellKnown = IsSpellKnown
local UnitEffectiveLevel = UnitEffectiveLevel

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

-- Initialize Data Variables
EarthShock.spellID = 8042
EarthShock.pulseTime = 0
EarthShock.powerTime = 0
EarthShock.elapsed = 0
EarthShock.condition = function()
	return UnitEffectiveLevel("player") >= 10
	--return IsSpellKnown(8042)
end

EarthShock: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 power = UnitPower('player',Enum.PowerType.Maelstrom)
			local glow = Auras.db.char.auras[1].auras[self:GetName()].glow
			local start,duration = GetSpellCooldown(Auras:GetSpellName(self.spellID))
			local buff,_,count,_,buffDuration,expires = Auras:RetrieveAuraInfo("player", 273453)
			local powerCost = GetSpellPowerCost(self.spellID)
			
			-- If the text element for the "Lava Shock" artifact trait counter doesn't exist, create it.
			if (not self.lavaShock) then
				self.lavaShock = self:CreateFontString(nil, 'MEDIUM', 'GameFontHighlightLarge')
				self.lavaShock:SetPoint('BOTTOMRIGHT',-3,3)
				self.lavaShock:SetFont((LSM.MediaTable.font['PT Sans Narrow'] or LSM.DefaultMedia.font), 13.5,'OUTLINE')
				self.lavaShock:SetTextColor(1,1,1,1)
				self.lavaShock:Hide()
			end
			
			if ((count or 0) > 0) then
				self.lavaShock:Show()
				self.lavaShock:SetText(count)
			else
				self.lavaShock:Hide()
			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:GlowHandler(self,groupID)
			Auras:ToggleAuraVisibility(self,true,'showhide')
			Auras:SpellRangeCheck(self,self.spellID,true)
			Auras:CooldownHandler(self,groupID,start,duration,true)
			
			if (Auras:IsPlayerInCombat()) then
				--[[if (power >= Auras.db.char.elements[1].statusbars.maelstromBar.threshold) then
					Auras:ToggleOverlayGlow(self.glow,true)
				else
					Auras:ToggleOverlayGlow(self.glow,false)
				end]]
				if (power >= powerCost[1].cost) 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)