context: using System;
  using System.Linq;
  using LeagueSharp;
  using LeagueSharp.Common;
  using SharpDX;
  using SebbyLib;
  
  using EloBuddy; 
  using LeagueSharp.Common; 
  namespace OneKeyToWin_AIO_Sebby.Champions
  {
      class Malzahar : Base
      {
          private float Rtime = 0;
  
          public Malzahar()
          {
              Q = new Spell(SpellSlot.Q, 900);
              Q1 = new Spell(SpellSlot.Q, 900);
              W = new Spell(SpellSlot.W, 750);
              E = new Spell(SpellSlot.E, 650);
              R = new Spell(SpellSlot.R, 700);
  
              Q1.SetSkillshot(0.25f, 100, float.MaxValue, false, SkillshotType.SkillshotCircle);
              Q.SetSkillshot(0.75f, 80, float.MaxValue, false, SkillshotType.SkillshotCircle);
              W.SetSkillshot(1.2f, 230, float.MaxValue, false, SkillshotType.SkillshotCircle);
  
              Config.SubMenu(Player.ChampionName).SubMenu("Draw").AddItem(new MenuItem("noti", "Show notification & line", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Draw").AddItem(new MenuItem("onlyRdy", "Draw only ready spells", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Draw").AddItem(new MenuItem("qRange", "Q range", true).SetValue(false));
              Config.SubMenu(Player.ChampionName).SubMenu("Draw").AddItem(new MenuItem("wRange", "W range", true).SetValue(false));
              Config.SubMenu(Player.ChampionName).SubMenu("Draw").AddItem(new MenuItem("eRange", "E range", true).SetValue(false));
              Config.SubMenu(Player.ChampionName).SubMenu("Draw").AddItem(new MenuItem("rRange", "R range", true).SetValue(false));
  
              Config.SubMenu(Player.ChampionName).SubMenu("Q Config").AddItem(new MenuItem("autoQ", "Auto Q", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Q Config").AddItem(new MenuItem("harassQ", "Harass Q", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Q Config").AddItem(new MenuItem("intQ", "Interrupt spells Q", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Q Config").AddItem(new MenuItem("gapQ", "Gapcloser Q", true).SetValue(true));
  
              Config.SubMenu(Player.ChampionName).SubMenu("W Config").AddItem(new MenuItem("autoW", "Auto W", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("W Config").AddItem(new MenuItem("harassW", "Harass W", true).SetValue(true));
  
              Config.SubMenu(Player.ChampionName).SubMenu("E Config").AddItem(new MenuItem("autoE", "Auto E", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("E Config").AddItem(new MenuItem("harassE", "Harass E", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("E Config").AddItem(new MenuItem("harrasEminion", "Try harras E on minion", true).SetValue(true));
  
              
              Config.SubMenu(Player.ChampionName).SubMenu("R Config").AddItem(new MenuItem("autoR", "Auto R", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("R Config").AddItem(new MenuItem("Rturrent", "Don't R under turret", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("R Config").AddItem(new MenuItem("smartR", "Semi-manual cast R key", true).SetValue(new KeyBind("T".ToCharArray()[0], KeyBindType.Press)));
  
              foreach (var enemy in HeroManager.Enemies)
                  Config.SubMenu(Player.ChampionName).SubMenu("R Config").SubMenu("Gapcloser").AddItem(new MenuItem("gapcloser" + enemy.ChampionName, enemy.ChampionName).SetValue(false));
  
  
              Config.SubMenu(Player.ChampionName).SubMenu("Misc").AddItem(new MenuItem("useR", "Fast combo key", true).SetValue(new KeyBind("T".ToCharArray()[0], KeyBindType.Press))); //32 == space
              foreach (var enemy in HeroManager.Enemies)
                  Config.SubMenu(Player.ChampionName).SubMenu("Misc").SubMenu("Fast combo key use on:").AddItem(new MenuItem("Ron" + enemy.ChampionName, enemy.ChampionName).SetValue(true));
  
             
  
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("farmQ", "Lane clear Q", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("farmW", "Lane clear W", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("farmE", "Lane clear E", true).SetValue(true));
  
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("jungleE", "Jungle clear E", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("jungleQ", "Jungle clear Q", true).SetValue(true));
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("jungleW", "Jungle clear W", true).SetValue(true));
  
              Game.OnUpdate += Game_OnGameUpdate;
              Drawing.OnDraw += Drawing_OnDraw;
              Interrupter2.OnInterruptableTarget += Interrupter2_OnInterruptableTarget;
              AntiGapcloser.OnEnemyGapcloser += AntiGapcloser_OnEnemyGapcloser;
              Spellbook.OnCastSpell += Spellbook_OnCastSpell;
          }
  
          private void Spellbook_OnCastSpell(Spellbook sender, SpellbookCastSpellEventArgs args)
          {
  
              if (args.Slot == SpellSlot.R && !Config.Item("smartR", true).GetValue<KeyBind>().Active)
              {
                  var t = args.Target as AIHeroClient;
                  if (t != null && t.Health - OktwCommon.GetIncomingDamage(t) > R.GetDamage(t) * 2.5)
                  {
                      if (E.IsReady() && Player.Mana > RMANA + EMANA)
                      {
                          E.CastOnUnit(t);
                          args.Process = false;
                          return;
                      }
  
                      if (W.IsReady() && Player.Mana > RMANA + WMANA)
                      {
                          W.Cast(t.Position);
                          args.Process = false;
                          return;
                      }
  
                      if (Q.IsReady() && t.IsValidTarget(Q.Range) && Player.Mana > RMANA + QMANA)
                      {
                          Q1.Cast(t);
                          args.Process = false;
                          return;
                      }
  
                  }
                  if(R.IsReady() && t.IsValidTarget())
                       Rtime = Game.Time;
                  
              }
          }
  
          private void AntiGapcloser_OnEnemyGapcloser(ActiveGapcloser gapcloser)
          {
  
              var t = gapcloser.Sender;
  
              if (Q.IsReady() && Config.Item("gapQ", true).GetValue<bool>() && t.IsValidTarget(Q.Range))
              {
                  Q.Cast(gapcloser.End);
              }
              else if (R.IsReady() && Config.Item("gapcloser" + gapcloser.Sender.ChampionName).GetValue<bool>() && t.IsValidTarget(R.Range))
              {
                  R.CastOnUnit(t);
              }
          }
  
          private void Interrupter2_OnInterruptableTarget(AIHeroClient t, Interrupter2.InterruptableTargetEventArgs args)
          {
              if (!Config.Item("intQ", true).GetValue<bool>() || !Q.IsReady())
                  return;
  
              if (t.IsValidTarget(Q.Range))
              {
                   Q.Cast(t);
              }
          }
  
          private void Game_OnGameUpdate(EventArgs args)
          {
              if (Player.IsChannelingImportantSpell() || Game.Time - Rtime < 2.5 || Player.HasBuff("malzaharrsound"))
              {
                  Program.debug("R chaneling");
                  OktwCommon.blockMove = true;
                  OktwCommon.blockAttack = true;
                  OktwCommon.blockSpells = true;
                  Orbwalking.Attack = false;
                  Orbwalking.Move = false;
                  return;
              }
              else
              {
                  OktwCommon.blockSpells = false;
                  OktwCommon.blockMove = false;
                  OktwCommon.blockAttack = false;
                  Orbwalking.Attack = true;
                  Orbwalking.Move = true;
              }
  
              
  
              if (R.IsReady() && Config.Item("useR", true).GetValue<KeyBind>().Active)
              {
                  if (Config.Item("useR", true).GetValue<KeyBind>().Active)
                  {
                      var t = TargetSelector.GetTarget(R.Range, TargetSelector.DamageType.Magical);
                      if (t.IsValidTarget(R.Range) && Config.Item("Ron" + t.ChampionName).GetValue<bool>() && OktwCommon.ValidUlt(t))
                      {
                          R.CastOnUnit(t);
                          return;
                      }
                  }
                  else if (Config.Item("smartR", true).GetValue<KeyBind>().Active)
                  {
                      var t = TargetSelector.GetTarget(R.Range, TargetSelector.DamageType.Magical);
                      if (t.IsValidTarget(R.Range) && OktwCommon.ValidUlt(t))
                      {
                          R.CastOnUnit(t);
                          return;
                      }
                  }
              }
  
              if (Program.LagFree(0))
              {
                  SetMana();
                  Jungle();
              }
  
              if (Program.LagFree(1) && E.IsReady() && Config.Item("autoE", true).GetValue<bool>())
                  LogicE();
              if (Program.LagFree(2) && Q.IsReady() && Config.Item("autoQ", true).GetValue<bool>())
                  LogicQ();
              if (Program.LagFree(3) && W.IsReady() && Config.Item("autoW", true).GetValue<bool>())
                  LogicW();
              if (Program.LagFree(4) && R.IsReady() && Config.Item("autoR", true).GetValue<bool>())
                  LogicR();
          }
  
          private void LogicQ()
          {
              var t = TargetSelector.GetTarget(Q.Range, TargetSelector.DamageType.Magical);
              if (t.IsValidTarget())
              {
                  var qDmg = OktwCommon.GetKsDamage(t, Q) + BonusDmg(t);
  
                  if (qDmg > t.Health)
                      Program.CastSpell(Q, t);
  
                  if (R.IsReady() && t.IsValidTarget(R.Range))
                  {
                      return;
                  }
                  if (Program.Combo && Player.Mana > RMANA + QMANA)
                      Program.CastSpell(Q, t);
                  else if (Program.Harass && Config.Item("harassQ", true).GetValue<bool>() && Player.Mana > RMANA + EMANA + WMANA + EMANA && Config.Item("Harass" + t.ChampionName).GetValue<bool>())
                      Program.CastSpell(Q, t);
  
                  if (Player.Mana > RMANA + QMANA)
                  {
                      foreach (var enemy in HeroManager.Enemies.Where(enemy => enemy.IsValidTarget(Q.Range) && !OktwCommon.CanMove(enemy)))
                          Q.Cast(enemy);
                  }
              }
              else if (FarmSpells && Config.Item("farmQ", true).GetValue<bool>() )
              {
                  var allMinions = Cache.GetMinions(Player.ServerPosition, Q.Range);
                  var farmPos = Q.GetCircularFarmLocation(allMinions, 150);
                  if (farmPos.MinionsHit >= FarmMinions)
                      Q.Cast(farmPos.Position);
              }
          }
  
          private void LogicW()
          {
              var t = TargetSelector.GetTarget(W.Range, TargetSelector.DamageType.Magical);
              if (t.IsValidTarget())
              {
  
gt:                 var qDmg = Q.GetDamage(t);