<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading;
using NLog.Config;
using Packager.Deserializers;
using Packager.Engine;
using Packager.Factories;
using Packager.Models.PodMetadataModels;
using Packager.Models.SettingsModels;
using Packager.Observers;
using Packager.Observers.LayoutRenderers;
using Packager.Processors;
using Packager.Providers;
using Packager.UserInterface;
using Packager.Utilities.Bext;
using Packager.Utilities.Email;
using Packager.Utilities.FileSystem;
using Packager.Utilities.Hashing;
using Packager.Utilities.ProcessRunners;
using Packager.Utilities.Xml;
using Packager.Validators;
using Packager.Verifiers;
using RestSharp;
using RestSharp.Authenticators;
using SimpleInjector;

namespace Packager
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            ConfigureNLog();

            var container = Bootstrap();
            RunApplication(container);
        }

        private static void RunApplication(Container container)
        {
            try
            {
                var outputWindow = container.GetInstance<OutputWindow>();

                var app = new App();
                app.Run(outputWindow);
            }
            catch (Exception e)
            {
                var trs = 0;
                // todo: log
            }
        }

        private static Container Bootstrap()
        {
            var container = new Container();
            
            container.RegisterSingleton<IEngine, StandardEngine>();

            container.RegisterSingleton (()=>SettingsFactory.Import(ConfigurationManager.AppSettings));
            container.RegisterSingleton<IHasher, Hasher>();
            container.RegisterSingleton<IXmlExporter, XmlExporter>();
            container.RegisterSingleton<IDirectoryProvider, DirectoryProvider>();
            container.RegisterSingleton<IProcessRunner, ProcessRunner>();
            container.RegisterSingleton<IFileProvider, FileProvider>();
            container.RegisterSingleton<IIngestDataFactory, IngestDataFactory>();
            container.RegisterSingleton<ISideDataFactory, SideDataFactory>();
            container.RegisterSingleton<ICarrierDataFactory<AudioPodMetadata>, AudioCarrierDataFactory>();
            container.RegisterSingleton<ICarrierDataFactory<VideoPodMetadata>, VideoCarrierDataFactory>();
            container.RegisterSingleton<ISystemInfoProvider, SystemInfoProvider>();
            container.RegisterSingleton<IEmbeddedMetadataFactory<AudioPodMetadata>, EmbeddedAudioMetadataFactory>();
            container.RegisterSingleton<IEmbeddedMetadataFactory<VideoPodMetadata>, EmbeddedVideoMetadataFactory>();
            container.RegisterSingleton<IBwfMetaEditRunner, BwfMetaEditRunner>();
            container.RegisterSingleton<IBwfMetaEditResultsVerifier, BwfMetaEditResultsVerifier>();
            container.RegisterSingleton<IBextProcessor, BextProcessor>();
            container.RegisterSingleton<IEmailSender, EmailSender>();
            container.RegisterSingleton<IImportableFactory, ImportableFactory>();
            container.RegisterSingleton<IPodMetadataProvider, PodMetadataProvider>();
            container.RegisterSingleton<ISuccessFolderCleaner, SuccessFolderCleaner>();
            container.RegisterSingleton<IFFProbeRunner, FFProbeRunner>();

            container.RegisterSingleton<IMediaInfoProvider, MediaInfoProvider>();
            
            container.RegisterSingleton<AbstractProcessor<AudioPodMetadata>, AudioProcessor>();
            container.RegisterSingleton<AbstractProcessor<VideoPodMetadata>, VideoProcessor>();

            container.RegisterSingleton<VideoFFMPEGRunner>();
            container.RegisterSingleton<AudioFFMPEGRunner>();

            container.RegisterSingleton( ()=> new Dictionary<string, IProcessor>
            {
                {".wav", container.GetInstance<AbstractProcessor<AudioPodMetadata>>()},
                {".mkv", container.GetInstance<AbstractProcessor<VideoPodMetadata>>()}
            });

            container.RegisterSingleton(()=>new CancellationTokenSource());

            container.RegisterSingleton(()=>GetRestClient(
                container.GetInstance<IProgramSettings>(), 
                container.GetInstance<IFileProvider>(), 
                container.GetInstance<IImportableFactory>())) ;

            container.RegisterSingleton<IValidatorCollection>(()=> new StandardValidatorCollection
            {
                new ValueRequiredValidator(),
                new DirectoryExistsValidator(container.GetInstance<IDirectoryProvider>()),
                new FileExistsValidator(container.GetInstance<IFileProvider>()),
                new UriValidator(),
                new MembersValidator()
            });

            
            container.RegisterSingleton<IObserverCollection>(()=>new ObserverCollection()
                {
                    new GeneralNLogObserver(container.GetInstance<IProgramSettings>()),
                    new ObjectNLogObserver(container.GetInstance<IProgramSettings>()),
                    new ViewModelObserver(container.GetInstance<IViewModel>()),
                    new IssueEmailerObserver(
                        container.GetInstance<ProgramSettings>(), 
                        container.GetInstance<ISystemInfoProvider>(), 
                        container.GetInstance<IEmailSender>())
                });

            container.RegisterSingleton<IViewModel, ViewModel>();
            container.RegisterSingleton<OutputWindow>();
            
            container.Verify();

            return container;
            
        }

        private static IRestClient GetRestClient(IProgramSettings programSettings, IFileProvider fileProvider,
            IImportableFactory factory)
        {
            var podAuth = fileProvider.Deserialize<PodAuth>(programSettings.PodAuthFilePath);
            var result = new RestClient(programSettings.WebServiceUrl)
            {
                Authenticator = new HttpBasicAuthenticator(podAuth.UserName, podAuth.Password)
            };

            result.AddHandler("application/xml", new PodResultDeserializer(factory));
            return result;
        }

        private static void ConfigureNLog()
        {
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("ProjectCode",
                typeof(ProjectCodeLayoutRenderer));
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("LogDirectoryName",
                typeof(LoggingDirectoryLayoutRenderer));
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("Barcode",
                typeof(BarcodeLayoutRenderer));

        }
    }
}
<commit_msg>fix issue with dependency injection<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using NLog.Config;
using Packager.Deserializers;
using Packager.Engine;
using Packager.Factories;
using Packager.Models.PodMetadataModels;
using Packager.Models.SettingsModels;
using Packager.Observers;
using Packager.Observers.LayoutRenderers;
using Packager.Processors;
using Packager.Providers;
using Packager.UserInterface;
using Packager.Utilities.Bext;
using Packager.Utilities.Email;
using Packager.Utilities.FileSystem;
using Packager.Utilities.Hashing;
using Packager.Utilities.ProcessRunners;
using Packager.Utilities.Xml;
using Packager.Validators;
using Packager.Verifiers;
using RestSharp;
using RestSharp.Authenticators;
using SimpleInjector;

