context: using System;
  using System.Collections.Generic;
  using System.Linq;
  using LeagueSharp;
  using LeagueSharp.Common;
  using SPrediction;
  using SharpDX;
  using UnderratedAIO.Helpers;
  using Color = System.Drawing.Color;
  using Environment = UnderratedAIO.Helpers.Environment;
  
  
  using EloBuddy; 
   using LeagueSharp.Common; 
   namespace UnderratedAIO.Champions
  {
      internal class Sejuani
      {
          public static Menu config;
          private static Orbwalking.Orbwalker orbwalker;
          private static readonly AIHeroClient me = ObjectManager.Player;
          public static Spell Q, W, E, R;
          public static AutoLeveler autoLeveler;
  
          public Sejuani()
          {
              InitSejuani();
              InitMenu();
              //Chat.Print("<font color='#9933FF'>Soresu </font><font color='#FFFFFF'>- Sejuani</font>");
              Game.OnUpdate += Game_OnGameUpdate;
              Drawing.OnDraw += Game_OnDraw;
              AntiGapcloser.OnEnemyGapcloser += OnEnemyGapcloser;
              Interrupter2.OnInterruptableTarget += OnPossibleToInterrupt;
              Orbwalking.AfterAttack += AfterAttack;
              Jungle.setSmiteSlot();
              HpBarDamageIndicator.DamageToUnit = ComboDamage;
          }
  
          private void AfterAttack(AttackableUnit unit, AttackableUnit target)
          {
              if (unit.IsMe && orbwalker.ActiveMode == Orbwalking.OrbwalkingMode.Combo && W.IsReady() &&
                  config.Item("usew", true).GetValue<bool>())
              {
                  W.Cast();
                  Orbwalking.ResetAutoAttackTimer();
              }
          }
  
          private void OnPossibleToInterrupt(AIHeroClient unit, Interrupter2.InterruptableTargetEventArgs args)
          {
              if (config.Item("useqint", true).GetValue<bool>())
              {
                  if (unit.IsValidTarget(Q.Range) && Q.IsReady() && me.Distance(unit) < Q.Range)
                  {
                      Q.Cast(unit.Position);
                  }
              }
              if (config.Item("userint", true).GetValue<bool>())
              {
                  if (unit.IsValidTarget(R.Range) && R.IsReady() && me.Distance(unit) < R.Range)
                  {
                      R.Cast(unit.Position);
                  }
              }
          }
  
          private static void Game_OnDraw(EventArgs args)
          {
              DrawHelper.DrawCircle(config.Item("drawqq", true).GetValue<Circle>(), Q.Range);
              DrawHelper.DrawCircle(config.Item("drawww", true).GetValue<Circle>(), W.Range);
              DrawHelper.DrawCircle(config.Item("drawee", true).GetValue<Circle>(), E.Range);
              DrawHelper.DrawCircle(config.Item("drawrr", true).GetValue<Circle>(), R.Range);
  
              HpBarDamageIndicator.Enabled = config.Item("drawcombo").GetValue<bool>();
          }
  
          private static void Game_OnGameUpdate(EventArgs args)
          {
              if (FpsBalancer.CheckCounter())
              {
                  return;
              }
              switch (orbwalker.ActiveMode)
              {
                  case Orbwalking.OrbwalkingMode.Combo:
                      Combo();
                      break;
                  case Orbwalking.OrbwalkingMode.Mixed:
                      //if (!minionBlock) Harass();
                      break;
                  case Orbwalking.OrbwalkingMode.LaneClear:
                      Clear();
                      break;
                  case Orbwalking.OrbwalkingMode.LastHit:
                      break;
                  default:
                      break;
              }
  
              if (config.Item("manualR", true).GetValue<KeyBind>().Active && R.IsReady())
              {
                  CastR();
              }
          }
  
          private static void CastR()
          {
              EloBuddy.Player.IssueOrder(GameObjectOrder.MoveTo, Game.CursorPos);
              foreach (var enemy in
                  HeroManager.Enemies.Where(i => !i.IsDead && me.Distance(i) < R.Range)
                      .OrderByDescending(l => l.CountEnemiesInRange(350f)))
              {
                  R.CastIfHitchanceEquals(enemy, HitChance.High);
                  break;
              }
          }
  
          private static void OnEnemyGapcloser(ActiveGapcloser gapcloser)
          {
              if (config.Item("useqgc", true).GetValue<bool>())
              {
                  if (gapcloser.Sender.IsValidTarget(Q.Range) && Q.IsReady() && me.Distance(gapcloser.End) < Q.Range)
                  {
                      Q.Cast(gapcloser.End);
                  }
              }
              if (config.Item("usergc", true).GetValue<bool>())
              {
                  if (gapcloser.Sender.IsValidTarget(R.Range) && R.IsReady() && me.Distance(gapcloser.End) < R.Range)
                  {
                      R.Cast(gapcloser.End);
                  }
              }
          }
  
          private static void Clear()
          {
              var minions =
                  MinionManager.GetMinions(400, MinionTypes.All, MinionTeam.NotAlly)
                      .Where(m => m.IsValidTarget(400))
                      .ToList();
              if (minions.Count() > 2)
              {
                  if (Items.HasItem(3077) && Items.CanUseItem(3077))
                  {
                      Items.UseItem(3077);
                  }
                  if (Items.HasItem(3074) && Items.CanUseItem(3074))
                  {
                      Items.UseItem(3074);
                  }
              }
              float perc = (float) config.Item("minmana", true).GetValue<Slider>().Value / 100f;
              if (me.Mana < me.MaxMana * perc)
              {
                  return;
              }
  
              Q.SetSkillshot(
                  Q.Instance.SData.SpellCastTime, Q.Instance.SData.LineWidth, Q.Instance.SData.MissileSpeed, false,
                  SkillshotType.SkillshotLine);
              var minionsSpells = MinionManager.GetMinions(W.Range, MinionTypes.All, MinionTeam.NotAlly);
              if (W.IsReady() && minionsSpells.Count() > 1 && config.Item("usewC", true).GetValue<bool>() &&
                  me.Spellbook.GetSpell(SpellSlot.W).SData.Mana <= me.Mana)
              {
                  W.Cast();
              }
              var minHit = config.Item("useeCmin", true).GetValue<Slider>().Value;
              if (E.IsReady() && me.Spellbook.GetSpell(SpellSlot.Q).SData.Mana <= me.Mana &&
                  CombatHelper.SejuaniCountFrostMinion(E.Range) >= minHit &&
                  (!(!Q.IsReady() && me.Mana - me.Spellbook.GetSpell(SpellSlot.Q).SData.Mana < me.MaxMana * perc) ||
                   !(!W.IsReady() && me.Mana - me.Spellbook.GetSpell(SpellSlot.W).SData.Mana < me.MaxMana * perc)))
              {
                  E.Cast();
              }
              if (config.Item("useqC", true).GetValue<bool>() && Q.IsReady() &&
                  me.Spellbook.GetSpell(SpellSlot.Q).SData.Mana <= me.Mana)
              {
                  var minionsForQ = MinionManager.GetMinions(
                      ObjectManager.Player.ServerPosition, Q.Range, MinionTypes.All, MinionTeam.NotAlly);
                  MinionManager.FarmLocation bestPosition = Q.GetLineFarmLocation(minionsForQ);
                  if (bestPosition.Position.IsValid())
                  {
                      if (bestPosition.MinionsHit >= 2)
                      {
                          Q.Cast(bestPosition.Position);
                      }
                  }
              }
  
              Q.SetSkillshot(
                  Q.Instance.SData.SpellCastTime, Q.Instance.SData.LineWidth, Q.Instance.SData.MissileSpeed, true,
                  SkillshotType.SkillshotLine);
          }
  
          private static void Ulti()
          {
              if (!R.IsReady() || config.Item("useRmin", true).GetValue<Slider>().Value == 0)
              {
                  return;
              }
              var target = DrawHelper.GetBetterTarget(R.Range, TargetSelector.DamageType.Magical);
              if (config.Item("useRmin", true).GetValue<Slider>().Value == 1)
              {
                  if (target != null && !config.Item("ult" + target.BaseSkinName, true).GetValue<bool>())
                  {
                      R.Cast(target);
                  }
              }
              else
              {
                  foreach (var enemy in
                      HeroManager.Enemies.Where(
                          i =>
                              i.IsValidTarget() && me.Distance(i) < R.Range &&
                              me.Distance(i) >= config.Item("useRminr", true).GetValue<Slider>().Value &&
                              !config.Item("ult" + i.BaseSkinName, true).GetValue<bool>() &&
                              i.Position.CountEnemiesInRange(350f) >=
                              config.Item("useRmin", true).GetValue<Slider>().Value && target.Distance(i.Position) < 350f)
                          .OrderByDescending(l => l.Position.CountEnemiesInRange(350f)))
                  {
                      R.Cast(enemy);
                      return;
                  }
              }
          }
  
          private static void Combo()
          {
              Ulti();
              float perc = (float) config.Item("minmana", true).GetValue<Slider>().Value / 100f;
              var minHit = config.Item("useemin", true).GetValue<Slider>().Value;
              AIHeroClient target = DrawHelper.GetBetterTarget(R.Range, TargetSelector.DamageType.Magical);
              if (target == null)
              {
                  return;
              }
              if (config.Item("useItems").GetValue<bool>())
              {
                  ItemHandler.UseItems(target, config, ComboDamage(target));
              }
  
              var buffs = CombatHelper.SejuaniCountFrostHero(E.Range);
              if (E.IsReady() && me.Distance(target.Position) < E.Range && buffs > 0 &&
                  ((buffs > minHit) || (Damage.GetSpellDamage(me, target, SpellSlot.E) >= target.Health) ||
                   (me.Distance(target) > config.Item("useEminr", true).GetValue<Slider>().Value &&
                    me.Distance(target) < E.Range && buffs == 1)))
              {
                  if (!(Q.IsReady() && me.Mana - me.Spellbook.GetSpell(SpellSlot.Q).SData.Mana < me.MaxMana * perc) ||
                      !(W.IsReady() && me.Mana - me.Spellbook.GetSpell(SpellSlot.W).SData.Mana < me.MaxMana * perc))
                  {
                      E.Cast();
                  }
              }
              if (Q.IsReady() && config.Item("useq", true).GetValue<bool>() &&
                  me.Distance(target.Position) > config.Item("useQminr", true).GetValue<Slider>().Value)
              {
  
gt:                 var hits = Q.GetHitCount(HitChance.High);