using System.Collections.Generic;
using System.IO;

namespace XuLiChuoi
{
    class EnDeCodeDirInfo
    {


        public string encode(string path)
        {
            if (System.IO.Directory.Exists(path))
            {
                string kq = "$";
                IEnumerable<string> enums = Directory.EnumerateDirectories(path);
                List<string> dirs = new List<string>(enums);
                DirectoryInfo dirInfo = new DirectoryInfo(path);
                FileInfo[] file = dirInfo.GetFiles();
                int sl = dirs.Count + file.Length;
                kq += sl.ToString() + ";";
                foreach (var dir in dirs)
                {
                    kq += dir.Replace(path + "\\", "") + ";0;";
                }
                for (int i = 0; i < file.Length; i++)
                {
                    kq += file[i].FullName.Replace(path + "\\", "") + ";1;";
                }
                kq = kq.Substring(0, kq.Length - 1);
                kq += "#";
                return kq;
            }
            return "Đường dẫn không tồn tại.";
        }

        public string decode(string S)
        {
            if (S.Length != 0)
            {
                string kq = "";
                if (S[0] == '$' && S[1] != '#')
                {
                    int sl = int.Parse(S[1].ToString());
                    kq += "\nTổng Folder và File là: " + sl.ToString();
                    int l = S.Length;
                    string s = S.Substring(3, l - 4);
                    string[] temp = s.Split(";");
                    int i = 0;
                    kq += "\n------------------";
                    kq += "\nDanh sách Folder:";
                    while (i < temp.Length - 1)
                    {
                        if (temp[i + 1] == "0") kq += "\n" + temp[i];
                        else
                        {
                            kq += "\n------------------";
                            kq += "\nDanh sách File:";
                            break;
                        }
                        i += 2;
                    }
                    while (i < temp.Length - 1)
                    {
                        if (temp[i + 1] == "1")
                            kq += "\n" + temp[i];
                        i += 2;
                    }
                }
                if (kq == "")
                    return "Không có thư mục/file nào.";
                return kq;
            }
            return "Chuỗi sai định dạng!";
        }
    }
}