using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
using Valg.EVA.Skanning.Resources.Language;
using Valg.EVA.Skanning.UserInterfaceElements.MessageBox;
using Valg.EVA.Skanning.Scan.Startup.TerminateProcess;
using MessageBox = Valg.EVA.Skanning.UserInterfaceElements.MessageBox.MessageBox;

namespace Valg.EVA.Skanning.Scan.Startup
{
    public class RunningInstancesChecker
    {
        public bool CanProceedStartup()
        {
            var proceed = true;
            var isEfscanRunning = DetermineTargetProcessIsRunning();
            if (isEfscanRunning)
            {
                var shallTerminateTargetProcess = DetermineTerminationAction();

                if (shallTerminateTargetProcess)
                {
                    proceed = TerminateExistingEfscanProcess();
                }
                else
                {
                    proceed = false;
                }
            }

            return proceed;
        }

        private bool DetermineTerminationAction()
        {
            var dialog = new TerminateTargetProcessDialog();
            return dialog.ShowDialog().GetValueOrDefault();
        }

        private static bool TerminateExistingEfscanProcess()
        {
            var efscanProcesses = GetEfscanProcess();
            var errorDetected = false;
            Exception lastException = null;
            bool? exitSuccessful = null;

            try
            {
                Mouse.OverrideCursor = Cursors.Wait;

                foreach (var efscanProcess in efscanProcesses)
                {
                    exitSuccessful = false;

                    if (efscanProcess != null && !efscanProcess.HasExited)
                    {
                        efscanProcess.Kill();

                        exitSuccessful = efscanProcess.WaitForExit(10000);
                    }
                }
            }
            catch (Exception ex)
            {
                errorDetected = true;
                lastException = ex;
            }
            finally
            {
                Mouse.OverrideCursor = null;
            }

            if (errorDetected || (exitSuccessful.HasValue && !exitSuccessful.Value))
            {
                var messageBoxViewModel = new MessageBoxViewModel();
                var messageBox = new MessageBox(messageBoxViewModel);
                messageBox.ShowWindowWhenNoParentWindow(ScanLanguageString.CouldNotForceShutdownOfEvaScan, ScanLanguageString.CouldNotForceShutdownOfEvaScanErrorDescription, 
                    ScanLanguageString.CouldNotForceShutdownOfEvaScanPossibleSolutions, MessageBoxType.Error, lastException);
            }

            return !exitSuccessful.HasValue || exitSuccessful.Value;
        }

        private bool DetermineTargetProcessIsRunning()
        {
            return GetEfscanProcess().Any();
        }

        private static Process[] GetEfscanProcess()
        {
            return Process.GetProcessesByName("efscan");
        }
    }
}
