using System;
using System.Collections.Generic;
using NotepadEnhanced.Properties;
using System.Collections.Specialized;

namespace NotepadEnhanced
{
    static class RecentFileQue
    {
        /// <summary>
        /// The string collection is null sometimes, it may need to be instantiated
        /// This needs to be called before the list can be used to avoid errors
        /// </summary>
        internal static void Initialize()
        {
            if (Settings.Default.recentFiles == null)
            {
                Settings.Default.recentFiles = new StringCollection();
            }
        }

        internal static void AddFiles(params string[] filenames)
        {
            foreach (string s in filenames)
            {
                if (!IsNewFile(s))
                {
                    Settings.Default.recentFiles.Remove(s);
                }

                Settings.Default.recentFiles.Add(s);
            }

            LimitQue();
        }

        /// <summary>
        /// This will allow us to treat the stringCollection as a que
        /// </summary>
        internal static void LimitQue()
        {
                while (Settings.Default.recentFiles.Count > Settings.Default.queSize)
                {
                    Settings.Default.recentFiles.RemoveAt(0);
                }
        }

        private static bool IsNewFile(string fileName) 
        {
            foreach (string s in Settings.Default.recentFiles)
            {
                if (fileName == s)
                {
                    return false;
                }
            }

            return true;
        }

        public static void ClearRecentList()
        {
            if (Settings.Default.recentFiles != null)
            {
                Settings.Default.recentFiles.Clear();
            }
        }

        public static StringCollection Filenames
        {
            get
            {
                return StringOps.ReverseStringCollection(Settings.Default.recentFiles);
            }
        }
    }
}
