using System.IO;
using System.Web.UI.WebControls;
using System;

namespace Sex
{
    public partial class Sex : System.Web.UI.Page
    {
        public class ListFileDirectory : MyList
        {
            private static string GetDateTime(DateTime d)
            {
                return d.Year.ToString().PadLeft(4, '0') + "&middot;" + d.Month.ToString().PadLeft(2, '0') + "&middot;" + d.Day.ToString().PadLeft(2, '0') + "&nbsp;" + d.TimeOfDay.Hours.ToString().PadLeft(2, '0') + ":" + d.TimeOfDay.Minutes.ToString().PadLeft(2, '0');
            }
            private static string GetAttributeWithCaption(string Attribute, string Caption)
            {
                return "<span title='" + Caption + "'>" + Attribute + "</span>";
            }
            private static string GetAttributesWithCaption()
            {
                string s = "";
                s += GetAttributeWithCaption("A", "Archive");
                s += GetAttributeWithCaption("C", "Compressed");
                s += GetAttributeWithCaption("D", "Directory");
                s += GetAttributeWithCaption("E", "Encrypted");
                s += GetAttributeWithCaption("H", "Hidden");
                s += GetAttributeWithCaption("I", "Indexed");
                s += GetAttributeWithCaption("N", "Normal");
                s += GetAttributeWithCaption("O", "Offline");
                s += GetAttributeWithCaption("P", "Reparse Point");
                s += GetAttributeWithCaption("R", "Read Only");
                s += GetAttributeWithCaption("S", "Sparse File");
                s += GetAttributeWithCaption("T", "Temporary");
                s += GetAttributeWithCaption("X", "Device");
                s += GetAttributeWithCaption("Y", "System");
                return s;
            }
            private static string GetCaption(string Class)
            {
                return "<span class='" + Class + "'>" + "Created".PadRight(17) + "Accessed".PadRight(17) + "Changed".PadRight(17) + (GetAttributesWithCaption()).PadRight(15) + "Length".PadLeft(11) + " Name</span>\r\n";
            }
            private string[] GetItemProperties(FileSystemInfo di, long len)
            {
                string Extension = di.Extension.ToLower();
                string[] s = new string[7];
                if (len == -1) len = ((FileInfo)di).Length;
                s[0] = len.ToString().PadLeft(12);
                s[1] = GetDateTime(di.CreationTime).PadRight(17); // File_DateCreated 
                s[2] = GetDateTime(di.LastAccessTime).PadRight(17); // File_DateAccessed
                s[3] = GetDateTime(di.LastWriteTime).PadRight(17); // File_DateChanged
                string a = "";
                FileAttributes fa = di.Attributes;
                a += (fa == FileAttributes.Archive ? "A" : ".");
                a += (fa == FileAttributes.Compressed ? "C" : ".");
                a += (fa == FileAttributes.Directory ? "D" : ".");
                a += (fa == FileAttributes.Encrypted ? "E" : ".");
                a += (fa == FileAttributes.Hidden ? "H" : ".");
                a += (fa == FileAttributes.NotContentIndexed ? "." : "I");
                a += (fa == FileAttributes.Normal ? "N" : ".");
                a += (fa == FileAttributes.Offline ? "O" : ".");
                a += (fa == FileAttributes.ReparsePoint ? "P" : ".");
                a += (fa == FileAttributes.ReadOnly ? "R" : ".");
                a += (fa == FileAttributes.SparseFile ? "S" : ".");
                a += (fa == FileAttributes.Temporary ? "T" : ".");
                a += (fa == FileAttributes.Device ? "X" : ".");
                a += (fa == FileAttributes.System ? "Y" : ".");
                s[4] = a;
                string Type = "";
                string MimeType = "";
                if (fa == FileAttributes.Directory)
                {
                    Type = "Directory";
                }
                else
                {
                    FileTypeInfo fti = FileTypes.Get(Extension);
                    if (fti != null)
                    {
                        FileGroup ea = FileGroups.Get(FileTypes.Get(Extension).Type);
                        Type = ea.Description;
                        MimeType = Mime.GetMimeType(Extension); if (string.IsNullOrEmpty(MimeType)) MimeType = "";
                    }
                }
                s[5] = Type;
                s[6] = MimeType;
                return s;
            }
            private static string[] GetTitleString()
            {
                return new string[] { "Length", "Created", "LastAccess", "Changed", GetAttributesWithCaption(), "FileGroup", "MimeType" };
            }
            // Directory Properties
            public ListFileDirectory(State MyState)
                : base(MyState, "", "Name", GetTitleString())
            {
                FileInfo fi = new FileInfo(MyState.CurrentPathAbs);
                Add(fi.Name.Replace(" ", "&nbsp;"), GetItemProperties(fi, -1));
            }
            // File Properties
            public ListFileDirectory(State MyState, long Length)
                : base(MyState, "", "Name", GetTitleString())
            {
                DirectoryInfo fi = new DirectoryInfo(MyState.CurrentPathAbs);
                Add(fi.Name.Replace(" ", "&nbsp;"), GetItemProperties(fi, Length));
            }
            // Directory Content
            public ListFileDirectory(State MyState, TreeNode tn)
                : base(MyState, "Directory: " + new ItemInfo(tn.Value).name, "Name", GetTitleString())
            {
                for (int i = 0; i < tn.ChildNodes.Count; i++)
                {
                    string val = tn.ChildNodes[i].Value;
                    ItemInfo ii = new ItemInfo(val);
                    string Name = ii.name;
                    Name = Name.Replace(" ", "&nbsp;");
                    switch (ii.type)
                    {
                        case E_ObjectType.DIRECTORY:
                            Add(ii.name, GetItemProperties(new DirectoryInfo(MyState.CurrentPathAbs + "/" + ii.name), Tree.Size(tn.ChildNodes[i])));
                            break;
                        case E_ObjectType.FILE:
                            Add(ii.name, GetItemProperties(new FileInfo(MyState.CurrentPathAbs + "/" + ii.name), -1));
                            break;
                    }
                }
            }
        }
    }
}