// Decompiled with JetBrains decompiler
// Type: FiestaLauncher.Services.HttpService
// Assembly: FiestaOnline, Version=2019.5.22.0, Culture=neutral, PublicKeyToken=null
// MVID: 2FD5EB61-BC25-469D-997E-88998E849F6A
// Assembly location: C:\Users\marbo\Documents\Gamigo 2020 Client\FiestaOnline.exe

using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Text;
using System.Windows.Forms;

namespace FiestaLauncher.Services
{
  internal static class HttpService
  {
    private static readonly Random Random = new Random();
    private static int _tryCountPatchHive;
    private static int _tryCountSettings;

    internal static void GetPatchHive()
    {
      object[] objArray = new object[5]
      {
        (object) gFiestaLauncher.Settings["PatchHive"],
        (object) "?.rand=",
        (object) HttpService.Random.Next(),
        (object) "&.srl=",
        (object) ResourceService.RandomString(50)
      };
      WebClient webClient = new WebClient()
      {
        Proxy = (IWebProxy) null
      };
      webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(HttpService.LoadPatchHive_DownloadStringCompleted);
      webClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
      webClient.DownloadStringAsync(new Uri(string.Concat(objArray)));
    }

    internal static void GetSettings()
    {
      string path = "XMLOverride.xml";
      if (gFiestaLauncher.XMLURL == path)
      {
        gFiestaLauncher.Settings.Set(System.IO.File.ReadAllText(path));
        HttpService.GetPatchHive();
      }
      else
      {
        object[] objArray = new object[5]
        {
          (object) gFiestaLauncher.XMLURL,
          (object) "?..rand=",
          (object) HttpService.Random.Next(),
          (object) "&.srl=",
          (object) ResourceService.RandomString(50)
        };
        WebClient webClient = new WebClient()
        {
          Proxy = (IWebProxy) null,
          Encoding = Encoding.UTF8
        };
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(HttpService.LoadSettings_DownloadStringCompleted);
        webClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
        webClient.DownloadStringAsync(new Uri(string.Concat(objArray)));
      }
    }

    internal static void GetSettingsBackup()
    {
      object[] objArray = new object[5]
      {
        (object) gFiestaLauncher.XMLURLBACKUP,
        (object) "?..rand=",
        (object) HttpService.Random.Next(),
        (object) "&.srl=",
        (object) ResourceService.RandomString(50)
      };
      WebClient webClient = new WebClient()
      {
        Proxy = (IWebProxy) null,
        Encoding = Encoding.UTF8
      };
      webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(HttpService.LoadSettingsBackup_DownloadStringCompleted);
      webClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
      webClient.DownloadStringAsync(new Uri(string.Concat(objArray)));
    }

    internal static void ReloadSettings()
    {
      object[] objArray = new object[5]
      {
        (object) gFiestaLauncher.XMLURL,
        (object) "?.rand=",
        (object) HttpService.Random.Next(),
        (object) "&.srl=",
        (object) ResourceService.RandomString(50)
      };
      WebClient webClient = new WebClient()
      {
        Proxy = (IWebProxy) null,
        Encoding = Encoding.UTF8
      };
      webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(HttpService.ReloadSettings_DownloadStringCompleted);
      webClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
      webClient.DownloadStringAsync(new Uri(string.Concat(objArray)));
    }

    internal static void StartDownloadPatch(string filename)
    {
      WebClient webClient = new WebClient()
      {
        Proxy = (IWebProxy) null
      };
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(HttpService.Patch_DownloadFileCompleted);
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(HttpService.Patch_DownloadProgressChanged);
      webClient.DownloadFileAsync(new Uri(gFiestaLauncher.Settings["PatchRoot"] + filename), Path.GetTempPath() + "\\" + filename);
    }

    private static void Patch_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
      gFiestaLauncher.MainWindow.RunOnUiThread((Main.RunOnUiThreadDelegate) (() => gFiestaLauncher.MainWindow.DownloadComplete()));
    }

    private static void Patch_DownloadProgressChanged(
      object sender,
      DownloadProgressChangedEventArgs e)
    {
      gFiestaLauncher.MainWindow.RunOnUiThread((Main.RunOnUiThreadDelegate) (() => gFiestaLauncher.MainWindow.DownloadProgressChanged(e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage)));
    }

    private static void LoadPatchHive_DownloadStringCompleted(
      object sender,
      DownloadStringCompletedEventArgs e)
    {
      if (e.Error != null && HttpService._tryCountPatchHive <= 10)
      {
        ++HttpService._tryCountPatchHive;
        HttpService.GetPatchHive();
      }
      else if (e.Error != null && HttpService._tryCountPatchHive > 10)
      {
        HttpService._tryCountPatchHive = 0;
        FeedbackService.PopError(e.Error.Message, true);
      }
      PatchHiveService.ParseHiveFile(e.Result);
      if (ClientUpdateService.IsUpdatingLauncher)
        return;
      gFiestaLauncher.MainWindow.RunOnUiThread((Main.RunOnUiThreadDelegate) (() => gFiestaLauncher.MainWindow.CheckForUpdates()));
      gFiestaLauncher.SplashWindow.SettingsLoadComplete();
    }

    private static void ReloadSettings_DownloadStringCompleted(
      object sender,
      DownloadStringCompletedEventArgs e)
    {
      if (e.Error == null)
        gFiestaLauncher.Settings.Reset(e.Result);
      else
        FeedbackService.PopError(e.Error.Message, true);
    }

    private static void LoadSettings_DownloadStringCompleted(
      object sender,
      DownloadStringCompletedEventArgs e)
    {
      if (e.Error != null && HttpService._tryCountSettings <= 10)
      {
        ++HttpService._tryCountSettings;
        HttpService.GetSettings();
      }
      else if (e.Error != null && HttpService._tryCountSettings > 10)
      {
        HttpService._tryCountSettings = 0;
        HttpService.GetSettingsBackup();
        return;
      }
      gFiestaLauncher.Settings.Set(e.Result);
      HttpService.GetPatchHive();
    }

    private static void LoadSettingsBackup_DownloadStringCompleted(
      object sender,
      DownloadStringCompletedEventArgs e)
    {
      if (e.Error == null)
      {
        gFiestaLauncher.Settings.Set(e.Result);
        gFiestaLauncher.SplashWindow.SettingsLoadComplete();
      }
      else if (MessageBox.Show(gFiestaLauncher.CDNError, "Fiesta Launcher Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        gFiestaLauncher.SplashWindow.ForceRunClient();
      else
        Environment.Exit(911);
    }
  }
}
