using System;
using KellermanSoftware.ContactsModule.Events;
using KellermanSoftware.ContactsModule.ViewModels;
using KellermanSoftware.ContactsModule.Views;
using Microsoft.Practices.Prism.Events;

namespace KellermanSoftware.ContactsModule.Controllers
{
    /// <summary>
    /// This controller subscribes to a loosely coupled event,
    /// which is published when the user starts a contact creation or editing.
    /// When a contact creation or editing started,
    /// the EditContactDetailsView is created and showed.
    /// </summary>
    public sealed class EditContactDetailsController
    {
        private readonly IEventAggregator _eventAggregator;

        public EditContactDetailsController(IEventAggregator eventAggregator)
        {
            if (eventAggregator == null)
            {
                throw new ArgumentNullException("eventAggregator");
            }

            _eventAggregator = eventAggregator;

            // Subscribe to the EditContactDetailsEvent event.
            _eventAggregator.GetEvent<EditContactDetailsEvent>().Subscribe(EditContactDetails, 
                                                                           true);
        }

        private void EditContactDetails(EditContactDetailsViewModel viewModel)
        {
            EditContactDetailsView view = new EditContactDetailsView(viewModel);
            view.ShowDialog();
        }
    }
}
