context: #region
  using System;
  using System.Linq;
  using LeagueSharp;
  using LeagueSharp.Common;
  using Marksman.Utils;
  using SharpDX;
  using Color = System.Drawing.Color;
  using Orbwalking = LeagueSharp.Common.Orbwalking;
  
  #endregion
  
  using EloBuddy; 
  using LeagueSharp.Common; 
   namespace Marksman.Champions
  {
      internal class Vayne : Champion
      {
          public static float rqTumbleBuffEndOfTime = 0;
          public static bool VayneUltiIsActive { get; set; }
  
          public static Spell Q, E, R;
  
          public Vayne()
          {
              Q = new Spell(SpellSlot.Q, 300f);
              E = new Spell(SpellSlot.E, 650f);
              R = new Spell(SpellSlot.R);
  
              E.SetTargetted(0.25f, 2200f);
  
              Utils.Utils.PrintMessage("Vayne loaded");
          }
  
  
          public override void Obj_AI_Base_OnProcessSpellCast(Obj_AI_Base sender, GameObjectProcessSpellCastEventArgs args)
          {
              if (!Q.IsReady()) return;
  
              if (args.Target != null && args.Target.IsMe && sender.Type == GameObjectType.AIHeroClient && sender.IsEnemy && sender.IsMelee && args.SData.IsAutoAttack())
                  //if (MenuProvider.Champion.Misc.GetBoolValue("Use Anti-Melee (E)"))
                  Q.Cast(ObjectManager.Player.Position.Extend(sender.Position, -Q.Range));
          }
  
          public override void Spellbook_OnCastSpell(Spellbook sender, SpellbookCastSpellEventArgs args)
          {
  
          }
  
          public override void Obj_AI_Base_OnBuffAdd(Obj_AI_Base sender, Obj_AI_BaseBuffGainEventArgs args)
          {
              rqTumbleBuffEndOfTime = GetValue<bool>("Misc.R.DontAttack") && sender.IsMe &&
                                      args.Buff.Name.ToLower() == "vaynetumblefade"
                  ? args.Buff.EndTime
                  : 0;
          }
  
          public override void AntiGapcloser_OnEnemyGapcloser(ActiveGapcloser gapcloser)
          {
              if (GetValue<bool>("Misc.E.Antigapcloser"))
                      if (gapcloser.End.Distance(ObjectManager.Player.Position) <= 200)
                          if (E.IsReady())
                              if (gapcloser.Sender.IsValidTarget(E.Range))
                                  E.CastOnUnit(gapcloser.Sender);
  
              if (GetValue<bool>("Misc.Q.Antigapcloser") && Q.IsReady() && gapcloser.Sender.IsValidTarget(Q.Range))
                  Q.CastOnUnit(gapcloser.Sender);
          }
  
          public override void Interrupter2_OnInterruptableTarget(AIHeroClient unit, Interrupter2.InterruptableTargetEventArgs args)
          {
              if (GetValue<bool>("UseEInterrupt") && unit.IsValidTarget(E.Range))
                      if (args.DangerLevel >= Interrupter2.DangerLevel.High)
                          if (unit.IsValidTarget(E.Range))
                              if (E.IsReady())
                                  E.CastOnUnit(unit);
          }
  
          private static bool CastE(Obj_AI_Base t)
          {
              for (var i = 1; i < 8; i++)
              {
                  var targetBehind = t.Position + Vector3.Normalize(t.ServerPosition - ObjectManager.Player.Position)*i*50;
  
                  if (targetBehind.IsWall() && t.IsValidTarget(E.Range))
                  {
                      E.CastOnUnit(t);
                      return true;
                  }
              }
              return false;
          }
  
          public override void GameOnUpdate(EventArgs args)
          {
              Orbwalker.SetAttack(Game.Time > rqTumbleBuffEndOfTime);
  
              if (JungleClearActive)
              {
                  ExecJungleClear();
              }
  
              if ((ComboActive || HarassActive))
              {
                  if (GetValue<bool>("FocusW"))
                  {
                      var silverBuffMarkedEnemy = VayneData.GetSilverBuffMarkedEnemy;
                      if (silverBuffMarkedEnemy != null)
                      {
                          TargetSelector.SetTarget(silverBuffMarkedEnemy);
                      }
                      else
                      {
                          var attackRange = Orbwalking.GetRealAutoAttackRange(ObjectManager.Player);
                          TargetSelector.SetTarget(
                              TargetSelector.GetTarget(attackRange, TargetSelector.DamageType.Physical));
                      }
                  }
  
                  var useQ = GetValue<StringList>("Combo.UseQ").SelectedIndex;
                  var t = TargetSelector.GetTarget(Q.Range + Orbwalking.GetRealAutoAttackRange(null),
                      TargetSelector.DamageType.Physical);
                  if (Q.IsReady() && t.IsValidTarget() && useQ != 0)
                  {
                      switch (useQ)
                      {
                          case 1:
                          {
                              Q.Cast(Game.CursorPos);
                              break;
                          }
  
                          case 2:
                          {
                              var silverEnemy = VayneData.GetSilverBuffMarkedEnemy;
                              if (silverEnemy != null && t.ChampionName == silverEnemy.ChampionName &&
                                  VayneData.GetSilverBuffMarkedCount == 2)
                              {
                                  Q.Cast(Game.CursorPos);
                                  Orbwalker.ForceTarget(t);
                              }
                              break;
                          }
  
                          case 3:
                          {
                              if (t.Distance(ObjectManager.Player.Position) > Orbwalking.GetRealAutoAttackRange(null) && Q.IsPositionSafe(t.Position.To2D()))
                              {
                                  Q.Cast(t.Position);
                              }
                              else if (Q.IsPositionSafe(Game.CursorPos.To2D()))
                              {
                                  Q.Cast(Game.CursorPos);
                              }
                              Orbwalker.ForceTarget(t);
                              break;
                          }
                      }
                  }
  
                  var useE = GetValue<StringList>("UseEC").SelectedIndex;
                  if (E.IsReady() && useE != 0)
                  {
                      t = TargetSelector.GetTarget(E.Range + Q.Range, TargetSelector.DamageType.Physical);
                      if (useE == 1)
                      {
                          if (t.IsValidTarget())
                          {
                              CastE(t);
                          }
                      }
                      else
                      {
                          foreach (var e in HeroManager.Enemies.Where(e => e.IsValidTarget(E.Range) && !e.IsZombie))
                          {
                              CastE(e);
                          }
                      }
                      /*
                      foreach (var hero in
                          from hero in ObjectManager.Get<AIHeroClient>().Where(hero => hero.IsValidTarget(550f))
                          let prediction = E.GetPrediction(hero)
                          where
                              NavMesh.GetCollisionFlags(
                                  prediction.UnitPosition.To2D()
                                      .Extend(
                                          ObjectManager.Player.ServerPosition.To2D(),
                                          -GetValue<Slider>("PushDistance").Value)
                                      .To3D()).HasFlag(CollisionFlags.Wall) ||
                              NavMesh.GetCollisionFlags(
                                  prediction.UnitPosition.To2D()
                                      .Extend(
                                          ObjectManager.Player.ServerPosition.To2D(),
                                          -(GetValue<Slider>("PushDistance").Value/2))
                                      .To3D()).HasFlag(CollisionFlags.Wall)
                          select hero)
                      {
                          E.Cast(hero);
                      }
                      */
                  }
              }
  
              if (LaneClearActive)
              {
                  var useQ = GetValue<bool>("UseQL");
  
                  if (Q.IsReady() && useQ)
                  {
                      var vMinions = MinionManager.GetMinions(ObjectManager.Player.Position, Q.Range);
                      foreach (var minions in
                          vMinions.Where(
                              minions => minions.Health < ObjectManager.Player.GetSpellDamage(minions, SpellSlot.Q)))
                          Q.Cast(minions);
                  }
              }
          }
  
          public void ExecJungleClear()
          {
              var jungleMobs =
                  Marksman.Utils.Utils.GetMobs(Q.Range + Orbwalking.GetRealAutoAttackRange(null) + 65,
                      Marksman.Utils.Utils.MobTypes.All);
  
              if (jungleMobs != null)
              {
                  switch (GetValue<StringList>("UseQJ").SelectedIndex)
                  {
                      case 1:
                      {
                          if (!jungleMobs.BaseSkinName.ToLower().Contains("baron") ||
                              !jungleMobs.BaseSkinName.ToLower().Contains("dragon"))
                          {
                              if (jungleMobs.IsValidTarget(Orbwalking.GetRealAutoAttackRange(null) + 65))
                                  Q.Cast(
                                      jungleMobs.IsValidTarget(Orbwalking.GetRealAutoAttackRange(null) + 65)
                                          ? Game.CursorPos
                                          : jungleMobs.Position);
                          }
                          break;
                      }
                      case 2:
                      {
                          if (!jungleMobs.BaseSkinName.ToLower().Contains("baron") ||
                              !jungleMobs.BaseSkinName.ToLower().Contains("dragon"))
                          {
                              jungleMobs =
                                  Marksman.Utils.Utils.GetMobs(
                                      Q.Range + Orbwalking.GetRealAutoAttackRange(null) + 65,
                                      Marksman.Utils.Utils.MobTypes.BigBoys);
                              if (jungleMobs != null)
                              {
                                  Q.Cast(
                                      jungleMobs.IsValidTarget(Orbwalking.GetRealAutoAttackRange(null) + 65)
                                          ? Game.CursorPos
                                          : jungleMobs.Position);
                              }
                          }
                          break;
                      }
                  }
  
                  switch (GetValue<StringList>("UseEJ").SelectedIndex)
                  {
                      case 1:
                      {
                          if (jungleMobs.IsValidTarget(E.Range))
                              E.CastOnUnit(jungleMobs);
                          break;
                      }
                      case 2:
                      {
                          jungleMobs = Marksman.Utils.Utils.GetMobs(E.Range, Marksman.Utils.Utils.MobTypes.BigBoys);
                          if (jungleMobs != null)
                          {
                              CastE(jungleMobs);
  
                              if (ObjectManager.Player.Distance(jungleMobs) < ObjectManager.Player.AttackRange/2)
                              {
                                  E.CastOnUnit(jungleMobs);
                              }
  
                          }
                          break;
                      }
                  }
              }
          }
  
          public override void Orbwalking_AfterAttack(AttackableUnit unit, AttackableUnit target)
          {
          }
  
          public override bool ComboMenu(Menu config)
          {
              config.AddItem(
                  new MenuItem("Combo.UseQ" + Id, "Use Q").SetValue(
                      new StringList(
                          new[]
                          {"Off", "Tumble to Mouse Cursor", "Just Complete 3rd Silver Buff Mark", "Marksman Settings"}, 1)));
              config.AddItem(
  
gt:                 new MenuItem("UseEC" + Id, "Use E").SetValue(new StringList(