namespace OfficeControlsTestApp.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.IO;

    public partial class TreeViewFolderBrowser : UserControl
    {
        private static object DUMMYNODE = null;

        public TreeViewFolderBrowser()
        {
            InitializeComponent();

            this.Loaded += TreeViewFolderBrowser_Loaded;
        }

        /// <summary>
        /// При загрузке контрола
        /// </summary>
        void TreeViewFolderBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (string drives in Directory.GetLogicalDrives())
            {
                TreeViewItem item = new TreeViewItem();
                item.Header = drives;
                item.Tag = drives;
                item.FontWeight = FontWeights.Normal;
                item.Items.Add(TreeViewFolderBrowser.DUMMYNODE);
                item.Expanded += browser_FolderExpanded;
                browser.Items.Add(item);
            }
        }

        /// <summary>
        /// При раскрытии папки
        /// </summary>
        void browser_FolderExpanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;

            if (item.Items.Count == 1 && item.Items[0] == TreeViewFolderBrowser.DUMMYNODE)
            {
                item.Items.Clear();

                try
                {
                    foreach (string folder in Directory.GetDirectories(item.Tag as string))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = folder.Substring(folder.LastIndexOf("\\") + 1);
                        subitem.Tag = folder;
                        subitem.FontWeight = FontWeights.Normal;
                        subitem.Items.Add(TreeViewFolderBrowser.DUMMYNODE);
                        subitem.Expanded += browser_FolderExpanded;

                        item.Items.Add(subitem);
                    }

                    foreach (string file in Directory.GetFiles(item.Tag as string))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = file.Substring(file.LastIndexOf("\\") + 1);
                        subitem.Tag = file;
                        subitem.FontWeight = FontWeights.Normal;
                        subitem.Selected += new RoutedEventHandler(browserFile_Selected);

                        item.Items.Add(subitem);
                    }
                }
                catch (Exception) { }
            }
        }

        /// <summary>
        /// При выборе файла
        /// </summary>
        void browserFile_Selected(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;

            if (item == null)
                return;

            this.File = (item.Tag as string);
        }

        /// <summary>
        /// Путь к файлу
        /// </summary>
        public string File
        {
            get { return (string)this.GetValue(FileProperty); }
            set { this.SetValue(FileProperty, value); }
        }

        public static readonly DependencyProperty FileProperty = DependencyProperty.Register("File", typeof(string), typeof(TreeViewFolderBrowser), new PropertyMetadata(string.Empty));
    }
}
