using ShellBoost.Core;
using ShellBoost.Core.WindowsShell;
using System;
using System.Collections.Generic;
using DigitalData.Modules.Logging;
using DigitalData.Modules.Database;
using System.Data;

namespace VirtualDrive
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            LogConfig LogConfig = new LogConfig(LogConfig.PathType.Temp);
            Logger Logger = LogConfig.GetLogger();
            MSSQLServer Db = new MSSQLServer(LogConfig, "Server=172.24.12.41\\tests;Database=DD_ECM_TEST;User Id=sa;Password=dd;");

            ShellFolderServer.RegisterNativeDll(RegistrationMode.User);

            using (var server = new MyShellFolderServer(Db))
            {
                var config = new ShellFolderConfiguration();   // this class is located in ShellBoost.Core
                server.Start(config); // start the server    

                while (Console.ReadKey(true).Key != ConsoleKey.Escape)
                {
                }
            }

            ShellFolderServer.UnregisterNativeDll(RegistrationMode.User);
        }

        public class MyShellFolderServer : ShellFolderServer
        {
            private MyRootFolder _root;
            private MSSQLServer _Db;

            public MyShellFolderServer(MSSQLServer Db)
            {
                _Db = Db;
            }

            // only the Shell knows our root folder PIDL
            protected override ShellFolder GetFolderAsRoot(ShellItemIdList idl)
            {
                if (_root == null)
                {
                    _root = new MyRootFolder(idl, _Db);
                }
                return _root;
            }
        }

        public class MyRootFolder : ShellFolder  // this base class is located in ShellBoost.Core
        {
            MSSQLServer _Db;

            // reference to the ShellFolderServer is now available as the FolderServer instance property 
            public MyRootFolder(ShellItemIdList idList, MSSQLServer db) : base(idList)
            {
                _Db = db;
            }

            public override IEnumerable<ShellItem> EnumItems(SHCONTF options)
            {
                DataTable oTable = _Db.GetDatatable("SELECT * FROM TBDD_USER");

                foreach (DataRow oRow in oTable.Rows)
                {
                    ShellItem oItem = new VirtualItem(this, oRow["USERNAME"].ToString().ToLower() + ".txt");
                    yield return oItem;
                }

                yield return new PhysicalItem(this, "My First Item");

                // note by default, ShellBoost uses the key/ID value as the display name if it’s not explicitly defined
                //yield return new ShellFolder(this, new StringKeyShellItemId("My First Folder"));
                //yield return new ShellItem(this, new StringKeyShellItemId("My First Item"));
            }

            protected override InformationBar GetInformationBar()
            {
                var bar = new InformationBar();
                bar.Guid = new Guid();
                bar.Message = "This is a very important folder!";
                return bar;
            }
        }

        public class VirtualItem : ShellItem
        {
            public VirtualItem(ShellFolder parent, string text): base(parent, new StringKeyShellItemId(text))
            {
                this.ItemType = ".txt";
                this.Size = 999;
                this.DateAccessed = DateTime.Now;
                this.DateCreated = DateTime.Now;
                this.DateModified = DateTime.Now;
                this.IsHidden = true;
            }

            public override ShellContent GetContent()
            {
                return new MemoryShellContent("Hallo!", System.Text.Encoding.UTF8);
            }
        }

        public class PhysicalItem : ShellItem
        {
            public PhysicalItem(ShellFolder parent, string text) : base(parent, new StringKeyShellItemId(text))
            {
                this.ItemType = ".txt";
                this.Size = 999;
                this.DateAccessed = DateTime.Now;
                this.DateCreated = DateTime.Now;
                this.DateModified = DateTime.Now;
                this.IsHidden = true;
            }

            public override ShellContent GetContent()
            {
                return new FileShellContent(@"E:\test.txt");
            }
        }
    }
}
