using System;
using System.Windows;

namespace VMwareLauncher
{
    /// <summary>
    /// Interaction logic for Startup.xaml
    /// </summary>
    public partial class Startup : Window
    {
        static internal TrayIcon trayIcon = new TrayIcon();
        static internal ServiceControl serviceControl = new ServiceControl();
        internal StatusWindow statusWindow = new StatusWindow();
        internal CheckDependencies checkDependencies = new CheckDependencies();
        
        public Startup()
        {
            InitializeComponent();

            serviceControl.ServiceList = ServiceControl.DefaultVmwareServices;

            ProcessControl vmwareControl = new ProcessControl();
            vmwareControl.ProcessName = ProcessControl.DefaultVmwarePath;

            ProcessControl vmlControl = new ProcessControl();
            vmlControl.ProcessName = ProcessControl.DefaultVmlPath;

            // Only continue if no errors have been detected.
            checkDependencies.PerformCheck();
            if (ApplicationControl.BlockingErrors)
                ApplicationControl.Shutdown();

            if (!vmlControl.IsProcessRunning())
            {
                statusWindow.Show();
                trayIcon.Create();
            }
            else
            {
                vmwareControl.StartProcess();
                ApplicationControl.Shutdown();
            }
        }
    }

    public static class ApplicationControl
    {
        public static bool BlockingErrors { get; set; }

        public static void Shutdown()
        {
            Startup.trayIcon.Destroy();

            if (BlockingErrors)
                Environment.Exit(1);
            else
                Environment.Exit(0);
        }
    }
}
