using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Foo.DataAccessLayer.DataTypes;

namespace Foo.WorkspaceMgr
{
    internal static class HelperFunc
    {
        internal const int DEFAULT_SHOWNHIDE_SLEEP_TIME = 250;
        internal const int DEFAULT_CLOSE_SLEEP_TIME = 500;
        internal const int DEFAULT_LAUNCH_SLEEP_TIME = 1000;        

        /// <summary>
        /// Depending on the Quality of the Index Optimizations,
        /// Return what the best type of action is afterward
        /// </summary>
        internal enum IndexOptimizationQuality
        {
            Good,
            Average,
            Poor
        }

        // Helper Class for OptimalLaunchIndexesForFileArtifacts
        internal class OptimalIndexes
        {
            public int Index;
            public string FileName;
            internal OptimalIndexes(int Index, string FileName)
            {
                this.Index = Index;
                this.FileName = FileName;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="quality"></param>
        /// <returns></returns>
        internal static int GetSleepTimeForQuality(IndexOptimizationQuality quality)
        {
            int nSleepTime = 1350;
            switch (quality)
            {
                case HelperFunc.IndexOptimizationQuality.Good:
                    nSleepTime = 1350;
                    break;

                case HelperFunc.IndexOptimizationQuality.Average:
                    nSleepTime = 1675;
                    break;

                case HelperFunc.IndexOptimizationQuality.Poor:
                    nSleepTime = 2000;
                    break;
            }
            return nSleepTime;
        }

        /// <summary>
        /// For Launching of File Artifacts we must make sure that artifacts are in an order that allows us
        /// to properly resolve them dynamically. When files with the same name get launched too close together,
        /// window resolution will become increasingly difficult.
        /// </summary>
        /// <param name="artifactItems">a group of artifacts (Only file Artifacts will be processed)</param>
        /// <returns>an array of indexes that specifies the launch order of all the files in this group</returns>
        internal static IndexOptimizationQuality OptimalLaunchIndexesForFileArtifacts(ArtifactItem[] artifactItems, out int[] optimalIndexes)
        {           
                        
            // Step One - If it is a file type, then we get the file name and it's index to put into a list
            List<OptimalIndexes> optIndex = new List<OptimalIndexes>();
            for (int i = 0; i < artifactItems.Length; ++i)
            {
                if(artifactItems[i].IsFile)
                   optIndex.Add(new OptimalIndexes(i, artifactItems[i].FileName));
            }

            // Step Two - Sort the List by FileName
            optIndex.Sort(delegate(OptimalIndexes o1, OptimalIndexes o2) { return o1.FileName.CompareTo(o2.FileName); });

            // Step Three - Go thru sorted list and get out all the Singles
            List<int> singlesList = new List<int>();
            for (int i = 0; i < optIndex.Count; ++i)
            {
                bool bIsSingle = true;
                bool bCompareUp = ((i > 0) && (i < optIndex.Count));
                bool bCompareDown = (i < (optIndex.Count - 1));

                if (bIsSingle && bCompareUp)
                    bIsSingle = (String.Compare(optIndex[i].FileName, optIndex[i - 1].FileName, true) != 0);

                if (bIsSingle && bCompareDown)
                    bIsSingle = (String.Compare(optIndex[i].FileName, optIndex[i + 1].FileName, true) != 0);

                // If Single - Add To Singles List
                if (bIsSingle)
                    singlesList.Add(i);                
            }

            // If all items in the List were singles then we are really good to go 
            // and can exit out of here
            if (singlesList.Count == optIndex.Count)
            {
                // Pass out the Indexes of the Array To The Caller
                optimalIndexes = new int[optIndex.Count];
                for (int i = 0; i < optIndex.Count; ++i)
                    optimalIndexes[i] = optIndex[i].Index;
                
                // Since there are no doubles - this is a low risk / high quality optimization
                return IndexOptimizationQuality.Good;
            }

            // Step Four - There are doubles and/or no singles
            //             ~remove all singles from optIndex            
            foreach (int index in singlesList)
            {
                
            }





            // 2) we make a list of all filenames in sorted order (with an index to which artifact it belongs)


            // 3) we then generate the most optimal list of indexes that we pass out for the caller to use

            foreach (ArtifactItem artifact in artifactItems)
            {
                

            }

            optimalIndexes = new int[1];

            return IndexOptimizationQuality.Average;
        }



    }
}
