Quantcast
local L = Elementarist.Locals;

Elementarist:RegisterModule("restoration");

Elementarist.restoration = {
	Initialize = function(self)
		Elementarist:LoadSpells({
			["Healing Wave"] = GetSpellInfo(331),
			["Greater Healing Wave"] = GetSpellInfo(77472),
			["Healing Surge"] = GetSpellInfo(8004),
			["Riptide"] = GetSpellInfo(61295),
			["Earthliving Weapon"] = GetSpellInfo(51730),
			["Water Shield"] = GetSpellInfo(52127),
			["Tidal Waves"] = GetSpellInfo(53390),

			["Flask of Falling Leaves"] = GetSpellInfo(105693),
			["Healing Stream Totem"] = GetSpellInfo(5394),
			["Earth Shield"] = GetSpellInfo(974),
			["Healing Rain"] = GetSpellInfo(73921),
			["Healing Tide Totem"]	= GetSpellInfo(108280),
			["Spirit Link Totem"] = GetSpellInfo(98008),
		});
		Elementarist.role = "heal";
		Elementarist.trackAura = Elementarist.SpellList["Riptide"];
		Elementarist.trackFilter = "PLAYER|HELPFUL";
		Elementarist.trackDuration = 18;
		Elementarist.callbacks = {
			["COMBAT_LOG"] = function(self,event,spellName,srcGUID,dstGUID)
				if (spellName == Elementarist.SpellList["Earth Shield"]) then
					if (srcGUID == Elementarist.playerGUID) then
						if (not Elementarist.auraCooldowns["0"]) then
							Elementarist.auraCooldowns["0"] = {}
						end;
						if (event=="SPELL_CAST_SUCCESS") then
							Elementarist.auraCooldowns["0"].charges = 9;
							Elementarist:SetTexture(Elementarist.textureList["debuff"],select(3, GetSpellInfo(Elementarist.SpellList["Earth Shield"])));
							Elementarist.textureList["debuff"]:Show();
						end;
						if (event=="SPELL_HEAL") and (Elementarist.auraCooldowns["0"]) and (Elementarist.auraCooldowns["0"].charges) then
							Elementarist.auraCooldowns["0"].charges = Elementarist.auraCooldowns["0"].charges - 1;
						end;
						if (Elementarist.auraCooldowns["0"]) and (Elementarist.auraCooldowns["0"].charges) then
							Elementarist.textList['debuff']:SetText( Elementarist.auraCooldowns["0"].charges );
						end;
						if (event=="SPELL_AURA_REMOVED") then
							Elementarist.auraCooldowns["0"] = {};
							Elementarist:SetTexture(Elementarist.textureList["debuff"],"");
							Elementarist.textList['debuff']:SetText("");
						end;
					end;
				end;
			end;
		};
	end;

	NextSpell = function(self,timeshift,exspell1,exspell2)
		local i, count, e;

		if (not timeshift) then
			timeshift = 0
		end

		-- Earth Shield, if not known
		if (not Elementarist.auraCooldowns["0"]) or (not Elementarist.auraCooldowns["0"].charges) or (Elementarist.auraCooldowns["0"].charges == 0) then
			if (exspell1 ~= Elementarist.SpellList["Earth Shield"]) and (exspell2 ~= Elementarist.SpellList["Earth Shield"]) then
				return Elementarist.SpellList["Earth Shield"];
			end;
		end;

		-- Riptide, if found on noone
		if (not Elementarist.auraCooldowns["0"]) or (not Elementarist.auraCooldowns["0"].charges) or (Elementarist.auraCooldowns["0"].charges == 0) then
			if (exspell1 ~= Elementarist.SpellList["Riptide"]) and (exspell2 ~= Elementarist.SpellList["Riptide"]) then
				count = 0;
				for i in pairs(Elementarist.auraCooldowns) do
					if (i ~= "0") then
						count = count + 1;
					end;
				end;
				if (count == 0) then
					e = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Riptide"])
					if (e-timeshift <= 0) then
						return Elementarist.SpellList["Riptide"];
					end
				end;
			end;
		end;

		-- Healing Rain
		if (exspell1 ~= Elementarist.SpellList["Healing Rain"]) and (exspell2 ~= Elementarist.SpellList["Healing Rain"]) then
			e = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Healing Rain"]);
			if (e-timeshift <= 0) then
				return Elementarist.SpellList["Healing Rain"];
			end;
		end;

		-- Healing Tide Totem
		if (exspell1 ~= Elementarist.SpellList["Healing Tide Totem"]) and (exspell2 ~= Elementarist.SpellList["Healing Tide Totem"]) then
			e = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Healing Tide Totem"]);
			if (e-timeshift <= 0) then
				return Elementarist.SpellList["Healing Tide Totem"];
			end;
		end;

		-- Spirit Link Totem
		if (exspell1 ~= Elementarist.SpellList["Spirit Link Totem"]) and (exspell2 ~= Elementarist.SpellList["Spirit Link Totem"]) then
			e = Elementarist:GetSpellCooldownRemaining(Elementarist.SpellList["Spirit Link Totem"]);
			if (e-timeshift <= 0) then
				return Elementarist.SpellList["Spirit Link Totem"];
			end;
		end;

		return ""
	end;
	MiscSpell = function(self)
		local d, e
		local name, expirationTime, _

		-- Alchemy Flask of Enhancement
		if GetItemCount(Elementarist.CustomIDs["Alchemist's Flask Item"]) ~= 0 then
			name = Elementarist:hasBuff("player", Elementarist.SpellList["Flask of Falling Leaves"]);
			if (name == nil) then
				name, _, _, _, _, _, expirationTime = Elementarist:hasBuff("player", Elementarist.SpellList["Alchemist's Flask"], false, Elementarist.CustomIDs["Alchemist's Flask Spell"]);
				if (name == nil) or (expirationTime < 2) then
					if (icon == nil) then
						icon = GetItemIcon(Elementarist.CustomIDs["Alchemist's Flask Item"])
					end
					return nil,icon
				end
			end
		end

		-- Earthliving weapon
		if Elementarist:SpellAvailable(Elementarist.SpellList["Earthliving Weapon"]) then
			local hasMainHandEnchant, mainHandExpiration, _, _, _, _ = GetWeaponEnchantInfo()
			if (hasMainHandEnchant == nil) or ((mainHandExpiration / 60000) < 1) then
				return Elementarist.SpellList["Earthliving Weapon"]
			end
		end

		-- Water Shield
		if Elementarist:SpellAvailable(Elementarist.SpellList["Water Shield"]) then
			name, _, _, _, _, _, expirationTime = Elementarist:hasBuff("player", Elementarist.SpellList["Water Shield"]);
			if (name == nil) or (expirationTime < 1) then
				return Elementarist.SpellList["Water Shield"]
			end
		end

		-- Healing Stream Totem
		if Elementarist:SpellAvailable(Elementarist.SpellList["Healing Stream Totem"]) then
			local haveWaterTotem,waterTotemName,_,_ = GetTotemInfo(3)
			if (waterTotemName == "") then
				-- no water totem
				return Elementarist.SpellList["Healing Stream Totem"]
			end
		end

		-- Searing Totem
		if Elementarist:SpellAvailable(Elementarist.SpellList["Searing Totem"]) then
			local haveFireTotem,fireTotemName,_,_ = GetTotemInfo(1)
			if (fireTotemName == "") then
				-- no fire totem
				return Elementarist.SpellList["Searing Totem"]
			end
		end

		return ""
	end;
	IntSpell = function(self)

		return ""
	end;
	ShieldName = function(Self)
		return "Tidal Waves";
	end;
};