context: using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net.WebSockets;
  using System.Runtime.Remoting.Messaging;
  using Color = System.Drawing.Color;
  using LeagueSharp;
  using LeagueSharp.Common;
  using SharpDX;
  using UnderratedAIO.Helpers;
  using Environment = UnderratedAIO.Helpers.Environment;
  
  
  using EloBuddy; 
  using LeagueSharp.Common; 
  namespace UnderratedAIO.Champions
  {
      internal class Garen
      {
          public static Menu config;
          private static Orbwalking.Orbwalker orbwalker;
          public static readonly AIHeroClient player = ObjectManager.Player;
          public static Spell Q, W, E, R;
          
  
          public Garen()
          {
              InitGaren();
              InitMenu();
              //Chat.Print("<font color='#9933FF'>Soresu </font><font color='#FFFFFF'>- Garen</font>");
              Game.OnUpdate += Game_OnGameUpdate;
              Orbwalking.AfterAttack += AfterAttack;
              Drawing.OnDraw += Game_OnDraw;
              Jungle.setSmiteSlot();
              
          }
  
          private void Game_OnGameUpdate(EventArgs args)
          {
              if (GarenE)
              {
                  orbwalker.SetMovement(false);
                  orbwalker.SetAttack(false);
                  if (orbwalker.ActiveMode != Orbwalking.OrbwalkingMode.None)
                  {
                      EloBuddy.Player.IssueOrder(GameObjectOrder.MoveTo, Game.CursorPos);
                  }
              }
              else
              {
                  orbwalker.SetAttack(true);
                  orbwalker.SetMovement(true);
              }
              if (false)
              {
                  return;
              }
              switch (orbwalker.ActiveMode)
              {
                  case Orbwalking.OrbwalkingMode.Combo:
                      Combo();
                      break;
                  case Orbwalking.OrbwalkingMode.Mixed:
                      break;
                  case Orbwalking.OrbwalkingMode.LaneClear:
                      Clear();
                      break;
                  case Orbwalking.OrbwalkingMode.LastHit:
                      break;
                  default:
                      break;
              }
          }
  
          private void Clear()
          {
              if (config.Item("useeLC", true).GetValue<bool>() && E.IsReady() && !GarenE &&
                  Environment.Minion.countMinionsInrange(player.Position, E.Range) > 2)
              {
                  E.Cast();
              }
          }
  
          private void AfterAttack(AttackableUnit unit, AttackableUnit target)
          {
              if (unit.IsMe && Q.IsReady() && config.Item("useqAAA", true).GetValue<bool>() && !GarenE && target.IsEnemy &&
                  target is AIHeroClient)
              {
                  Q.Cast();
                  EloBuddy.Player.IssueOrder(GameObjectOrder.AutoAttack, target);
              }
          }
  
          private static bool GarenE
          {
              get { return player.Buffs.Any(buff => buff.Name == "GarenE"); }
          }
  
          private static bool GarenQ
          {
              get { return player.Buffs.Any(buff => buff.Name == "GarenQ"); }
          }
  
          private void Combo()
          {
              AIHeroClient target = DrawHelper.GetBetterTarget(700, TargetSelector.DamageType.Physical);
              if (target == null)
              {
                  return;
              }
              var combodamage = ComboDamage(target);
              if (config.Item("useItems").GetValue<bool>() && !GarenQ)
              {
                  ItemHandler.UseItems(target, config, combodamage);
              }
              bool hasIgnite = player.Spellbook.CanUseSpell(player.GetSpellSlot("SummonerDot")) == SpellState.Ready;
              var ignitedmg = (float) player.GetSummonerSpellDamage(target, Damage.SummonerSpell.Ignite);
              if (config.Item("useIgnite").GetValue<bool>() && hasIgnite &&
                  ((R.IsReady() && ignitedmg + R.GetDamage(target) > target.Health) || ignitedmg > target.Health) &&
                  (target.Distance(player) > E.Range || player.HealthPercent < 20))
              {
                  player.Spellbook.CastSpell(player.GetSpellSlot("SummonerDot"), target);
              }
              if (config.Item("useq", true).GetValue<bool>() && Q.IsReady() &&
                  player.Distance(target) > player.AttackRange && !GarenE && !GarenQ &&
                  player.Distance(target) > Orbwalking.GetRealAutoAttackRange(target) &&
                  CombatHelper.IsPossibleToReachHim(target, 0.30f, new float[5] { 1.5f, 2f, 2.5f, 3f, 3.5f }[Q.Level - 1]))
              {
                  Q.Cast();
              }
              if (config.Item("useq", true).GetValue<bool>() && Q.IsReady() && !GarenQ &&
                  (!GarenE || (Q.IsReady() && Damage.GetSpellDamage(player, target, SpellSlot.Q) > target.Health)))
              {
                  if (GarenE)
                  {
                      E.Cast();
                  }
                  Q.Cast();
                  EloBuddy.Player.IssueOrder(GameObjectOrder.AutoAttack, target);
              }
              if (config.Item("usee", true).GetValue<bool>() && E.IsReady() && !Q.IsReady() && !GarenQ && !GarenE &&
                  !Orbwalking.CanAttack() && !player.Spellbook.IsAutoAttacking && player.CountEnemiesInRange(E.Range) > 0)
              {
                  E.Cast();
              }
              var targHP = target.Health + 20 - CombatHelper.IgniteDamage(target);
              var rLogic = config.Item("user", true).GetValue<bool>() && R.IsReady() && target.IsValidTarget() &&
                           (!config.Item("ult" + target.BaseSkinName, true).GetValue<bool>() ||
                            player.CountEnemiesInRange(1500) == 1) && getRDamage(target) > targHP && targHP > 0;
              if (rLogic && target.Distance(player) < R.Range)
              {
                  if (!(GarenE && target.Health < getEDamage(target, true) && target.Distance(player) < E.Range))
                  {
                      if (GarenE)
                      {
                          E.Cast();
                      }
                      else
                      {
                          R.Cast(target);
                      }
                  }
              }
              var data = Program.IncDamages.GetAllyData(player.NetworkId);
              if (config.Item("usew", true).GetValue<bool>() && W.IsReady() && target.IsFacing(player) &&
                  data.DamageTaken > 40)
              {
                  W.Cast();
              }
              bool hasFlash = player.Spellbook.CanUseSpell(player.GetSpellSlot("SummonerFlash")) == SpellState.Ready;
              if (config.Item("useFlash", true).GetValue<bool>() && hasFlash && rLogic &&
                  target.Distance(player) < R.Range + 425 && target.Distance(player) > R.Range + 250 && !Q.IsReady() &&
                  !CombatHelper.IsFacing(target, player.Position) && !GarenQ)
              {
                  if (target.Distance(player) < R.Range + 300 && player.MoveSpeed > target.MoveSpeed)
                  {
                      return;
                  }
                  if (GarenE)
                  {
                      E.Cast();
                  }
                  else if (!player.Position.Extend(target.Position, 425f).IsWall()) {}
                  {
                      player.Spellbook.CastSpell(
                          player.GetSpellSlot("SummonerFlash"), player.Position.Extend(target.Position, 425f));
                  }
              }
          }
  
          private void Game_OnDraw(EventArgs args)
          {
              DrawHelper.DrawCircle(config.Item("drawaa", true).GetValue<Circle>(), player.AttackRange);
              DrawHelper.DrawCircle(config.Item("drawee", true).GetValue<Circle>(), E.Range);
              DrawHelper.DrawCircle(config.Item("drawrr", true).GetValue<Circle>(), R.Range);
              
              if (R.IsReady() && config.Item("drawrkillable", true).GetValue<bool>())
              {
                  foreach (var e in HeroManager.Enemies.Where(e => e.IsValid && e.IsHPBarRendered))
                  {
                      if (e.Health < getRDamage(e))
                      {
                          Render.Circle.DrawCircle(e.Position, 157, Color.Gold, 12);
                      }
                  }
              }
          }
  
          private float ComboDamage(AIHeroClient hero)
          {
              double damage = 0;
              if (R.IsReady())
              {
                  damage += getRDamage(hero);
              }
              damage += ItemHandler.GetItemsDamage(hero);
  
              if ((Items.HasItem(ItemHandler.Bft.Id) && Items.CanUseItem(ItemHandler.Bft.Id)) ||
                  (Items.HasItem(ItemHandler.Dfg.Id) && Items.CanUseItem(ItemHandler.Dfg.Id)))
              {
                  damage = (float) (damage * 1.2);
              }
              if (Q.IsReady() && !GarenQ)
              {
                  damage += Damage.GetSpellDamage(player, hero, SpellSlot.Q);
              }
              if (E.IsReady() && !GarenE)
              {
                  damage += getEDamage(hero);
              }
              else if (GarenE)
              {
                  damage += getEDamage(hero, true);
              }
              var ignitedmg = player.GetSummonerSpellDamage(hero, Damage.SummonerSpell.Ignite);
              if (player.Spellbook.CanUseSpell(player.GetSpellSlot("summonerdot")) == SpellState.Ready &&
                  hero.Health < damage + ignitedmg)
              {
                  damage += ignitedmg;
              }
              return (float) damage;
          }
  
          private double getRDamage(AIHeroClient hero)
          {
  
gt:             var dmg = new double[] { 175, 350, 525 }[R.Level - 1] +