﻿namespace Meeko.Universal.Unity
{
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity;
    using Registries;
    using System.Collections.Generic;

    public static class UnityBootstrapper
    {
        public static UnityContainer Container { get; set; }

        public static List<IRegistry> Registries { get; private set; }

        public static void Init()
        {
            Container = new UnityContainer();
            Registries = new List<IRegistry>();

            var serviceLocator = new UnityServiceLocator(Container);
            ServiceLocator.SetLocatorProvider(() => serviceLocator);
        }

        public static void ConfigureRegistries()
        {
            AddRegistries();

            foreach (var registry in Registries)
                registry.Configure();
        }

        public static void ConfigureNavigationRegistry()
        {
            var registry = new NavigationRegistry(Container);
            registry.Configure();
            Registries.Add(registry);
        }

        private static void AddRegistries()
        {
            Registries.Add(new RepositoryContextRegistry(Container));
            Registries.Add(new RepositoryRegistry(Container));
            Registries.Add(new ServiceRegistry(Container));
            Registries.Add(new ViewModelRegistry(Container));
            Registries.Add(new ViewRegistry());
        }
    }
}
