context: #region License
  
  /*
   Copyright 2014 - 2015 Nikita Bernthaler
   Replay.cs is part of SFXReplay.
  
   SFXReplay is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
  
   SFXReplay is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with SFXReplay. If not, see <http://www.gnu.org/licenses/>.
  */
  
  #endregion License
  
  #region
  
  using System;
  using System.Linq;
  using System.Net;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Timers;
  using System.Web;
  using LeagueSharp;
  using LeagueSharp.Common;
  using SFXReplay.Classes;
  using SFXReplay.Library.Extensions.NET;
  using SFXReplay.Library.Extensions.SharpDX;
  using SFXReplay.Library.Logger;
  using SFXReplay.Properties;
  using SharpDX.Direct3D9;
  using Timer = System.Timers.Timer;
  
  #endregion
  
  namespace SFXReplay.Features.Detectors
  {
      internal class Replay : Child<App>
      {
          private readonly Timer _timer = new Timer { Enabled = false };
          private bool _isRecording;
          private Texture _recordTexture;
          private Sprite _sprite;
  
          public Replay(App parent) : base(parent)
          {
              OnLoad();
          }
  
          public override string Name
          {
              get { return "Replay op.gg"; }
          }
  
          protected override void OnEnable()
          {
              Drawing.OnEndScene += OnDrawingEndScene;
  
              if (!_isRecording)
              {
                  OnTimerElapsed(null, null);
  
                  if (_timer != null)
                  {
                      _timer.Enabled = true;
                      _timer.Start();
                  }
              }
  
              base.OnEnable();
          }
  
          protected override void OnDisable()
          {
              Drawing.OnEndScene -= OnDrawingEndScene;
  
              if (_timer != null && _timer.Enabled)
              {
                  _timer.Enabled = false;
                  _timer.Stop();
              }
  
              base.OnDisable();
          }
  
          private void OnDrawingEndScene(EventArgs args)
          {
              try
              {
                  if (Drawing.Direct3DDevice == null || Drawing.Direct3DDevice.IsDisposed || !_isRecording ||
                      !Menu.Item(Name + "IsRecording").GetValue<bool>())
                  {
                      return;
                  }
  
                  _sprite.Begin(SpriteFlags.AlphaBlend);
  
                  _sprite.DrawCentered(_recordTexture, 20, 20);
  
                  _sprite.End();
              }
              catch (Exception ex)
              {
                  Global.Logger.AddItem(new LogItem(ex));
              }
          }
  
          protected sealed override void OnLoad()
          {
              try
              {
                  Menu = new Menu(Name, Name);
                  Menu.AddItem(new MenuItem(Name + "DoRecord", "Do Record").SetValue(false));
                  Menu.AddItem(new MenuItem(Name + "IsRecording", "Notify Record").SetValue(false));
                  Menu.AddItem(new MenuItem(Name + "CheckInterval", "Check Interval").SetValue(new Slider(3, 1, 10)))
                      .ValueChanged += delegate(object o, OnValueChangeEventArgs args)
                      {
                          if (_timer != null)
                          {
                              _timer.Interval = args.GetNewValue<Slider>().Value * 60 * 1000;
                          }
                      };
  
                  Menu.AddItem(new MenuItem(Name + "Enabled", "Enabled").SetValue(true));
  
                  Parent.Menu.AddSubMenu(Menu);
  
                  _sprite = MDrawing.GetSprite();
              }
              catch (Exception ex)
              {
                  Global.Logger.AddItem(new LogItem(ex));
              }
          }
  
          protected override void OnInitialize()
          {
              try
              {
                  _recordTexture = Resources.RC_On.ToTexture();
  
                  _timer.Interval = Menu.Item(Name + "CheckInterval").GetValue<Slider>().Value * 60 * 1000;
                  _timer.Elapsed += OnTimerElapsed;
  
                  base.OnInitialize();
              }
              catch (Exception ex)
              {
                  Global.Logger.AddItem(new LogItem(ex));
              }
          }
  
          private void OnTimerElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
          {
              try
              {
                  new Thread(
                      async () =>
                      {
                          try
                          {
                              if (!_isRecording && Menu.Item(Name + "DoRecord").GetValue<bool>())
                              {
                                  _isRecording = await Spectator.DoRecord();
                              }
                              if (!_isRecording && Menu.Item(Name + "IsRecording").GetValue<bool>())
                              {
                                  _isRecording = await Spectator.IsRecoding();
                              }
                              if (_isRecording)
                              {
                                  _timer.Enabled = false;
                                  _timer.Stop();
                              }
                          }
                          catch (Exception ex)
                          {
                              Global.Logger.AddItem(new LogItem(ex));
                          }
                      }).Start();
              }
              catch (Exception ex)
              {
                  Global.Logger.AddItem(new LogItem(ex));
              }
          }
  
          internal class Spectator
          {
              private static readonly string DoRecordUrl =
                  "http://{region}op.gg/summoner/ajax/requestRecording.json/gameId={game_id}";
  
              private static readonly string IsRecordingUrl = "http://{region}op.gg/summoner/ajax/spectator/";
              private static readonly string UpdateUrl = "http://{region}op.gg/summoner/ajax/update.json/";
              private static readonly string MainUrl = "http://{region}op.gg/summoner/userName={name}";
              private static bool _updated;
  
              static Spectator()
              {
                  Region = PlatformId.Last().IsNumeric()
                      ? PlatformId.Remove(PlatformId.Length - 1).ToLower()
                      : PlatformId.ToLower();
                  Region = Region.Contains("kr", StringComparison.OrdinalIgnoreCase) ? string.Empty : Region + ".";
                  DoRecordUrl = DoRecordUrl.Replace("{region}", Region).Replace("{game_id}", GameId.ToString());
                  IsRecordingUrl = IsRecordingUrl.Replace("{region}", Region);
                  UpdateUrl = UpdateUrl.Replace("{region}", Region);
                  MainUrl = MainUrl.Replace("{region}", Region)
                      .Replace("{name}", HttpUtility.UrlEncode(ObjectManager.Player.Name));
              }
  
              public static string PlatformId
              {
                  get { return Game.Region; }
              }
  
              private static string Region { get; set; }
  
              public static long GameId
              {
                  get { return Game.Id; }
              }
  
              public static int SummonerId { get; private set; }
  
              public static void UpdateSummonerId()
              {
                  using (var client = new WebClient())
                  {
                      try
                      {
  
gt:                         var response = client.DownloadString(new Uri(MainUrl));