using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

namespace Core.Mes.ServerFrameWork
{
    public delegate void Action<T1, T2>(T1 _t1, T2 _t2);
    public static class FtpHelper
    {
        public static string FtpUserID = string.Empty;

        public static string FtpPassword = string.Empty;

        public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress)
        {
            Stream stream = null;
            FtpWebResponse ftpWebResponse = null;
            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(localFileName, FileMode.Create);
                Uri requestUri = new Uri(remoteFileName);
                FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
                ftpWebRequest.UseBinary = true;
                FtpWebRequest ftpWebRequest2 = (FtpWebRequest)WebRequest.Create(requestUri);
                ftpWebRequest2.UseBinary = true;
                ftpWebRequest2.KeepAlive = false;
                if (ifCredential)
                {
                    ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
                    ftpWebRequest2.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
                }
                ftpWebRequest.Method = "SIZE";
                FtpWebResponse ftpWebResponse2 = (FtpWebResponse)ftpWebRequest.GetResponse();
                long contentLength = ftpWebResponse2.ContentLength;
                ftpWebResponse2.Close();
                ftpWebRequest2.Method = "RETR";
                ftpWebResponse = (FtpWebResponse)ftpWebRequest2.GetResponse();
                stream = ftpWebResponse.GetResponseStream();
                if (updateProgress != null)
                    updateProgress(0, (int)contentLength);
                long num = 0L;
                int num2 = 2048;
                byte[] buffer = new byte[num2];
                for (int num3 = stream.Read(buffer, 0, num2); num3 > 0; num3 = stream.Read(buffer, 0, num2))
                {
                    num = num3 + num;
                    fileStream.Write(buffer, 0, num3);
                    if (updateProgress != null)
                        updateProgress((int)num, (int)contentLength);
                }
                stream.Close();
                fileStream.Close();
                ftpWebResponse.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                stream.Close();
                fileStream.Close();
                ftpWebResponse.Close();
            }
        }

        public static bool FtpBrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress)
        {
            Stream stream = null;
            FtpWebResponse ftpWebResponse = null;
            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(localFileName, FileMode.Append);
                Uri requestUri = new Uri(remoteFileName);
                FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
                ftpWebRequest.UseBinary = true;
                ftpWebRequest.ContentOffset = size;
                FtpWebRequest ftpWebRequest2 = (FtpWebRequest)WebRequest.Create(requestUri);
                ftpWebRequest2.UseBinary = true;
                ftpWebRequest2.KeepAlive = false;
                ftpWebRequest2.ContentOffset = size;
                if (ifCredential)
                {
                    ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
                    ftpWebRequest2.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
                }
                ftpWebRequest.Method = "SIZE";
                FtpWebResponse ftpWebResponse2 = (FtpWebResponse)ftpWebRequest.GetResponse();
                long contentLength = ftpWebResponse2.ContentLength;
                ftpWebResponse2.Close();
                ftpWebRequest2.Method = "RETR";
                ftpWebResponse = (FtpWebResponse)ftpWebRequest2.GetResponse();
                stream = ftpWebResponse.GetResponseStream();
                if (updateProgress != null)
                    updateProgress(0, (int)contentLength);
                long num = 0L;
                int num2 = 2048;
                byte[] buffer = new byte[num2];
                for (int num3 = stream.Read(buffer, 0, num2); num3 > 0; num3 = stream.Read(buffer, 0, num2))
                {
                    num = num3 + num;
                    fileStream.Write(buffer, 0, num3);
                    if (updateProgress != null)
                        updateProgress((int)num, (int)contentLength);
                }
                stream.Close();
                fileStream.Close();
                ftpWebResponse.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                stream.Close();
                fileStream.Close();
                ftpWebResponse.Close();
            }
        }

        public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress)
        {
            if (brokenOpen)
            {
                try
                {
                    long size = 0L;
                    if (File.Exists(localFileName))
                    {
                        using (FileStream fileStream = new FileStream(localFileName, FileMode.Open))
                        {
                            size = fileStream.Length;
                        }
                    }
                    return FtpBrokenDownload(remoteFileName, localFileName, ifCredential, size, updateProgress);
                }
                catch
                {
                    throw;
                }
            }
            return FtpDownload(remoteFileName, localFileName, ifCredential, updateProgress);
        }

        public static bool FtpUploadFile(string localFullPathName, string remotePath, Action<int, int> updateProgress)
        {
            Stream stream = null;
            FtpWebResponse ftpWebResponse = null;
            FileStream fileStream = null;
            try
            {
                FileInfo fileInfo = new FileInfo(localFullPathName);
                Uri requestUri = new Uri(remotePath);
                FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
                ftpWebRequest.KeepAlive = false;
                ftpWebRequest.UseBinary = true;
                ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
                ftpWebRequest.Method = "STOR";
                ftpWebRequest.ContentLength = fileInfo.Length;
                ftpWebResponse = (ftpWebRequest.GetResponse() as FtpWebResponse);
                ftpWebRequest.ContentLength = fileInfo.Length;
                int num = 1024;
                byte[] buffer = new byte[num];
                fileStream = fileInfo.OpenRead();
                stream = ftpWebRequest.GetRequestStream();
                int num2 = fileStream.Read(buffer, 0, num);
                int num3 = (int)fileInfo.Length;
                if (updateProgress != null)
                    updateProgress(0, num3);

                int num4 = 0;
                while (num2 != 0)
                {
                    num4 = num2 + num4;
                    stream.Write(buffer, 0, num2);
                    if (updateProgress != null)
                        updateProgress(num4, num3);
                    num2 = fileStream.Read(buffer, 0, num);
                }
                stream.Close();
                fileStream.Close();
                ftpWebResponse.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                fileStream.Close();
                stream.Close();
                ftpWebResponse.Close();
            }
        }

        public static bool FtpUploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress)
        {
            if (remoteFilepath == null)
            {
                remoteFilepath = "";
            }
            string empty = string.Empty;
            bool result = true;
            FileInfo fileInfo = new FileInfo(localFullPath);
            long length = fileInfo.Length;
            if (fileInfo.Name.IndexOf("#") == -1)
            {
                empty = RemoveSpaces(fileInfo.Name);
            }
            else
            {
                empty = fileInfo.Name.Replace("#", "＃");
                empty = RemoveSpaces(empty);
            }
            long fileSize = GetFileSize(empty, remoteFilepath);
            if (fileSize >= length)
            {
                return false;
            }
            long num = fileSize;
            if (updateProgress != null)
                updateProgress((int)fileSize, (int)length);
            string uriString = (remoteFilepath.Length != 0) ? (remoteFilepath + "/" + remoteFilepath + "/" + empty) : (remoteFilepath + "/" + empty);
            FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(new Uri(uriString));
            ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
            ftpWebRequest.KeepAlive = false;
            ftpWebRequest.Method = "APPE";
            ftpWebRequest.UseBinary = true;
            ftpWebRequest.ContentLength = fileInfo.Length;
            int num2 = 2048;
            byte[] buffer = new byte[num2];
            FileStream fileStream = fileInfo.OpenRead();
            Stream stream = null;
            try
            {
                stream = ftpWebRequest.GetRequestStream();
                fileStream.Seek(fileSize, SeekOrigin.Begin);
                int num3 = fileStream.Read(buffer, 0, num2);
                while (num3 != 0)
                {
                    stream.Write(buffer, 0, num3);
                    num3 = fileStream.Read(buffer, 0, num2);
                    num += num3;
                    if (updateProgress != null)
                        updateProgress((int)num, (int)length);
                }
                stream.Close();
                fileStream.Close();
            }
            catch
            {
                result = false;
                throw;
            }
            finally
            {
                fileStream.Close();
                stream.Close();
            }
            return result;
        }

        private static string RemoveSpaces(string str)
        {
            string text = "";
            CharEnumerator enumerator = str.GetEnumerator();
            while (enumerator.MoveNext())
            {
                byte[] array = new byte[1];
                array = Encoding.ASCII.GetBytes(enumerator.Current.ToString());
                int num = array[0];
                if (num != 32)
                {
                    text += enumerator.Current.ToString();
                }
            }
            string text2 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            return text.Split('.')[text.Split('.').Length - 2] + "." + text.Split('.')[text.Split('.').Length - 1];
        }

        public static long GetFileSize(string filename, string remoteFilepath)
        {
            try
            {
                if (remoteFilepath.Length == 0)
                {
                    return 0L;
                }
                FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(remoteFilepath);
                ftpWebRequest.KeepAlive = false;
                ftpWebRequest.UseBinary = true;
                ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
                ftpWebRequest.Method = "SIZE";
                FtpWebResponse ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
                return ftpWebResponse.ContentLength;
            }
            catch
            {
                return 0L;
            }
        }
    }


}
