﻿using Autofac;
using Topshelf;
using Topshelf.Autofac;

namespace EasyConfig.Git
{
    public static class EntryPoint
    {
        public static void Main()
        {
            var containerBuilder = new ContainerBuilder();
            ConfigureContainer(containerBuilder);
            var container = containerBuilder.Build();
            HostFactory.Run(host =>
            {
                host.SetServiceName("EasyConfig");
                host.UseAutofacContainer(container);
                host.Service<IService>(settings =>
                {
                    settings.ConstructUsingAutofacContainer();
                    settings.WhenStarted((service, control) =>
                    {
                        service.Start();
                        return true;
                    });
                    settings.WhenStopped((service, control) =>
                    {
                        service.Stop();
                        return true;
                    });
                });
            });
        }

        private static void ConfigureContainer(ContainerBuilder containerBuilder)
        {
            containerBuilder.RegisterType(typeof (Service)).AsImplementedInterfaces().SingleInstance();
            containerBuilder.RegisterType(typeof (Consul)).AsImplementedInterfaces().SingleInstance();
            containerBuilder.RegisterType(typeof (Git)).AsImplementedInterfaces().SingleInstance();
            containerBuilder.RegisterType(typeof (FileSystem)).AsImplementedInterfaces().SingleInstance();
            containerBuilder.RegisterType(typeof (Shell)).AsImplementedInterfaces().SingleInstance();
            containerBuilder.RegisterType(typeof (RepoExporter)).AsImplementedInterfaces().SingleInstance();
            containerBuilder.RegisterType(typeof (Config)).AsImplementedInterfaces().SingleInstance();
            containerBuilder.RegisterType(typeof (Logger)).AsImplementedInterfaces().SingleInstance();
        }
    }
}