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

namespace DDUtilityApp.SPECDOCUMENT
{

    public partial class FrmSpecDocument : Form
    {

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

        private SpecDocument SpecDocument { get; set; } = null;

        #endregion

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

        #region [ FrmSpecDocument ] ===========================================

        public FrmSpecDocument()
        {
            InitializeComponent();

            this.SetLayout();
            this.SetEventHandler();
            this.SpecDocument = new SpecDocument("ftp://127.0.0.1:2100", "", "");
            //this.SpecDocument = new SpecDocument("ftp://192.168.8.217", "", "");
            //192.168.8.217
        }

        private void SetLayout()
        {

        }

        public void SetEventHandler()
        {
            this.Load += this.FrmSpecDocument_Load;
            this.FormClosed += this.FrmSpecDocument_FormClosed;

            this.tree.AfterExpand += this.Tree_AfterExpand;
        }

        private string TreeNodeLastFullPath { get; set; } = string.Empty;

        private void Tree_AfterExpand(object sender, TreeViewEventArgs e)
        {
            FtpDirectory ftpDirectory = e.Node.Tag as FtpDirectory;
            if (ftpDirectory == null) return;
            if (string.Compare(this.TreeNodeLastFullPath, e.Node.FullPath) == 0) return;

            int index = e.Node.Index;
            this.TreeNodeLastFullPath = e.Node.FullPath;
            TreeNode parent = e.Node.Parent;

            FtpClient ftpClient = this.SpecDocument.GetFtpClient();
            ftpDirectory.GetChilds(ftpClient, 1);
            TreeNode newNode = ftpDirectory.GetTreeNode();
            newNode.Expand();

            this.tree.SuspendLayout();
            parent.Nodes.Insert(index, newNode);
            e.Node.Remove();
            this.tree.ResumeLayout();
        }

        private void FrmSpecDocument_Load(object sender, EventArgs e)
        {
            this.SpecDocument.ReBuild(1);
            TreeNode node = this.SpecDocument.GetTreeNode();
            this.tree.Nodes.Add(node);
        }

        private void FrmSpecDocument_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        #endregion

        #region [ Events ] ====================================================
        #endregion

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

    }

}
