using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
using KellermanSoftware.ContactsModule.Models;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;

namespace KellermanSoftware.ContactsModule.ViewModels
{
    public class EditContactHistoryViewModel : NotificationObject, 
                                               IDataErrorInfo
    {
        private readonly IContactHistoryListViewModel _contactHistoryListViewModel;
        private readonly DelegateCommand _saveCommand;
        private readonly ContactHistory _contactHistory;

        public EditContactHistoryViewModel(IContactHistoryListViewModel contactHistoryListViewModel,
                                           ContactHistory contactHistory)
        {
            if (contactHistoryListViewModel == null)
            {
                throw new ArgumentNullException("contactHistoryListViewModel");
            }

            _contactHistoryListViewModel = contactHistoryListViewModel;
            _contactHistory = contactHistory;

            _saveCommand = new DelegateCommand(SaveHistory, CanSaveHistory);
        }

        public ICommand SaveCommand
        {
            get { return _saveCommand; }
        }

        public IEnumerable<string> ContactTypes
        {
            get { return ContactHistory.ContactTypes; }
        }

        public string ContactType
        {
            get { return _contactHistory.ContactType; }
            set
            {
                if (value != _contactHistory.ContactType)
                {
                    _contactHistory.ContactType = value;
                    RaisePropertyChanged(() => ContactType);
                }
            }
        }

        public string Note
        {
            get { return _contactHistory.Note; }
            set
            {
                if (value != _contactHistory.Note)
                {
                    _contactHistory.Note = value;
                    RaisePropertyChanged(() => Note);
                    _saveCommand.RaiseCanExecuteChanged();
                }
            }
        }

        #region IDataErrorInfo Members

        // WPF doesn't use this property.
        public string Error
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public string this[string columnName]
        {
            get
            {
                string error = null;
                switch (columnName)
                {
                    case "Note":
                        var result = _contactHistory.ValidateNote();
                        if (!result.Item1)
                        {
                            error = result.Item2;
                        }
                        break;
                }
                return error;
            }
        }

        #endregion IDataErrorInfo Members

        private void SaveHistory()
        {
            _contactHistoryListViewModel.SaveContactHistory(_contactHistory);
        }

        private bool CanSaveHistory()
        {
            return _contactHistory.ValidateNote().Item1;
        }
    }
}
