using System;
using System.Diagnostics;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using System.Windows;
using Unity;
using Valg.EVA.Skanning.Configuration;
using Valg.EVA.Skanning.Scan.Api;
using Valg.EVA.Skanning.Scan.ReadSoftEmulation.ScannerAccess;
using Valg.EVA.Skanning.UserInterfaceElements.UserSettings;
using Valg.EVA.Skanning.ContainerExtensions;
using Valg.EVA.Skanning.Contracts.SharedConfiguration;
using Valg.EVA.Skanning.Scan.Startup.SelectScannerModel;
using Valg.EVA.Skanning.ServicesModel.Internal.SystemSettings;

namespace Valg.EVA.Skanning.Scan.Startup
{
    public class WpfApplication : Application
    {
        private readonly string[] args;
        private static SelectScannerModel.ScannerModelSelectorWindow _scannerModelSelectorWindow;
        private static SelectScannerModel.ScannerModelSelectorWindow ScannerModelSelectorWindow
        {
            get
            {
                if (_scannerModelSelectorWindow == null) _scannerModelSelectorWindow = new SelectScannerModel.ScannerModelSelectorWindow();
                return _scannerModelSelectorWindow;
            }
        }

        public WpfApplication(string[] args)
        {
            this.args = args;
            Startup += OnStart;
        }


        private void OnStart(object sender, EventArgs e)
        {
            ShutdownMode = ShutdownMode.OnExplicitShutdown; 
            var languageResourceInitializer = new LanguageResourceInitializer();
            languageResourceInitializer.SetCurrentCulture();

            var instanceChecker = new RunningInstancesChecker();

            if (!instanceChecker.CanProceedStartup())
            {
                return;
            }
            var settingManager = new UserSettingsProvider(new FileSystem());
            var userSettings = settingManager.GetUserSettings();

            var scannerSelectionIsOk = true;
            if (userSettings.ShowSelectScannerModel || !ScannerModels.Connected.Contains(userSettings.SelectedScannerModel))
            {
                scannerSelectionIsOk = ScannerModelSelectorWindow.ShowDialog().GetValueOrDefault();
            }

            if (scannerSelectionIsOk)
            {
                StartScanWithInterpreterLibrary();
            }
            else
            {
                Application.Current.Shutdown();
            }
        }

        [Obsolete]
        private void AddRegistrySettingForEfscanHighDPIAware()
        {
            try
            {
                var keyName = @"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers";
                var valueName = @"C:\ReadSoft\FORMS\Bin\efscan.exe";
                Microsoft.Win32.Registry.SetValue(keyName, valueName, "HIGHDPIAWARE");
            }
            catch (Exception)
            {
                
            }
        }

        private static void StartScanWithInterpreterLibrary()
        {

            AssemblyInstaller.ForceLoadAssemblies();
            var container = new UnityContainer();
            AssemblyInstaller.InstallAssemblies(container);

            var systemSettingsProvider = container.Resolve<ISystemSettingsProvider>();
            var sharedSystemSettings = container.Resolve<ISharedConfigurationManager>();
            float minimumTesseractConfidence = 0.6f;

            if (!sharedSystemSettings.Configuration.RunOnce.Any())
            {
                minimumTesseractConfidence = systemSettingsProvider.GetMinimumTesseractConfidence();
            }

            var interpretInParallel = ReadSoftConfiguration.GetDefaultReadSoftConfiguration().InterpretInParallel;
            var app = new Scan.ReadSoftEmulation.ScanApp(ScannerConfig.GetCurrentConfig(), minimumTesseractConfidence, interpretInParallel);

            var api = new ReadSoftApiDotNet(); 
            api.Connect(app, "", "");
        }
    }
}
