using System;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace VMwareLauncher
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class StatusWindow : Window
    {
        BackgroundWorker runProcess = new BackgroundWorker();
        BackgroundWorker loadServices = new BackgroundWorker();
        BackgroundWorker stopServices = new BackgroundWorker();


        public StatusWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer delayWindowHide = new DispatcherTimer();
            delayWindowHide.Tick += DelayWindowHide_Tick;
            delayWindowHide.Interval = new TimeSpan(0, 0, 2);

            runProcess.DoWork += RunProcess_DoWork;
            loadServices.DoWork += LoadServices_DoWork;
            stopServices.DoWork += StopServices_DoWork;

            delayWindowHide.Start();
            loadServices.RunWorkerAsync();
        }

        private void DelayWindowHide_Tick(object sender, EventArgs e)
        {
            Application.Current.Dispatcher.Invoke(() => { Visibility = Visibility.Hidden; });
        }

        private void StopServices_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                foreach (string service in ServiceControl.VMwareServiceList)
                {
                    ServiceController sc = new ServiceController(service, Environment.MachineName.ToString());

                    if (sc.Status.Equals(ServiceControllerStatus.Running))
                    {
                        sc.Stop();
                        sc.WaitForStatus(ServiceControllerStatus.Stopped);
                    }
                }
                ApplicationControl.Shutdown(0);
            }
            catch (Exception) { throw; }
        }

        private void LoadServices_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach (string service in ServiceControl.VMwareServiceList)
            {
                ServiceController sc = new ServiceController(service, Environment.MachineName.ToString());

                try
                {

                    if (sc.Status.Equals(ServiceControllerStatus.Stopped))
                    {
                        sc.Start();
                        sc.WaitForStatus(ServiceControllerStatus.Running);
                    }
                    if (sc.Status.Equals(ServiceControllerStatus.Running))
                    { }
                }
                catch (Exception) { throw; }
            }
            runProcess.RunWorkerAsync();
        }

        private void RunProcess_DoWork(object sender, DoWorkEventArgs e)
        {
            ProcessControl processControl = new ProcessControl();
            processControl.ProcessName = ProcessControl.DefaultVmwarePath;
            processControl.StartProcess();

            bool isVmwareRunning = true;
            while (isVmwareRunning || TrayIcon.KeepLauncherRunning)
            {
                Thread.Sleep(3000);

                isVmwareRunning = false; // Default value.
                foreach (Process instance in Process.GetProcessesByName(ProcessControl.DefaultVmwareExe))
                isVmwareRunning = true;
            }
            stopServices.RunWorkerAsync();
        }

        /// <summary>
        /// Disable user from terminating application from close button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, CancelEventArgs e) { e.Cancel = true; }
    }
}
