using System;
using System.IO;
using System.Net;

namespace FantasySolution.FantasyCorrector
{

  public class DownloadTools
  {
    //11111333
    public string DownloadFile(string SavePath, string DownloadUrl, string FileName)
    {
      if (!Directory.Exists(SavePath))
      {
        Directory.CreateDirectory(SavePath);
      }
      string path = SavePath + "\\" + FileName;
      string requestUriString = DownloadUrl + "/" + FileName;
      try
      {
        WebResponse response = WebRequest.Create(requestUriString).GetResponse();
        if (response.ContentLength > 0)
        {
          try
          {
            long contentLength = response.ContentLength;
            try
            {
              Stream responseStream = response.GetResponseStream();
              StreamReader reader = new StreamReader(responseStream);
              byte[] buffer = new byte[contentLength];
              int count = buffer.Length;
              int offset = 0;
              while (contentLength > 0)
              {
                int num4 = responseStream.Read(buffer, offset, count);
                if (num4 == 0)
                {
                  break;
                }
                offset += num4;
                count -= num4;
                float num5 = ((float)offset) / 1024f;
                float num6 = ((float)buffer.Length) / 1024f;
                Convert.ToInt32((float)((num5 / num6) * 100f));
              }
              FileStream stream2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
              stream2.Write(buffer, 0, buffer.Length);
              responseStream.Close();
              reader.Close();
              stream2.Close();
              return FileName;
            }
            catch
            {
              return null;
            }
          }
          catch
          {
            return null;
          }
        }
      }
      catch
      {
        return null;
      }
      return null;
    }

    public bool IsDownloadFile(string SavePath, string DownloadUrl, string FileName)
    {
      if (!Directory.Exists(SavePath))
      {
        Directory.CreateDirectory(SavePath);
      }
      string path = SavePath + "\\" + FileName;
      string requestUriString = DownloadUrl + "/" + FileName;
      try
      {
        WebResponse response = WebRequest.Create(requestUriString).GetResponse();
        if (response.ContentLength > 0)
        {
          try
          {
            long contentLength = response.ContentLength;
            try
            {
              Stream responseStream = response.GetResponseStream();
              StreamReader reader = new StreamReader(responseStream);
              byte[] buffer = new byte[contentLength];
              int count = buffer.Length;
              int offset = 0;
              while (contentLength > 0)
              {
                int num4 = responseStream.Read(buffer, offset, count);
                if (num4 == 0)
                {
                  break;
                }
                offset += num4;
                count -= num4;
                float num5 = ((float)offset) / 1024f;
                float num6 = ((float)buffer.Length) / 1024f;
                Convert.ToInt32((float)((num5 / num6) * 100f));
              }
              FileStream stream2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
              stream2.Write(buffer, 0, buffer.Length);
              responseStream.Close();
              reader.Close();
              stream2.Close();
              return true;
            }
            catch
            {
              return false;
            }
          }
          catch
          {
            return false;
          }
        }
      }
      catch
      {
        return false;
      }
      return false;
    }
  }
}
