using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace ListView
{
    public partial class ListViewForm : Form
    {
        public ListViewForm()
        {
            InitializeComponent();
        }

        int _TotalFolders;
        int _TotalFiles;

        /// <summary>
        /// Load the DriversNodes to TreeView
        /// </summary>
        private void ListFormLoad(object sender, EventArgs e)
        {
            _FolderFileListView.View = View.Details;
            try
            {
                string[] drivers = Environment.GetLogicalDrives();
                foreach (string driver in drivers)
                {
                    TreeNode subNode = new TreeNode(driver.Remove(2, 1));
                    subNode.ImageIndex = 0;
                    subNode.SelectedImageIndex = 0;
                    _DirTreeView.Nodes.Add(subNode);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        /// <summary>
        /// Add subNodes to SelectedNodes in TreeView
        /// </summary>
        private void AddDirs(TreeNode node)
        {
            node.Nodes.Clear();

            DirectoryInfo pDir = new DirectoryInfo(node.FullPath + "//");
            DirectoryInfo[] sdirs = pDir.GetDirectories();

            foreach (DirectoryInfo sdir in sdirs)
            {
                TreeNode subNode = new TreeNode(sdir.Name.ToString());
                subNode.ImageIndex = 1;
                subNode.SelectedImageIndex = 2;
                _DirTreeView.SelectedNode.Nodes.Add(subNode);

            }

            _DirTreeView.SelectedNode.Expand();
            _PathTextBox.Text = _DirTreeView.SelectedNode.FullPath;
        }

        /// <summary>
        /// Add Folders and Files in ListView
        /// </summary>
        private void AddFoldersFiles(TreeNode node)
        {
            _FolderFileListView.Items.Clear();

            DirectoryInfo pDir = new DirectoryInfo(node.FullPath + "//");
            DirectoryInfo[] sdirs = pDir.GetDirectories();
            FileInfo[] files = pDir.GetFiles();

            foreach (DirectoryInfo folder in sdirs)
            {
                _TotalFolders++;

                ListViewItem item = new ListViewItem(folder.Name, 1);

                item.SubItems.Add(" ");
                item.SubItems.Add(folder.LastWriteTime.ToShortDateString());
                item.SubItems.Add(folder.LastWriteTime.ToShortTimeString());
                item.SubItems.Add("Folder");

                _FolderFileListView.Items.Add(item);
            }

            foreach (FileInfo file in files)
            {
                _TotalFiles++;

                ListViewItem item = new ListViewItem(file.Name, 3);
                item.SubItems.Add("KB");
                item.SubItems.Add(file.LastWriteTime.ToShortDateString());
                item.SubItems.Add(file.LastWriteTime.ToShortTimeString());

                string path = node.FullPath + "//" + file.Name;
                string extention = Path.GetExtension(path);
                item.SubItems.Add(extention);


                _FolderFileListView.Items.Add(item);
            }

        }


        TreeNode _IsSameNode;
        private void NodesAfterSelect(object sender, TreeViewEventArgs e)
        {
            _TotalFolders = 0;
            _TotalFiles = 0;

            if (_DirTreeView.SelectedNode == _IsSameNode) return;

            _PathTextBox.Text = _DirTreeView.SelectedNode.FullPath;
            AddDirs(_DirTreeView.SelectedNode);
            AddFoldersFiles(_DirTreeView.SelectedNode);

            _IsSameNode = _DirTreeView.SelectedNode;

            _StatusStrip.Items[0].Text = "Folders:" + _TotalFolders.ToString() + " Files:" + _TotalFiles.ToString();
        }

        private void ListViewItemActivate(object sender, EventArgs e)
        {
            string active = _FolderFileListView.FocusedItem.Text;
            for (int i = 0; i < _DirTreeView.SelectedNode.Nodes.Count; i++)
            {
                if (active == _DirTreeView.SelectedNode.Nodes[i].Text)
                {
                    _DirTreeView.SelectedNode = _DirTreeView.SelectedNode.Nodes[i];
                    return;
                }

            }
            Process.Start(_DirTreeView.SelectedNode.FullPath + "//" + active);
        }



        private void EnterPath(object sender, EventArgs e)
        {

        }

        TreeNode _PreNode;
        string _PrePath;
        private void InputPathKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                bool rightPath = true;
                int counts = 0;
                string _PrePath = _DirTreeView.SelectedNode.FullPath;
                string path = _PathTextBox.Text;

                //First nodeselect
                if (path.Length > 2)
                {
                    string drivername = path.Remove(2);
                    for (int i = 0; i < _DirTreeView.Nodes.Count; i++)
                    {
                        if (drivername == _DirTreeView.Nodes[i].Text)
                        {
                            _DirTreeView.SelectedNode = _DirTreeView.Nodes[i];
                        }
                    }
                    path = path.Remove(0, 3);
                }
                else
                {
                    rightPath = false;
                }

                //Judge right or wrong path
                char[] charpath = path.ToCharArray();
                foreach (char rods in charpath)
                {
                    counts++;
                    if (rods == '\\')
                    {
                        try
                        {
                            if (charpath[counts] == '\\')
                            {
                                rightPath = false;
                            }
                        }
                        catch
                        {
                            rightPath = false;
                        }
                    }
                }

                if (rightPath)
                {
                    //Get Foldername
                    string newpath = path;
                    counts = 0;

                    foreach (char rods in path)
                    {
                        counts++;

                        if (rods == '\\')
                        {
                            string foldername = newpath.Remove(counts - 1);
                            for (int i = 0; i < _DirTreeView.SelectedNode.Nodes.Count; i++)
                            {
                                if (foldername == _DirTreeView.SelectedNode.Nodes[i].Text)
                                {
                                    _DirTreeView.SelectedNode = _DirTreeView.SelectedNode.Nodes[i];
                                }
                            }
                            newpath = newpath.Remove(0, counts);
                            
                        }
                    }

                    //Get the lasr Folder
                    for (int i = 0; i < _DirTreeView.SelectedNode.Nodes.Count; i++)
                    {
                        if (newpath == _DirTreeView.SelectedNode.Nodes[i].Text)
                        {
                            _DirTreeView.SelectedNode = _DirTreeView.SelectedNode.Nodes[i];
                         //   _PreNode = _DirTreeView.SelectedNode;
                            return;
                        }
                    }

                    BacktoPreNode();
                }


                //Deal with wrongPath
                else
                {
                    BacktoPreNode();
                }
                //回复问题,初始磁盘问题
            }
        }

        //back to the preNode
        private void BacktoPreNode()
        {
         
            int counts1 = 0;
            if (_PrePath.Length > 2)
            {
                string drivername = _PrePath.Remove(2);
               
                for (int i = 0; i < _DirTreeView.Nodes.Count; i++)
                {
                    if (drivername == _DirTreeView.Nodes[i].Text)
                    {
                        _DirTreeView.SelectedNode = _DirTreeView.Nodes[i];
                    }
                }
                _PrePath = _PrePath.Remove(0, 3);
            }
            else
            {
                for (int i = 0; i < _DirTreeView.Nodes.Count; i++)
                {
                    if (_PrePath == _DirTreeView.Nodes[i].Text)
                    {
                        _DirTreeView.SelectedNode = _DirTreeView.Nodes[i];
                        _PathTextBox.Text = _DirTreeView.SelectedNode.FullPath;
                        return;
                    }
                }
            }

            string preNewPath = _PrePath;
            counts1 = 0;

            foreach (char rods in _PrePath)
            {
                counts1++;

                if (rods == '\\')
                {
                    string foldername = preNewPath.Remove(counts1 - 1);
                    for (int i = 0; i < _DirTreeView.SelectedNode.Nodes.Count; i++)
                    {
                        if (foldername == _DirTreeView.SelectedNode.Nodes[i].Text)
                        {
                            _DirTreeView.SelectedNode = _DirTreeView.SelectedNode.Nodes[i];
                        }
                    }
                    preNewPath = preNewPath.Remove(0, counts1);
                }
            }

            //Get the lasr Folder
            for (int i = 0; i < _DirTreeView.SelectedNode.Nodes.Count; i++)
            {
                if (preNewPath == _DirTreeView.SelectedNode.Nodes[i].Text)
                {
                    _DirTreeView.SelectedNode = _DirTreeView.SelectedNode.Nodes[i];
                    _PathTextBox.Text = _DirTreeView.SelectedNode.FullPath;
                    //   _PreNode = _DirTreeView.SelectedNode;
                }
            }
            MessageBox.Show("Please Input the right Path~", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
}