using System;
using System.IO;
using System.Reflection;
using Valg.EVA.Skanning.ContainerExtensions;
using Valg.EVA.Skanning.Scan.ReadSoftContracts.Events;
using Valg.EVA.Skanning.UserInterfaceElements.DatabaseVerifier;
using Valg.EVA.Skanning.UserInterfaceElements.MainWindowProvider;
using Prism.Events;
using Valg.Eva.Skanning.Scan.Presentation.MainWindow;
using Valg.EVA.Skanning.Scan.Presentation;
using Valg.EVA.Skanning.ServicesModel.Internal.Interpreter;
using Valg.EVA.Skanning.UserInterfaceElements;
using System.Linq;
using System.Threading.Tasks;

namespace Valg.Eva.Skanning.Scan.Presentation
{
    public class MainWindowInitializer : IMainWindowInitializer, IStartableComponent
    {
        private readonly IEventAggregator eventAggregator;
        private readonly IWindowHider windowHider;
        private readonly ITypedFactory typedFactory;
        private readonly IMainWindowProvider mainWindowProvider;
        private bool scanAppConnected;
        private bool databaseVerified;
        private bool isElectionConfigInstalled;

        public MainWindowInitializer(IEventAggregator eventAggregator, IWindowHider windowHider, ITypedFactory typedFactory, IMainWindowProvider mainWindowProvider)
        {
            WebBrowserHelper.FixBrowserVersion("efscan");
            this.eventAggregator = eventAggregator;
            this.windowHider = windowHider;
            this.typedFactory = typedFactory;
            this.mainWindowProvider = mainWindowProvider;
        }

        public void Start()
        {
            var connectedEvent = eventAggregator.GetEvent<ScanAppSuccessfullyConnectedEvent>();
            connectedEvent.Subscribe(args =>
            {
                scanAppConnected = true;
                TryInitializeMainWindow();
            });

            var databasedVerifiedEvent = eventAggregator.GetEvent<DatabaseConsistencyVerifiedEvent>();
            databasedVerifiedEvent.Subscribe(args =>
            {
                databaseVerified = true;
                isElectionConfigInstalled = args.IsElectionConfigInstalled;
                TryInitializeMainWindow();
            });
        }

        private void TryInitializeMainWindow()
        {
            if (databaseVerified && scanAppConnected)
            {
                try
                {
                    windowHider.Hide();
                }
                catch (Exception)
                {
                    
                }

                var mainWindow = typedFactory.Create<IMainWindow>(); 
                ((System.Windows.Window)mainWindow).Loaded += (fff, ggg) =>
                {
                    var leroy = 234;
                    Console.WriteLine(leroy);
                };

                mainWindowProvider.SetMainWindowManually(mainWindow.AsWindow);

                var isKioskMode = Environment.GetCommandLineArgs().Contains("-kiosk");
                mainWindow.ShowWindow(isElectionConfigInstalled, isKioskMode);
            }
        }
    }
}
