using System.Collections.Generic;
using Microsoft.VisualBasic.ApplicationServices;
using NotepadEnhanced.AppState;
using NotepadEnhanced.GUI.Forms;

namespace NotepadEnhanced
{
    class SingleInstanceBase : WindowsFormsApplicationBase
    {
        private static List<string> _cmdLineArgs = new List<string>();

        public SingleInstanceBase(IEnumerable<string> bootArgs)
        {
            this.IsSingleInstance = UserSettings.Instance.SingleInstance;
            _cmdLineArgs = new List<string>(bootArgs);
        }

        protected override bool OnStartup(StartupEventArgs eventArgs)
        {
            this.MainForm = new MainForm();
            return base.OnStartup(eventArgs);
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
            eventArgs.BringToForeground = true;
            _cmdLineArgs = new List<string>(eventArgs.CommandLine);
            ((MainForm)this.MainForm).OpenFileFromArguments(CmdLineArgs);
            this.MainForm.BringToFront();
        }

        public static string[] CmdLineArgs
        {
            get { return _cmdLineArgs.ToArray(); }
        }
    }
}
