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

namespace iSTP_DataProcessing
{
    /// <summary>
    /// Creates all SPS folder structure and safes current path to each folder.
    /// </summary>
    public class FolderManagerClass
    {
        #region Private global values
        private SubfolderManagerClass Avg = null;
        private SubfolderManagerClass Raw = null;
        private SubfolderManagerClass Res = null;
        private SubfolderManagerClass Sim = null;
        #endregion

        #region Public global values
        /// <summary>
        /// Gets path to Avg folder
        /// </summary>
        public string GetPath_Avg { get { return Avg.GetPath_Root; } }
        /// <summary>
        /// Gets path to Avg subfolders
        /// </summary>
        public string[] GetPath_AvgSubfolders { get { return Avg.GetPath_SubFolders; } }
        /// <summary>
        /// gets path to Raw folder
        /// </summary>
        public string GetPath_Raw { get { return Raw.GetPath_Root; } }
        /// <summary>
        /// Gets path to Raw subfolders
        /// </summary>
        public string[] GetPath_RawSubfolders { get { return Raw.GetPath_SubFolders; } }
        /// <summary>
        /// Gets path to Res folder
        /// </summary>
        public string GetPath_Res { get { return Res.GetPath_Root; } }
        /// <summary>
        /// Gets path to Res subfolders
        /// </summary>
        public string[] GetPath_ResSubfolders { get { return Res.GetPath_SubFolders; } }
        /// <summary>
        /// Gets path to Sim folder
        /// </summary>
        public string GetPath_Sim { get { return Sim.GetPath_Root; } }
        /// <summary>
        /// Gets path to Sim subfolders
        /// </summary>
        public string[] GetPath_SimSubfolders { get { return Sim.GetPath_SubFolders; } }
        #endregion

        #region Structs & Enums
        #endregion

        #region Constructors & Destructor
        /// <summary>
        /// Initialization internal array for each folder
        /// </summary>
        /// <param name="path">Roots path</param>
        public FolderManagerClass(string path)
        {
            Avg = new SubfolderManagerClass(path, SubfolderManagerClass.FolderTestEnum.Avg);
            Raw = new SubfolderManagerClass(path, SubfolderManagerClass.FolderTestEnum.Raw);
            Res = new SubfolderManagerClass(path, SubfolderManagerClass.FolderTestEnum.Res);
            Sim = new SubfolderManagerClass(path, SubfolderManagerClass.FolderTestEnum.Sim);
        }
        #endregion

        #region Private Sector
        #endregion

        #region Public Sector
        #endregion
    }

    /// <summary>
    /// Subfolder manager for each tests
    /// </summary>
    public class SubfolderManagerClass
    { 
        private readonly string[] SubFolders_Avg = new string[] { "Plots" };
        private readonly string[] SubFolders_Res = new string[] { "BiasTab", "FlightTab", "ScorsbyTab", "StaticTab", "AllanTab"};
        private readonly string[] SubFolders_Sim = new string[] { "Flight", "Scorsby", "Static" };
        private readonly string[] InSubFolders_Sim = new string[] { "Plots", "Traces" };
        private readonly string[] Folders = new string[] { "Avg", "Raw", "Res", "Sim" };

        private List<string> PathMemoryRoot = new List<string>();
        private List<string> PathMemorySubFolders = new List<string>();

        /// <summary>
        /// Gets roots path
        /// </summary>
        public string GetPath_Root { get { return PathMemoryRoot.ToArray()[0]; } }
        /// <summary>
        /// Gets path to subfolders
        /// </summary>
        public string[] GetPath_SubFolders { get { return PathMemorySubFolders.ToArray(); } }
        /// <summary>
        /// Type of subfolder
        /// </summary>
        public enum FolderTestEnum
        { 
            Avg,
            Raw,
            Res,
            Sim
        }

        /// <summary>
        /// Creates sub folders in each cycle
        /// </summary>
        /// <param name="path">Path in cycle folder</param>
        /// <param name="folder">Which type of folder has to be create</param>
        public SubfolderManagerClass(string path, FolderTestEnum folder)
        {
            string dummy = null;
            PathMemoryRoot.Clear();
            switch (folder)
            { 
                case FolderTestEnum.Avg:
                    dummy = path + Path.DirectorySeparatorChar + Folders[0];
                    PathMemoryRoot.Add(dummy + Path.DirectorySeparatorChar);
                    if (!Directory.Exists(dummy))
                    {
                        Directory.CreateDirectory(dummy);
                    }
                    foreach(string x  in SubFolders_Avg)
                    {
                        dummy = path + Path.DirectorySeparatorChar + Folders[0] + Path.DirectorySeparatorChar + x;
                        PathMemorySubFolders.Add(dummy + Path.DirectorySeparatorChar);
                        if (!Directory.Exists(dummy))
                        {
                            Directory.CreateDirectory(dummy);
                        }
                    }
                    break;

                case FolderTestEnum.Raw:
                    dummy = path + Path.DirectorySeparatorChar + Folders[1];
                    PathMemoryRoot.Add(dummy + Path.DirectorySeparatorChar);
                    if (!Directory.Exists(dummy))
                    {
                        Directory.CreateDirectory(dummy);
                    }
                    break;

                case FolderTestEnum.Res:
                    dummy = path + Path.DirectorySeparatorChar + Folders[2];
                    PathMemoryRoot.Add(dummy + Path.DirectorySeparatorChar);
                    if (!Directory.Exists(dummy))
                    {
                        Directory.CreateDirectory(dummy);
                    }
                    foreach(string x  in SubFolders_Res)
                    {
                        dummy = path + Path.DirectorySeparatorChar + Folders[2] + Path.DirectorySeparatorChar + x;
                        PathMemorySubFolders.Add(dummy + Path.DirectorySeparatorChar);
                        if (!Directory.Exists(dummy))
                        {
                            Directory.CreateDirectory(dummy);
                        }
                        if (x == SubFolders_Res[4])
                        {
                            if (!Directory.Exists(string.Format("{0}{1}Plots", dummy, Path.DirectorySeparatorChar)))
                            {
                                Directory.CreateDirectory(string.Format("{0}{1}Plots", dummy, Path.DirectorySeparatorChar));
                            }
                        }
                    }
                    break;

                case FolderTestEnum.Sim:
                    dummy = path + Path.DirectorySeparatorChar + Folders[3];
                    PathMemoryRoot.Add(dummy + Path.DirectorySeparatorChar);
                    if (!Directory.Exists(dummy))
                    {
                        Directory.CreateDirectory(dummy);
                    }
                    foreach(string x  in SubFolders_Sim)
                    {
                        dummy = path + Path.DirectorySeparatorChar + Folders[3] + Path.DirectorySeparatorChar + x;
                        PathMemorySubFolders.Add(dummy + Path.DirectorySeparatorChar);
                        if (!Directory.Exists(dummy))
                        {
                            Directory.CreateDirectory(dummy);
                        }

                        foreach (string y in InSubFolders_Sim)
                        {
                            dummy = path + Path.DirectorySeparatorChar + Folders[3] + Path.DirectorySeparatorChar + x + Path.DirectorySeparatorChar + y;
                            PathMemorySubFolders.Add(dummy + Path.DirectorySeparatorChar);
                            if (!Directory.Exists(dummy))
                            {
                                Directory.CreateDirectory(dummy);
                            }
                        }
                    }
                    break;
                    
            }
        }
    }
}
