using ryCommon;
using ryCommonDb;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ExtendUI.FTPManager
{
    /// <summary>
    /// FTP数据管理类(不含上传下载功能)
    /// </summary>
    public class FTPUI
    {
        private IDbInterface Db { get; set; } =null;
        private string SQLConn { get; set; } = "";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Db"></param>
        /// <param name="SQLConn"></param>
        public FTPUI(IDbInterface Db, string SQLConn) {
            this.Db = Db;
            this.SQLConn = SQLConn;
            if (Db is SQLiteDataProvider)
            {
                CreateSQLiteDb();
            }
            if(Db.IsOpened!=1)
            {
                Db.ConnDb(SQLConn);
            }
        }
        /// <summary>
        /// 获取指定id的FTP信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FTPInfo GetFTPInfo(int id)
        {
            DataSet ds = Db.ReadData("FTP", id);
            if (ds.HaveData())
            {
                DataRow row = ds.Tables[0].Rows[0];
                #region 读取信息 
                var FtpInfo = new FTPInfo()
                {
                    Id = row["id"].ToInt(),
                    Name = row["name"].ToString(),
                    IP = row["ip"].ToString(),
                    Port = row["port"].ToInt(),
                    RemoteDir = row["remoteDir"].ToString(),
                    UserName = row["username"].ToString(),
                    Pwd = row["pwd"].ToString(),
                    Encrypt = row["encrypt"].ToInt(0, 2, 0),
                    AddTime = row["AddTime"].ToDateTime(),
                    Desc = row["des"].ToString(),
                };
                ds?.Dispose();
                return FtpInfo;
                #endregion
            }
            return new FTPInfo();
        }
        /// <summary>
        /// 获取指定name的FTP信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public FTPInfo GetFTPInfo(string name)
        {
            RyQuickSQL mySQL = new RyQuickSQL("FTP");
            mySQL.AddField("name",name);
            DataSet ds = Db.ReadData("select * from FTP where name=@name",mySQL);
            if (ds.HaveData())
            {
                DataRow row = ds.Tables[0].Rows[0];
                #region 读取信息 
                var FtpInfo = new FTPInfo()
                {
                    Id = row["id"].ToInt(),
                    Name = row["name"].ToString(),
                    IP = row["ip"].ToString(),
                    Port = row["port"].ToInt(),
                    RemoteDir = row["remoteDir"].ToString(),
                    UserName = row["username"].ToString(),
                    Pwd = row["pwd"].ToString(),
                    Encrypt = row["encrypt"].ToInt(0, 2, 0),
                    AddTime = row["AddTime"].ToDateTime(),
                    Desc = row["des"].ToString(),
                };
                ds?.Dispose();
                return FtpInfo;
                #endregion
            }
            return new FTPInfo();
        }
        /// <summary>
        /// 打开FTP管理界面
        /// </summary>
        public void ShowFTPManager()
        {
            FrmFTPView ftpView = new FrmFTPView
            {
                UseDb = Db
            };
            ftpView.ShowDialog();
            ftpView.Dispose();
        }
        /// <summary>
        /// 创建SQLite数据库表
        /// </summary>
        public void CreateSQLiteDb()
        {
            IDbInterface db = new SQLiteDataProvider();
            if (db.ConnDb(SQLConn) == 1)
            {
                #region FTP表
                RyQuickSQL mySQL = new RyQuickSQL("FTP");
                mySQL.AddField("name", "");
                mySQL.AddField("ip", "");
                mySQL.AddField("port", 0);
                mySQL.AddField("remoteDir", "");
                mySQL.AddField("username", "");
                mySQL.AddField("pwd", "");
                mySQL.AddField("encrypt", 0);
                mySQL.AddField("des", "");
                mySQL.AddField("addTime", DateTime.Now);
                mySQL.AddField("editTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
            }
            db.Free();
        }
    }
    /// <summary>
    /// FTP信息
    /// </summary>
    public class FTPInfo
    {
        /// <summary>
        /// id
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// IP
        /// </summary>
        public string IP { get; set; }
        /// <summary>
        /// 端口
        /// </summary>
        public int Port { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Pwd { get; set; }
        /// <summary>
        /// 远程目录
        /// </summary>
        public string RemoteDir { get; set; }
        /// <summary>
        /// 加密方式
        /// </summary>
        public int Encrypt { get; set; }
        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime AddTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Encoding { get; set; } = "UTF8";
        /// <summary>
        /// 备注
        /// </summary>
        public string Desc { get; set; } = "";
        /// <summary>
        /// 添加时间字符串
        /// </summary>
        public string AddTimeStr
        {
            get { return AddTime.ToString("yyyy-MM-dd dddd HH:mm:ss"); }
        }
        /// <summary>
        /// 设置
        /// </summary>
        public string Setting { get; set; } = "";
        /// <summary>
        /// 部门列表
        /// </summary>
        public string DidList { get; set; } = "";
        /// <summary>
        /// 
        /// </summary>
        public List<RemoteFileInfo> RemoteFiles { get; set; } = new List<RemoteFileInfo>();
    }
    /// <summary>
    /// 远程文件信息
    /// </summary>
    public class RemoteFileInfo
    {
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string FullName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public long Length { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTime LastWriteTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public bool IsDirectory { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return FullName;
        }
    }
}
