context: using EloBuddy; 
   using LeagueSharp.Common; 
   namespace ElXerath
  {
      using System;
      using System.Collections.Generic;
      using System.Linq;
  
      using LeagueSharp;
      using LeagueSharp.Common;
  
      using SharpDX;
  
      using Color = System.Drawing.Color;
  
      internal enum Spells
      {
          Q,
  
          W,
  
          E,
  
          R
      }
  
      internal class Xerath
      {
          #region Static Fields
  
          public static Orbwalking.Orbwalker Orbwalker;
  
          public static Dictionary<Spells, Spell> spells = new Dictionary<Spells, Spell>
                                                               {
                                                                   { Spells.Q, new Spell(SpellSlot.Q, 1600) },
                                                                   { Spells.W, new Spell(SpellSlot.W, 1000) },
                                                                   { Spells.E, new Spell(SpellSlot.E, 1150) },
                                                                   { Spells.R, new Spell(SpellSlot.R, 5600) }
                                                               };
  
          private static SpellSlot _ignite;
  
          private static int lastNotification;
  
          #endregion
  
          #region Public Properties
  
          public static bool CastingR
          {
              get
              {
                  return ObjectManager.Player.HasBuff("XerathLocusOfPower2")
                         || (ObjectManager.Player.LastCastedSpellName() == "XerathLocusOfPower2"
                             && Environment.TickCount - ObjectManager.Player.LastCastedSpellT() < 500);
              }
          }
  
          #endregion
  
          #region Properties
  
          private static HitChance CustomHitChance
          {
              get
              {
                  return GetHitchance();
              }
          }
  
          private static AIHeroClient Player
          {
              get
              {
                  return ObjectManager.Player;
              }
          }
  
          #endregion
  
          #region Public Methods and Operators
  
          public static void Game_OnGameLoad()
          {
              if (ObjectManager.Player.CharData.BaseSkinName != "Xerath")
              {
                  return;
              }
  
              spells[Spells.Q].SetSkillshot(0.6f, 95f, float.MaxValue, false, SkillshotType.SkillshotLine);
              spells[Spells.W].SetSkillshot(0.7f, 125f, float.MaxValue, false, SkillshotType.SkillshotCircle);
              spells[Spells.E].SetSkillshot(0.25f, 60f, 1400f, true, SkillshotType.SkillshotLine);
              spells[Spells.R].SetSkillshot(0.7f, 130f, float.MaxValue, false, SkillshotType.SkillshotCircle);
              spells[Spells.Q].SetCharged(750, 1550, 1.5f);
              _ignite = Player.GetSpellSlot("summonerdot");
  
              ElXerathMenu.Initialize();
              Game.OnUpdate += OnUpdate;
              Drawing.OnDraw += Drawings.Drawing_OnDraw;
              EloBuddy.Player.OnIssueOrder += AIHeroClient_OnIssueOrder;
              AntiGapcloser.OnEnemyGapcloser += AntiGapcloser_OnEnemyGapcloser;
              Obj_AI_Base.OnProcessSpellCast += AIHeroClient_OnProcessSpellCast;
              Game.OnWndProc += Game_OnWndProc;
          }
  
          #endregion
  
          #region Methods
  
          private static void AntiGapcloser_OnEnemyGapcloser(ActiveGapcloser gapcloser)
          {
              if (!gapcloser.Sender.IsValidTarget(spells[Spells.E].Range)
                  || gapcloser.Sender.Distance(ObjectManager.Player) > spells[Spells.E].Range)
              {
                  return;
              }
  
              if (gapcloser.Sender.IsValidTarget(spells[Spells.E].Range)
                  && (ElXerathMenu.Menu.Item("ElXerath.misc.Antigapcloser").IsActive() && spells[Spells.E].IsReady()))
              {
                  spells[Spells.E].Cast(gapcloser.Sender);
              }
          }
  
          private static void AutoHarassMode()
          {
              var target = TargetSelector.GetTarget(spells[Spells.Q].ChargedMaxRange, TargetSelector.DamageType.Magical);
              var wTarget = TargetSelector.GetTarget(
                  spells[Spells.W].Range + spells[Spells.W].Width * 0.5f,
                  TargetSelector.DamageType.Magical);
  
              if (target == null || !target.IsValidTarget())
              {
                  return;
              }
  
              if (ElXerathMenu.Menu.Item("ElXerath.AutoHarass").GetValue<KeyBind>().Active)
              {
                  var q = ElXerathMenu.Menu.Item("ElXerath.UseQAutoHarass").IsActive();
                  var w = ElXerathMenu.Menu.Item("ElXerath.UseWAutoHarass").IsActive();
                  var mana = ElXerathMenu.Menu.Item("ElXerath.harass.mana").GetValue<Slider>().Value;
  
                  if (Player.ManaPercent < mana)
                  {
                      return;
                  }
  
                  if (q && spells[Spells.Q].IsReady() && target.IsValidTarget(spells[Spells.Q].ChargedMaxRange))
                  {
                      if (!spells[Spells.Q].IsCharging)
                      {
                          spells[Spells.Q].StartCharging();
                          return;
                      }
  
                      if (spells[Spells.Q].IsCharging)
                      {
                          var pred = spells[Spells.Q].GetPrediction(target);
                          if (pred.Hitchance >= CustomHitChance)
                          {
                              spells[Spells.Q].Cast(target);
                          }
                      }
                  }
                  if (wTarget != null && w && spells[Spells.W].IsReady())
                  {
                      var pred = spells[Spells.W].GetPrediction(wTarget);
                      if (pred.Hitchance >= CustomHitChance)
                      {
                          spells[Spells.W].Cast(wTarget);
                      }
                  }
              }
          }
  
          private static void CastR(Obj_AI_Base target)
          {
              var useR = ElXerathMenu.Menu.Item("ElXerath.R.AutoUseR").IsActive();
              var tapkey = ElXerathMenu.Menu.Item("ElXerath.R.OnTap").GetValue<KeyBind>().Active;
              var ultRadius = ElXerathMenu.Menu.Item("ElXerath.R.Radius").GetValue<Slider>().Value;
              var drawROn = ElXerathMenu.Menu.Item("ElXerath.Draw.RON").IsActive();
  
              if (!useR)
              {
                  return;
              }
  
              if (target == null || !target.IsValidTarget())
              {
                  return;
              }
  
              var ultType = ElXerathMenu.Menu.Item("ElXerath.R.Mode").GetValue<StringList>().SelectedIndex;
  
              if (target.Health - spells[Spells.R].GetDamage(target) < 0)
              {
                  if (Utils.TickCount - RCombo.CastSpell <= 700)
                  {
                      return;
                  }
              }
  
              if ((RCombo._index != 0 && target.Distance(RCombo._position) > 1000))
              {
                  if (Utils.TickCount - RCombo.CastSpell <= Math.Min(2500, target.Distance(RCombo._position) - 1000))
                  {
                      return;
                  }
              }
  
              switch (ultType)
              {
                  case 0:
                      spells[Spells.R].Cast(target);
                      break;
  
                  case 1:
                      var d = ElXerathMenu.Menu.Item("Delay" + (RCombo._index + 1)).GetValue<Slider>().Value;
                      if (Utils.TickCount - RCombo.CastSpell > d)
                      {
                          spells[Spells.R].Cast(target);
                      }
                      break;
  
                  case 2:
                      //if (tapkey)
                      if (RCombo._tapKey)
                      {
                          spells[Spells.R].Cast(target);
                      }
                      break;
  
                  case 3:
                      if (spells[Spells.R].GetPrediction(target).Hitchance >= CustomHitChance)
                      {
                          spells[Spells.R].Cast(target);
                      }
  
                      break;
  
                  case 4:
  
                      if (Game.CursorPos.Distance(target.ServerPosition) < ultRadius
                          && ObjectManager.Player.Distance(target.ServerPosition) < spells[Spells.R].Range)
                      {
                          spells[Spells.R].Cast(target);
                      }
  
                      if (drawROn)
                      {
                          Render.Circle.DrawCircle(Game.CursorPos, ultRadius, Color.White);
                      }
  
                      break;
              }
          }
  
          private static void Combo()
          {
              var target = TargetSelector.GetTarget(spells[Spells.Q].ChargedMaxRange, TargetSelector.DamageType.Magical);
              if (!target.IsValidTarget())
              {
                  return;
              }
  
              var comboQ = ElXerathMenu.Menu.Item("ElXerath.Combo.Q").IsActive();
              var comboW = ElXerathMenu.Menu.Item("ElXerath.Combo.W").IsActive();
              var comboE = ElXerathMenu.Menu.Item("ElXerath.Combo.E").IsActive();
  
              if (comboE && spells[Spells.E].IsReady() && Player.Distance(target) < spells[Spells.E].Range)
              {
                  spells[Spells.E].Cast(target);
              }
  
              if (comboW && spells[Spells.W].IsReady())
              {
                  var prediction = spells[Spells.W].GetPrediction(target);
                  if (prediction.Hitchance >= HitChance.VeryHigh)
                  {
                      spells[Spells.W].Cast(prediction.CastPosition);
                  }
              }
  
              if (comboQ && spells[Spells.Q].IsReady() && target.IsValidTarget(spells[Spells.Q].ChargedMaxRange))
              {
                  if (!spells[Spells.Q].IsCharging)
                  {
                      spells[Spells.Q].StartCharging();
                  }
  
                  if (spells[Spells.Q].IsCharging)
                  {
                      var prediction = spells[Spells.Q].GetPrediction(target);
                      if (prediction.Hitchance >= HitChance.VeryHigh)
                      {
                          spells[Spells.Q].Cast(prediction.CastPosition);
                      }
                  }
              }
  
              if (Player.Distance(target) <= 600 && IgniteDamage(target) >= target.Health
                  && ElXerathMenu.Menu.Item("ElXerath.Ignite").IsActive())
              {
                  Player.Spellbook.CastSpell(_ignite, target);
              }
          }
  
          //Thanks to Esk0r for the R
          private static void Game_OnWndProc(WndEventArgs args)
          {
              if (args.Msg == (uint)WindowsMessages.WM_KEYUP)
              {
                  RCombo._tapKey = true;
              }
          }
  
          private static HitChance GetHitchance()
          {
              switch (ElXerathMenu.Menu.Item("ElXerath.hitChance").GetValue<StringList>().SelectedIndex)
              {
                  case 0:
                      return HitChance.Low;
                  case 1:
                      return HitChance.Medium;
                  case 2:
                      return HitChance.High;
                  case 3:
                      return HitChance.VeryHigh;
                  default:
                      return HitChance.Medium;
              }
          }
  
          private static void Harass()
          {
              var target = TargetSelector.GetTarget(spells[Spells.Q].ChargedMaxRange, TargetSelector.DamageType.Magical);
              var wTarget = TargetSelector.GetTarget(
                  spells[Spells.W].Range + spells[Spells.W].Width * 0.5f,
                  TargetSelector.DamageType.Magical);
  
              if (target == null || !target.IsValidTarget())
              {
                  return;
              }
  
              var harassQ = ElXerathMenu.Menu.Item("ElXerath.Harass.Q").IsActive();
              var harassW = ElXerathMenu.Menu.Item("ElXerath.Harass.W").IsActive();
  
              if (wTarget != null && harassW && spells[Spells.W].IsReady())
              {
                  spells[Spells.W].CastIfHitchanceEquals(wTarget, CustomHitChance);
              }
  
              if (harassQ && spells[Spells.Q].IsReady() && spells[Spells.Q].IsInRange(target)
                  && target.IsValidTarget(spells[Spells.Q].ChargedMaxRange))
              {
                  if (!spells[Spells.Q].IsCharging)
                  {
                      spells[Spells.Q].StartCharging();
                      return;
                  }
  
                  if (spells[Spells.Q].IsCharging)
                  {
                      spells[Spells.Q].CastIfHitchanceEquals(target, CustomHitChance);
                  }
              }
          }
  
          private static float IgniteDamage(Obj_AI_Base target)
          {
              if (_ignite == SpellSlot.Unknown || Player.Spellbook.CanUseSpell(_ignite) != SpellState.Ready)
              {
                  return 0f;
              }
              return (float)Player.GetSummonerSpellDamage(target, Damage.SummonerSpell.Ignite);
          }
  
          private static void JungleClear()
          {
              var clearQ = ElXerathMenu.Menu.Item("ElXerath.jclear.Q").IsActive();
              var clearW = ElXerathMenu.Menu.Item("ElXerath.jclear.W").IsActive();
              var clearE = ElXerathMenu.Menu.Item("ElXerath.jclear.E").IsActive();
              var minmana = ElXerathMenu.Menu.Item("minmanaclear").GetValue<Slider>().Value;
  
              if (Player.ManaPercent < minmana)
              {
                  return;
              }
  
              var minions = MinionManager.GetMinions(
                  ObjectManager.Player.ServerPosition,
                  spells[Spells.W].Range,
                  MinionTypes.All,
                  MinionTeam.Neutral,
                  MinionOrderTypes.MaxHealth);
  
  
gt:             if (minions.Count <= 0)