Quantcast
local L = Elementarist.Locals;

Elementarist:RegisterModule("elemental");

Elementarist.elemental = {
	["Configuration"] = {
	};
	Initialize = function(self)
		Elementarist:LoadSpells({
			["Elemental Mastery"] = GetSpellInfo(16166),
			["Flame Shock"] = GetSpellInfo(188389),
			["Totem Mastery"] = GetSpellInfo(210643),
			["Fire Elemental"] = GetSpellInfo(198067),
			["Storm Elemental"] = GetSpellInfo(192249),
			["Icefury"] = GetSpellInfo(210714),
			["Earthquake Totem"] = GetSpellInfo(61882),
			["Elemental Blast"] = GetSpellInfo(117014),
			["AscendanceElementalBuff"] = GetSpellInfo(114050),
		});

		Elementarist.role = "DPS";
		Elementarist.trackAura = Elementarist.SpellList["Flame Shock"]  -- Spell used in Aura Tracking window
		Elementarist.trackFilter = "PLAYER|HARMFUL";
		Elementarist.trackDuration = 30;
	end;

-----------------------------------------------------
-- Determine next spell to display
-----------------------------------------------------

	NextSpell = function(self,timeshift,exspell1,exspell2)
		local guid = UnitGUID("target")
		local currentTime = GetTime()
		local s,d,e
		local haveFireTotem,fireTotemName,fireTotemStart,fireTotemDuration
		local lastSpell
		local boostFSpriority = false
		local _
		local fsRefreshPercentage=0.3
		if Elementarist.hasT18_Class_Trinket then
			fsRefreshPercentage=fsRefreshPercentage + 0.48
			if UFTalent then fsRefreshPercentage=fsRefreshPercentage + 0.22 end
		end
		if (exspell1) then
			if (exspell2) then
				lastSpell = exspell2
			else
				lastSpell = exspell1
			end
		else
			lastSpell = Elementarist.lastSpell
		end

		-- get current spell and target information
		tguid = UnitGUID("target")
		local spellInCast, _, _, _, sICstartTime, sICendTime = UnitCastingInfo("player")


		-----------------------------------------------------
		-- Timing calulatutions
		-----------------------------------------------------

		-- set minimum amount of flameshock remaining for a lava burst
		local LvBct = 2 - (2 * Elementarist.spellHaste * .01)

		--  Set Global Coolodown
		Elementarist.lastBaseGCD = 1.5 - (1.5 * Elementarist.spellHaste * .01)

		if (not timeshift) then
			timeshift = 0
		end

		-- Adjust current spell to deal with timming issue
		if (spellInCast) then
			if ( (sICendTime - sICstartTime) / 1000 ) < Elementarist.lastBaseGCD then
				sICendTime = sICstartTime + (Elementarist.lastBaseGCD * 1000)
			end
			Elementarist.lastCastTime = sICendTime;
			timeshift = timeshift + (sICendTime / 1000) - GetTime()
		else
			-- to prevent tick in current spell, check if last one finished in short time
			if (Elementarist.lastCastTime) and ((Elementarist.lastCastTime / 1000) + Elementarist.lastBaseGCD >= GetTime() ) then
				spellInCast = Elementarist.lastSpell
			end

			-- no spell in cast, check global cd via Ghost Wolf
			if (Elementarist.SpellList["Ghost Wolf"]) then
				local ftcd = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Ghost Wolf"])
				if (ftcd) then
					timeshift = timeshift + Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Ghost Wolf"])
				else
					timeshift = timeshift + Elementarist.lastBaseGCD
				end
			else
				timeshift = timeshift + Elementarist.lastBaseGCD
			end
		end

		-----------------------------------------------------
		-- buffs and debuff status
		-----------------------------------------------------

		-- get targets flame shock debuff information
		name, _, _, _, _, fsDuration,fsExpiration, unitCaster = Elementarist:hasDeBuff("target", Elementarist.SpellList["Flame Shock"], "player");
		if (not fsExpiration) then
			fsExpiration = 0
			fsDuration = 0
		end

		-- get lava burst charges and adjust Charges based on timeshift
		local lvbCharges, maxLvbCharges, cdStart, cdLength  = GetSpellCharges(Elementarist.SpellList["Lava Burst"]);
		lvbCharges = lvbCharges - Elementarist:Count(Elementarist.SpellList["Lava Burst"], spellInCast,exspell1,exspell2);
		if (((cdStart + cdLength)- GetTime()) - timeshift <= 0) then
			lvbCharges = lvbCharges + 1
		end

		-- check if assendance active
		local ascendance, _, _, _, _, _, ascendanceExp = Elementarist:hasBuff("player",Elementarist.SpellList["AscendanceElementalBuff"]);
		if (ascendance == nil) then
			ascendanceExp = 0
		end

		-----------------------------------------------------
		-- Spell Priorities for Next Spell to be recommeneded
		-----------------------------------------------------

		-- if target is dead, return ""
		if (UnitHealth("target")<=0) then
			return ""
		end

		-- if not in combat Recommend Pre-combat and spells

		if 	Elementarist.inCombat == false then
		    -- confirm out of combat
			if UnitAffectingCombat ("player") then
				Elementarist.inCombat = true
			end
			if 	Elementarist.inCombat == false then

			end
		end

		-- If Tier 1 talent is totem mastery keep those totems up
		if Elementarist.talents[1] == 3 then
			local haveFireTotem,fireTotemName,fireTotemStart,fireTotemDuration = GetTotemInfo(1)
			if (Elementarist:ZeroCount(Elementarist.SpellList["Totem Mastery"],spellInCast,exspell1,exspell2)) then
				if (not haveFireTotem) or (fireTotemName ~= Elementarist.SpellList["Totem Mastery"]) or (fireTotemStart + fireTotemDuration - currentTime - timeshift <= 0) then
					return Elementarist.SpellList["Totem Mastery"]
				end
			end
		end

		-- Flame Shock
		if (Elementarist:ZeroCount(Elementarist.SpellList["Flame Shock"],spellInCast,exspell1,exspell2)) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Flame Shock"])
			if ((d - timeshift) <= 0) then
				if ((fsExpiration - currentTime - timeshift) < 1) then
					return Elementarist.SpellList["Flame Shock"]
				end
			end
		end

		-- Earth Shock if Maelstrom capped
		if (UnitPower("player",11)>=100) and (Elementarist:ZeroCount(Elementarist.SpellList["Earth Shock"],spellInCast,exspell1,exspell2)) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Earth Shock"])
			if ((d - timeshift) <= 0) then
				return Elementarist.SpellList["Earth Shock"]
			end
		end

		-- Elemental Blast if available
		if (Elementarist.talents[4] == 1) then
			if ((Elementarist:ZeroCount(Elementarist.SpellList["Elemental Blast"],spellInCast,exspell1,exspell2) ) and
				(IsSpellInRange(Elementarist.SpellList["Elemental Blast"], "target") == 1) ) then
				d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Elemental Blast"])
				if ((d - timeshift) <= 0) then
					return Elementarist.SpellList["Elemental Blast"]
				end
			end
		end

		-- Icefury if available and Maelstrom <= 60
		if (Elementarist.talents[5] == 3) then
			if (UnitPower("player",11)<60) and (Elementarist:ZeroCount(Elementarist.SpellList["Icefury"],spellInCast,exspell1,exspell2)) then
				d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Icefury"])
				if ((d - timeshift) <= 0) then
					return Elementarist.SpellList["Icefury"]
				end
			end
		end

		-- Lava Burst
		if ( (lvbCharges > 0) or((ascendanceExp-GetTime()-timeshift) > 0)  ) and
			Elementarist:SpellAvailable(Elementarist.SpellList["Lava Burst"])
		then
			if (IsSpellInRange(Elementarist.SpellList["Flame Shock"], "target") == 1) and
			(
				((fsExpiration~=0) and ((fsExpiration-GetTime()-timeshift) > LvBct)) or
				(Elementarist:Count(Elementarist.SpellList["Flame Shock"],spellInCast,exspell1,exspell2) > 0)
			) then
				d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Lava Burst"])
				if ((d-timeshift) <= 0) or ((ascendanceExp-GetTime()-timeshift) > 0) then
					return Elementarist.SpellList["Lava Burst"]
				end
			end
		end

		-- Earth Shock if Maelstrom > 90
		if (UnitPower("player",11)>=90) and (Elementarist:ZeroCount(Elementarist.SpellList["Earth Shock"],spellInCast,exspell1,exspell2)) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Earth Shock"])
			if ((d - timeshift) <= 0) then
				return Elementarist.SpellList["Earth Shock"]
			end
		end

		-- Lightning Bolt
		if IsSpellInRange(Elementarist.SpellList["Lightning Bolt"], "target") == 1 and
			Elementarist:SpellAvailable(Elementarist.SpellList["Lightning Bolt"])then
			return Elementarist.SpellList["Lightning Bolt"]
		end

		return ""
	end;
	MiscSpell = function(self)
		-- Miscelaneous spell order:
		-- Fire Elemental
		-- Ascendance
		-- Elemental Mastery
		-- Berserking troll racial (if available)
		-- Blood Fury orc racial

		local d, e
		local name, expirationTime, _, name2, expirationTime2, name3, expirationTime3

		------  Augment Rune
		if (
			(
				((Elementarist.faction == "Horde") and (GetItemCount(Elementarist.CustomIDs["Empowered Augment Rune Horde"]) ~= 0)) or
				((Elementarist.faction == "Alliance") and (GetItemCount(Elementarist.CustomIDs["Empowered Augment Rune Alliance"]) ~= 0))
			) and ( not  Elementarist:hasBuff("player", Elementarist.SpellList["Focus Augmentation"]))
		) then
        	if (
        		((Elementarist.faction == "Horde") and (Elementarist:GetItemCooldownRemaining(Elementarist.CustomIDs["Empowered Augment Rune Horde"]) < 2)) or
        		((Elementarist.faction == "Alliance") and (Elementarist:GetItemCooldownRemaining(Elementarist.CustomIDs["Empowered Augment Rune Alliance"]) < 2))
        	) then
				return nil,GetItemIcon(Elementarist.CustomIDs["Empowered Augment Rune Horde"])
			end
       	end

		-- Alchemy Flask of Enhancement
		if	(
			((GetItemCount(Elementarist.CustomIDs["Whispers of Insanity Item"]) ~= 0) and (Elementarist.playerLevel >= 90)  ) or
			((GetItemCount(Elementarist.CustomIDs["Crystal of Insanity Item"]) ~= 0)and (Elementarist.playerLevel >= 85)  ) or
			((GetItemCount(Elementarist.CustomIDs["Alchemist's Flask Item"]) ~= 0)  and (Elementarist.playerLevel <= 91)  )
		) then
			name = Elementarist:hasBuff("player", Elementarist.SpellList["Greater Draenic Intellect Flask"]);
			if (name == nil) then
				name = Elementarist:hasBuff("player", Elementarist.SpellList["Draenic Intellect Flask"]);
			end
			if (name == nil) then
				name = Elementarist:hasBuff("player", Elementarist.SpellList["Flask of the Warm Sun"]);
			end
			if (name == nil) then
				name = Elementarist:hasBuff("player", Elementarist.SpellList["Flask of the Draconic Mind"]);
			end
			if (name == nil) then
				name,  _, _, _, _, _, expirationTime = Elementarist:hasBuff("player", Elementarist.SpellList["Whispers of Insanity"], false, Elementarist.CustomIDs["Whispers of Insanity Spell"]);
				name2, _, _, _, _, _, expirationTime2 = Elementarist:hasBuff("player", Elementarist.SpellList["Crystal of Insanity"], false, Elementarist.CustomIDs["Crystal of Insanity Spell"]);
				name3, _, _, _, _, _, expirationTime3 = Elementarist:hasBuff("player", Elementarist.SpellList["Alchemist's Flask"], false, Elementarist.CustomIDs["Alchemist's Flask Spell"]);

				if ((name == nil) and (name2 == nil) and (name3 == nil)) or (((name ~= nil) and (expirationTime<2)) or ((name2 ~= nil) and (expirationTime2<2)) or ((name3 ~= nil) and (expirationTime3<2))) then
					if (GetItemCount(Elementarist.CustomIDs["Whispers of Insanity Item"]) ~= 0) then
						local itemCooldown = Elementarist:GetItemCooldownRemaining(Elementarist.CustomIDs["Whispers of Insanity Item"]);
						if ((name == nil) or (expirationTime < 2)) and (itemCooldown < 1) then
							return nil,GetItemIcon(Elementarist.CustomIDs["Whispers of Insanity Item"])
						end
					elseif (GetItemCount(Elementarist.CustomIDs["Crystal of Insanity Item"]) ~= 0) then
						local itemCooldown = Elementarist:GetItemCooldownRemaining(Elementarist.CustomIDs["Crystal of Insanity Item"]);
						if ((name == nil) or (expirationTime < 2)) and (itemCooldown < 1) then
							return nil,GetItemIcon(Elementarist.CustomIDs["Crystal of Insanity Item"])
						end
					elseif (GetItemCount(Elementarist.CustomIDs["Alchemist's Flask Item"]) ~= 0) then
						local itemCooldown = Elementarist:GetItemCooldownRemaining(Elementarist.CustomIDs["Alchemist's Flask Item"]);
						if ((name == nil) or (expirationTime < 2)) and (itemCooldown < 1) then
							return nil,GetItemIcon(Elementarist.CustomIDs["Alchemist's Flask Item"])
						end
					end
				end
			end
		end

		-- Fire Elemental
		if Elementarist:SpellAvailable(Elementarist.SpellList["Fire Elemental"]) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Fire Elemental"])
			if d <= 0.5 then
				return Elementarist.SpellList["Fire Elemental"]
			end
		end

		-- Storm Elemental
		if Elementarist:SpellAvailable(Elementarist.SpellList["Storm Elemental"]) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Storm Elemental"])
			if d <= 0.5 then
				return Elementarist.SpellList["Storm Elemental"]
			end
		end

		-- Ascendance
		if Elementarist:SpellAvailable(Elementarist.SpellList["Ascendance"]) then
			name = Elementarist:hasBuff("player",Elementarist.SpellList["AscendanceElementalBuff"])
			if (name == nil) then
				d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Ascendance"])
				if d <= 0.5 then
					return Elementarist.SpellList["Ascendance"]
				end
			end
		end

		-- Elemental Mastery
		if Elementarist:SpellAvailable(Elementarist.SpellList["Elemental Mastery"]) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Elemental Mastery"])
			if d <= 0.5 then
				return Elementarist.SpellList["Elemental Mastery"]
			end
		end

		-- Berserking
		if Elementarist:SpellAvailable(Elementarist.SpellList["Berserking"]) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Berserking"])
			if d <= 0.5 then
				return Elementarist.SpellList["Berserking"]
			end
		end

		-- Blood Fury
		if Elementarist:SpellAvailable(Elementarist.SpellList["Blood Fury"]) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Blood Fury"])
			if d <= 0.5 then
				return Elementarist.SpellList["Blood Fury"]
			end
		end

		return ""
	end;
	IntSpell = function(self)
		-- interruptions, purge, and AoE on target
		local d

		if Elementarist:SpellAvailable(Elementarist.SpellList["Wind Shear"]) then
			d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Wind Shear"])
			if ( (IsSpellInRange(Elementarist.SpellList["Wind Shear"], "target") == 1) and (d) and (d<0.5)  ) then
				local _, status, threatpct, _, _ = UnitDetailedThreatSituation("player", "target")

				--- windshear to drop threat
				if ( (status) and (threatpct>80) and (Elementarist.person["friendCount"]>1) and (Elementarist.inParty>0)) then
					return Elementarist.SpellList["Wind Shear"]
				end

				--- windshear to interupt channel spell
				_, _, _, _, _, _, _, notInterruptible = UnitChannelInfo("target")
				if (notInterruptible==false) then
					return Elementarist.SpellList["Wind Shear"]
				end

				--- windshear to interupt cast spell
				_, _, _, _, _, _, _, _, notInterruptible = UnitCastingInfo("target")
				if (notInterruptible==false)  then
					return Elementarist.SpellList["Wind Shear"]
				end
			end
		end

		if (Elementarist.person["foeCount"]>1) then
			----  Lava beam if available
			if Elementarist:SpellAvailable(Elementarist.SpellList["Lava Beam"]) then
				if IsSpellInRange(Elementarist.SpellList["Lava Beam"], "target") == 1 then
					d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Lava Beam"])
					return Elementarist.SpellList["Lava Beam"]
				end
			end

			---- Chain Lightning as filler
			if Elementarist:SpellAvailable(Elementarist.SpellList["Chain Lightning"]) then
				return Elementarist.SpellList["Chain Lightning"]
			end

			-- Earthquake if 3 or more targets
			if (Elementarist.person["foeCount"]>=3) and Elementarist:SpellAvailable(Elementarist.SpellList["Earthquake Totem"]) then
				if (Elementarist:Count(Elementarist.SpellList["Earthquake Totem"],spellInCast,exspell1,exspell2) == 0) then
					return Elementarist.SpellList["Earthquake Totem"]
				end
			end

		end

		-- check if purgeable buff is on target (not sure if this is ok)
		if Elementarist:SpellAvailable(Elementarist.SpellList["Purge"]) then
			if IsSpellInRange(Elementarist.SpellList["Purge"], "target") == 1 then
				if (Elementarist:hasBuff("target", ".", 1)) then
					d = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Purge"])
					if (d) and (d<0.5) then
						return Elementarist.SpellList["Purge"]
					end
				end
			end
		end

		return ""
	end;
	ShieldName = function(self)
		return "";
	end;

};