using KellermanSoftware.ContactsModule.Controllers;
using KellermanSoftware.ContactsModule.DataAccess;
using KellermanSoftware.ContactsModule.ViewModels;
using KellermanSoftware.ContactsModule.Views;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;

namespace KellermanSoftware.ContactsModule
{
    public sealed class ContactsModule : IModule
    {
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;
        private EditContactDetailsController _editContactDetailsController;
        private EditContactHistoryController _editContactHistoryController;
        
        public ContactsModule(IUnityContainer container, 
                              IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
        }

        public void Initialize()
        {
            _container.RegisterType<IRepository, Repository>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IConfirmationController, ConfirmationController>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IContactsViewModel, ContactsViewModel>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IContactDetailsViewModel, ContactDetailsViewModel>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IContactHistoryListViewModel, ContactHistoryListViewModel>(new ContainerControlledLifetimeManager());

            _editContactDetailsController = 
                _container.Resolve<EditContactDetailsController>();
            _editContactHistoryController =
                _container.Resolve<EditContactHistoryController>();

            // Show the ContactsView in the shell's ContactsRegion.
            _regionManager.RegisterViewWithRegion("ContactsRegion",
                                                  () => _container.Resolve<ContactsView>());
            // Show the ContactDetailsView in the shell's ContactDetailsRegion.
            _regionManager.RegisterViewWithRegion("ContactDetailsRegion",
                                                  () => _container.Resolve<ContactDetailsView>());
            // Show the ContactHistoryView in the shell's ContactHistoryRegion.
            _regionManager.RegisterViewWithRegion("ContactHistoryRegion",
                                                  () => _container.Resolve<ContactHistoryView>());
        }
    }
}
