context: using System;
  using System.Linq;
  using LeagueSharp;
  using LeagueSharp.Common;
  using SebbyLib;
  
  using EloBuddy; 
  using LeagueSharp.Common; 
  namespace OneKeyToWin_AIO_Sebby.Champions
  {
      class Anivia : Base
      {
          private float RCastTime = 0;
          private int Rwidth = 400;
          private static GameObject QMissile, RMissile;
  
          public Anivia()
          {
              Q = new Spell(SpellSlot.Q, 1000);
              W = new Spell(SpellSlot.W, 950);
              E = new Spell(SpellSlot.E, 650);
              R = new Spell(SpellSlot.R, 685);
  
              Q.SetSkillshot(0.25f, 110f, 870f, false, SkillshotType.SkillshotLine);
              W.SetSkillshot(0.6f, 1f, float.MaxValue, false, SkillshotType.SkillshotLine);
              R.SetSkillshot(0.5f, 200f, float.MaxValue, false, SkillshotType.SkillshotCircle);
  
              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("Draw").AddItem(new MenuItem("onlyRdy", "Draw only ready spells", true).SetValue(true));
  
              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("AGCQ", "Q gapcloser", true).SetValue(false));
              Config.SubMenu(Player.ChampionName).SubMenu("Q Config").AddItem(new MenuItem("harassQ", "Harass 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("AGCW", "AntiGapcloser W", true).SetValue(false));
              Config.SubMenu(Player.ChampionName).SubMenu("W Config").AddItem(new MenuItem("inter", "OnPossibleToInterrupt 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("R Config").AddItem(new MenuItem("autoR", "Auto R", true).SetValue(true));
  
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("farmE", "Lane clear E", true).SetValue(false));
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("farmR", "Lane clear R", true).SetValue(false));
              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));
              Config.SubMenu(Player.ChampionName).SubMenu("Farm").AddItem(new MenuItem("jungleR", "Jungle clear R", true).SetValue(true));
  
  
              Config.SubMenu(Player.ChampionName).AddItem(new MenuItem("AACombo", "Disable AA if can use E", true).SetValue(true));
  
              Game.OnUpdate += Game_OnGameUpdate;
              GameObject.OnDelete += Obj_AI_Base_OnDelete;
              GameObject.OnCreate += Obj_AI_Base_OnCreate;
              Interrupter2.OnInterruptableTarget += Interrupter2_OnInterruptableTarget;
              AntiGapcloser.OnEnemyGapcloser += AntiGapcloser_OnEnemyGapcloser;
              Drawing.OnDraw += Drawing_OnDraw;
          }
  
          private void Interrupter2_OnInterruptableTarget(AIHeroClient sender, Interrupter2.InterruptableTargetEventArgs args)
          {
              if (Config.Item("inter", true).GetValue<bool>() && W.IsReady() && sender.IsValidTarget(W.Range))
                  W.Cast(sender);
          }
  
          private void AntiGapcloser_OnEnemyGapcloser(ActiveGapcloser gapcloser)
          {
              var Target = gapcloser.Sender;
              if (Q.IsReady() && Config.Item("AGCQ", true).GetValue<bool>())
              {
                  if (Target.IsValidTarget(300))
                  {
                      Q.Cast(Target);
                      Program.debug("AGC Q");
                  }
              }
              else if (W.IsReady() && Config.Item("AGCW", true).GetValue<bool>())
              {
                  if (Target.IsValidTarget(W.Range))
                  {
                      W.Cast(ObjectManager.Player.Position.Extend(Target.Position, 50), true);
                  }
              }
          }
  
          private void Obj_AI_Base_OnCreate(GameObject obj, EventArgs args)
          {
              if (obj.IsValid)
              {
                  if (obj.Name == "cryo_FlashFrost_Player_mis.troy")
                  {
                      QMissile = obj;
                  }
                  if (obj.Name.Contains("cryo_storm"))
                  {
                      RMissile = obj;
                      Program.debug("dupa");
                  }
              }
          }
  
          private void Obj_AI_Base_OnDelete(GameObject obj, EventArgs args)
          {
              if (obj.IsValid)
              {
                  if (obj.Name == "cryo_FlashFrost_Player_mis.troy")
                      QMissile = null;
                  if (obj.Name.Contains("cryo_storm"))
                      RMissile = null;
              }
          }
  
          private void Game_OnGameUpdate(EventArgs args)
          {
              if (Program.Combo && Config.Item("AACombo", true).GetValue<bool>())
              {
                  if (!E.IsReady())
                      SebbyLib.Orbwalking.Attack = true;
  
                  else
                      SebbyLib.Orbwalking.Attack = false;
              }
              else
                  SebbyLib.Orbwalking.Attack = true;
  
              if (Q.IsReady() && QMissile != null && QMissile.Position.CountEnemiesInRange(230) > 0)
                  Q.Cast();
              
  
              if (Program.LagFree(0))
              {
                  SetMana();
              }
  
              if (Program.LagFree(1) && R.IsReady() && Config.Item("autoR", true).GetValue<bool>())
                  LogicR();
  
              if (Program.LagFree(2) && W.IsReady() && Config.Item("autoW", true).GetValue<bool>())
                  LogicW();
  
              if (Program.LagFree(3) && Q.IsReady() && QMissile == null && Config.Item("autoQ", true).GetValue<bool>())
                  LogicQ();
  
              if (Program.LagFree(4) )
              {
                  if(E.IsReady() && Config.Item("autoE", true).GetValue<bool>())
                      LogicE();
  
                  Jungle();
              }
          }
  
          private void LogicQ()
          {
              var t = TargetSelector.GetTarget(Q.Range, TargetSelector.DamageType.Magical);
              if (t.IsValidTarget())
              {
                  if (Program.Combo && Player.Mana > EMANA + QMANA - 10)
                      Program.CastSpell(Q, t);
                  else if (Program.Harass && Config.Item("harassQ", true).GetValue<bool>() && Config.Item("Harass" + t.ChampionName).GetValue<bool>() && Player.Mana > RMANA + EMANA + QMANA + WMANA && OktwCommon.CanHarras())
                  {
                      Program.CastSpell(Q, t);
                  }
                  else
                  {
                      var qDmg = OktwCommon.GetKsDamage(t,Q);
                      var eDmg = E.GetDamage(t);
                      if (qDmg > t.Health)
                          Program.CastSpell(Q, t);
                      else if (qDmg + eDmg > t.Health && Player.Mana > QMANA + WMANA)
                          Program.CastSpell(Q, t);
                  }
                  if (!Program.None && Player.Mana > RMANA + EMANA)
                  {
                      foreach (var enemy in HeroManager.Enemies.Where(enemy => enemy.IsValidTarget(Q.Range) && !OktwCommon.CanMove(enemy)))
                          Q.Cast(enemy, true);
                  }
              }
          }
  
          private void LogicW()
          {
              if (Program.Combo && Player.Mana > RMANA + EMANA + WMANA)
              {
                  var t = TargetSelector.GetTarget(W.Range, TargetSelector.DamageType.Magical);
                  if (t.IsValidTarget(W.Range) && W.GetPrediction(t).CastPosition.Distance(t.Position) > 100)
                  {
                      if (Player.Position.Distance(t.ServerPosition) > Player.Position.Distance(t.Position))
                      {
                          if (t.Position.Distance(Player.ServerPosition) < t.Position.Distance(Player.Position))
                              Program.CastSpell(W, t);
                      }
                      else
                      {
                          if (t.Position.Distance(Player.ServerPosition) > t.Position.Distance(Player.Position) && t.Distance(Player) < R.Range)
                              Program.CastSpell(W, t);
                      }
                  }
              }
          }
          
          private void LogicE()
          {
              var t = TargetSelector.GetTarget(E.Range, TargetSelector.DamageType.Magical);
              if (t.IsValidTarget())
              {
                  var qCd = Q.Instance.CooldownExpires - Game.Time;
                  var rCd = R.Instance.CooldownExpires - Game.Time;
                  if (Player.Level < 7)
                      rCd = 10;
                  //debug("Q " + qCd + "R " + rCd + "E now " + E.Instance.Cooldown);
  
gt:                 var eDmg = OktwCommon.GetKsDamage(t, E);