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 history creation or editing. 
    /// When a contact history creation or editing started,
    /// the EditContactHistoryView is created and showed.
    /// </summary>
    public sealed class EditContactHistoryController
    {
        private readonly IEventAggregator _eventAggregator;

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

            _eventAggregator = eventAggregator;

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

        private void EditContactHistory(EditContactHistoryViewModel viewModel)
        {
            EditContactHistoryView view = new EditContactHistoryView(viewModel);
            view.ShowDialog();
        }
    }
}
