using JWH;
using JWH.NETWORK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DDUtilityApp.SPECDOCUMENT
{

    [Serializable]
    public class SpecDocument
    {

        #region [ Variables ] =================================================
        #endregion

        #region [ Properties ] ================================================

        public string URI { get; set; } = string.Empty;

        public string UID { get; set; } = string.Empty;

        public string PWD { get; set; } = string.Empty;

        public List<SpecInfo> SpecInfos { get; set; } = new List<SpecInfo>();

        public List<FtpObject> Childs { get; set; } = new List<FtpObject>();

        #endregion

        #region [ SpecDocument ] ==============================================

        public SpecDocument()
        {
        }

        public SpecDocument(string uri, string uid, string pwd)
        {
            this.URI = uri;
            this.UID = uid;
            this.PWD = pwd;
        }

        #endregion

        #region [ Public Methods ] ============================================

        public bool ReBuild(int depth = 0)
        {
            FtpObject[] ftpObjects = this.GetChilds(depth);

            this.Childs.Clear();
            this.Childs.AddRange(ftpObjects);

            return true;
        }

        public TreeNode GetTreeNode()
        {
            TreeNode node = new TreeNode(this.URI, 1, 0);
            foreach(FtpObject child in this.Childs)
            {
                node.Nodes.Add(child.GetTreeNode());
            }

            return node;
        }

        public void Refresh(FtpObject ftpObject, int depth = 0)
        {
        }

        public void Refresh(TreeNode node, int depth = 0)
        {
            FtpObject ftpObject = node.Tag as FtpObject;
            if (ftpObject == null) return;
        }

        #endregion

        #region [ Methods ] ===================================================

        public FtpClient GetFtpClient()
        {
            try
            {
                FtpClient ftpClient = new FtpClient(this.URI, this.UID, this.PWD);
                return ftpClient;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private FtpObject[] GetChilds(int depth = 0)
        {
            try
            {
                List<FtpObject> lstChild = new List<FtpObject>();

                FtpClient ftpClient = this.GetFtpClient();
                foreach(FtpObject child in ftpClient.GetFtpObjects(""))
                {
                    lstChild.Add(child);
                    if (child.GetType() == typeof(FtpDirectory) && depth > 0)
                    {
                        FtpDirectory ftpDirectory = child as FtpDirectory;
                        ftpDirectory.GetChilds(ftpClient, depth - 1);
                    }
                }

                return lstChild.ToArray();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

    }

}
