using FluentFTP;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;

namespace TestTool.RemoteTriggerAPP
{
    internal class FluentFTPClient
    {
        #region 属性与构造函数

        /// <summary>
        /// IP地址
        /// </summary>
        public string IpAddr { get; set; }

        /// <summary>
        /// 相对路径
        /// </summary>
        public string RelatePath { get; set; }

        /// <summary>
        /// 端口号
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

       

        public FluentFTPClient(string ipAddr, int port, string userName, string password, string relatePath)
        {
            this.IpAddr = ipAddr;
            this.Port = port;
            this.UserName = userName;
            this.Password = password;
            this.RelatePath = relatePath;
        }

        #endregion

        #region 方法

        public FtpListItem[] ListDir()
        {
            FtpListItem[] lists;
            using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
            {
                client.Config.EncryptionMode = FtpEncryptionMode.Explicit;
                client.Config.SslProtocols = SslProtocols.Tls12;
                client.ValidateCertificate += Client_ValidateCertificate;
                client.Connect();

                client.SetWorkingDirectory(this.RelatePath);
                lists = client.GetListing();
            }
            return lists;
        }

        private void Client_ValidateCertificate(FluentFTP.Client.BaseClient.BaseFtpClient control, FtpSslValidationEventArgs e)
        {
            if (e.PolicyErrors != System.Net.Security.SslPolicyErrors.None)
            {
                e.Accept = false;
            }
            else
            {
                e.Accept = true;
            }
        }

        async internal Task<bool> UploadFileAsync(string localFilePath, string remoteFilePath)
        {
            bool isOk = false;
            using (var client = new AsyncFtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
            {
                client.Config.EncryptionMode = FtpEncryptionMode.Implicit;
                client.Config.SslProtocols = SslProtocols.Tls12;
                client.ValidateCertificate += Client_ValidateCertificate;
                await client.SetWorkingDirectory(this.RelatePath);
                await client.Connect();


                var status = await client.UploadFile(localFilePath, "/" + remoteFilePath, FtpRemoteExists.Overwrite, false, FtpVerify.Retry);
                isOk = status == FtpStatus.Success;

            }

            return isOk;


        }



        public bool Upload(string dir, string file)
        {
            bool isOk = false;
            FileInfo fi = new FileInfo(Path.Combine(dir, file));
            using (FileStream fs = fi.OpenRead())
            {
                //long length = fs.Length;
                using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
                {
                    client.Config.EncryptionMode = FtpEncryptionMode.Implicit;
                    client.Config.SslProtocols = SslProtocols.Tls12;
                    client.ValidateCertificate += Client_ValidateCertificate;
                    client.Connect();
                    client.SetWorkingDirectory(this.RelatePath);
                    string remotePath = Path.GetFileName(file);
                    FtpStatus status = client.UploadStream(fs, remotePath, FtpRemoteExists.Overwrite, true);
                    isOk = status == FtpStatus.Success;

                }
            }

            return isOk;
        }




        /// <summary>
        /// 下载ftp
        /// </summary>
        /// <param name="localAddress"></param>
        /// <param name="remoteAddress"></param>
        /// <returns></returns>
        public bool DownloadFile(string localAddress, string remoteAddress)
        {
            using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
            {
                client.Config.EncryptionMode = FtpEncryptionMode.Explicit;
                client.Config.SslProtocols = SslProtocols.Tls12;
                client.ValidateCertificate += Client_ValidateCertificate;

                client.SetWorkingDirectory("/");
                client.Connect();
                if (client.DownloadFile(localAddress, remoteAddress) == FtpStatus.Success)
                {
                    return true;
                }
                return false;
            }
        }

        #endregion
    }
}