namespace Packager
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            ConfigureNLog();

            var container = Bootstrap();
            RunApplication(container);
        }

        private static void RunApplication(Container container)
        {
            try
            {
                var outputWindow = container.GetInstance<OutputWindow>();

                var app = new App();
                app.Run(outputWindow);
            }
            catch (Exception e)
            {
                var trs = 0;
                // todo: log
            }
        }

        private static Container Bootstrap()
        {
            var container = new Container();
            
            container.RegisterSingleton<IEngine, StandardEngine>();

            container.RegisterSingleton (()=>SettingsFactory.Import(ConfigurationManager.AppSettings));
            container.RegisterSingleton<IHasher, Hasher>();
            container.RegisterSingleton<IXmlExporter, XmlExporter>();
            container.RegisterSingleton<IDirectoryProvider, DirectoryProvider>();
            container.RegisterSingleton<IProcessRunner, ProcessRunner>();
            container.RegisterSingleton<IFileProvider, FileProvider>();
            container.RegisterSingleton<IIngestDataFactory, IngestDataFactory>();
            container.RegisterSingleton<ISideDataFactory, SideDataFactory>();
            container.RegisterSingleton<ICarrierDataFactory<AudioPodMetadata>, AudioCarrierDataFactory>();
            container.RegisterSingleton<ICarrierDataFactory<VideoPodMetadata>, VideoCarrierDataFactory>();
            container.RegisterSingleton<ISystemInfoProvider, SystemInfoProvider>();
            container.RegisterSingleton<IEmbeddedMetadataFactory<AudioPodMetadata>, EmbeddedAudioMetadataFactory>();
            container.RegisterSingleton<IEmbeddedMetadataFactory<VideoPodMetadata>, EmbeddedVideoMetadataFactory>();
            container.RegisterSingleton<IBwfMetaEditRunner, BwfMetaEditRunner>();
            container.RegisterSingleton<IBwfMetaEditResultsVerifier, BwfMetaEditResultsVerifier>();
            container.RegisterSingleton<IBextProcessor, BextProcessor>();
            container.RegisterSingleton<IEmailSender, EmailSender>();
            container.RegisterSingleton<IImportableFactory, ImportableFactory>();
            container.RegisterSingleton<IPodMetadataProvider, PodMetadataProvider>();
            container.RegisterSingleton<ISuccessFolderCleaner, SuccessFolderCleaner>();
            container.RegisterSingleton<IFFProbeRunner, FFProbeRunner>();

            container.RegisterSingleton<IMediaInfoProvider, MediaInfoProvider>();
            
            container.RegisterSingleton<AbstractProcessor<AudioPodMetadata>, AudioProcessor>();
            container.RegisterSingleton<AbstractProcessor<VideoPodMetadata>, VideoProcessor>();
            
            container.RegisterConditional<IFFMPEGRunner, VideoFFMPEGRunner>(Lifestyle.Singleton, 
                c=>c.Consumer.ImplementationType == typeof(VideoProcessor));

            container.RegisterConditional<IFFMPEGRunner, AudioFFMPEGRunner>(Lifestyle.Singleton,
                c => c.Consumer.ImplementationType == typeof(AudioProcessor));

            container.RegisterSingleton( ()=> new Dictionary<string, IProcessor>
            {
                {".wav", container.GetInstance<AbstractProcessor<AudioPodMetadata>>()},
                {".mkv", container.GetInstance<AbstractProcessor<VideoPodMetadata>>()}
            });

            container.RegisterSingleton(()=>new CancellationTokenSource());

            container.RegisterSingleton(()=>GetRestClient(
                container.GetInstance<IProgramSettings>(), 
                container.GetInstance<IFileProvider>(), 
                container.GetInstance<IImportableFactory>())) ;

            container.RegisterSingleton<IValidatorCollection>(()=> new StandardValidatorCollection
            {
                new ValueRequiredValidator(),
                new DirectoryExistsValidator(container.GetInstance<IDirectoryProvider>()),
                new FileExistsValidator(container.GetInstance<IFileProvider>()),
                new UriValidator(),
                new MembersValidator()
            });

            
            container.RegisterSingleton<IObserverCollection>(()=>new ObserverCollection()
                {
                    new GeneralNLogObserver(container.GetInstance<IProgramSettings>()),
                    new ObjectNLogObserver(container.GetInstance<IProgramSettings>()),
                    new ViewModelObserver(container.GetInstance<IViewModel>()),
                    new IssueEmailerObserver(
                        container.GetInstance<ProgramSettings>(), 
                        container.GetInstance<ISystemInfoProvider>(), 
                        container.GetInstance<IEmailSender>())
                });

            container.RegisterSingleton<IViewModel, ViewModel>();
            container.RegisterSingleton<OutputWindow>();
            
            container.Verify();

            return container;
            
        }

        private static IRestClient GetRestClient(IProgramSettings programSettings, IFileProvider fileProvider,
            IImportableFactory factory)
        {
            var podAuth = fileProvider.Deserialize<PodAuth>(programSettings.PodAuthFilePath);
            var result = new RestClient(programSettings.WebServiceUrl)
            {
                Authenticator = new HttpBasicAuthenticator(podAuth.UserName, podAuth.Password)
            };

            result.AddHandler("application/xml", new PodResultDeserializer(factory));
            return result;
        }

        private static void ConfigureNLog()
        {
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("ProjectCode",
                typeof(ProjectCodeLayoutRenderer));
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("LogDirectoryName",
                typeof(LoggingDirectoryLayoutRenderer));
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("Barcode",
                typeof(BarcodeLayoutRenderer));

        }
    }
}

{'fraction_non_alphanumeric': 0.08637443316778233, 'fraction_numerical': 0.00014395738861297056, 'mean_word_length': 2.771444082519001, 'pattern_counts': {'":': 0, '<': 111, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6468090', 'n_tokens_mistral': 3184, 'n_tokens_neox': 3095, 'n_words': 471}