using System.ServiceModel.DomainServices.Client;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using TaskIt.Assets.Resources;
using TaskIt.Core;
using TaskIt.Infrastructure;
using TaskIt.Modules.Tasks.Assets.Resources;
using TaskIt.Modules.Tasks.Core;
using TaskIt.ViewModels;
using TaskIt.Web;

namespace TaskIt.Modules.Tasks.ViewModels
{    
    public abstract class TasksViewModelBase : TaskItViewModelBase
    {
        private Project _selectedProject;        
        private Context _selectedContext;        
        private Contact _selectedContact;
        private EntityType _entityType;     
        private bool _hasNotes = true;

        public void OnNewNote(EntityType entityType)
        {
            Messenger.Default.Send(new NewEntityArgs(entityType));
            NotifyViewNoteForm();
        }

        public void OnSaveNote(EntityType entityType)
        {
            if (HasValidationErrors())
            {
                FireValidationErrorNotification();
                return;
            }
            _entityType = entityType;
            ValidationErrors.Clear();
            Messenger.Default.Send(new SaveEntityArgs(entityType));
            Utils.ShowBusyIndicator();
            Utils.DataContext.SubmitChanges(OnSaveNoteCompleted, null);
        }

        protected void OnSaveNoteCompleted(SubmitOperation so)
        {
            Utils.HideBusyIndicator();
            if (so.HasError)
            {
                Utils.ShowErrorMessage(ErrorStrings.Message_SavingNoteFailed, so.Error.Message);
                so.MarkErrorAsHandled();
                return;
            }

            Utils.DataContext.RejectChanges();
            Messenger.Default.Send(new NoteSaveCompletedArgs(_entityType));
            NotifyViewNoteList();
        }

        public void OnBackToNoteList()
        {
            ValidationErrors.Clear();
            NotifyViewNoteList();
        }

        # region Properties

        public bool HasNotes
        {
            get { return _hasNotes; }
            set { _hasNotes = value; this.OnPropertyChanged(m => m.HasNotes); }
        }

        public Project SelectedProject
        {
            get { return _selectedProject; }
            set { _selectedProject = value; this.OnPropertyChanged(p => p.SelectedProject); }
        }

        public Context SelectedContext
        {
            get { return _selectedContext; }
            set { _selectedContext = value; this.OnPropertyChanged(p => p.SelectedContext); }
        }

        public Contact SelectedContact
        {
            get { return _selectedContact; }
            set { _selectedContact = value; this.OnPropertyChanged(p => p.SelectedContact); }
        }        

        public EntitySet<TaskCategory> TaskCategories
        {
            get { return Utils.DataContext.TaskCategories; }
        }

        # endregion

        # region UI Events

        public event RoutedEventHandler ViewNoteForm;
        public void NotifyViewNoteForm()
        {
            if (ViewNoteForm != null) { ViewNoteForm(this, new RoutedEventArgs()); }
        }

        public event RoutedEventHandler ViewNoteList;
        public void NotifyViewNoteList()
        {
            if (ViewNoteList != null) { ViewNoteList(this, new RoutedEventArgs()); }
        }

        # endregion

        # region Commands

        private RelayCommand _backCommand;
        public RelayCommand BackCommand
        {
            get { return _backCommand ?? (_backCommand = new RelayCommand(OnBack)); }
        }

        private RelayCommand<EntityType> _newNoteCommand;
        public RelayCommand<EntityType> NewNoteCommand
        {
            get { return _newNoteCommand ?? (_newNoteCommand = new RelayCommand<EntityType>(OnNewNote)); }
        }

        private RelayCommand<EntityType> _saveNoteCommand;
        public RelayCommand<EntityType> SaveNoteCommand
        {
            get { return _saveNoteCommand ?? (_saveNoteCommand = new RelayCommand<EntityType>(OnSaveNote)); }
        }

        private RelayCommand _backToNoteListCommand;
        public RelayCommand BackToNoteListCommand
        {
            get { return _backToNoteListCommand ?? (_backToNoteListCommand = new RelayCommand(OnBackToNoteList)); }
        }

        # endregion

        # region Resource Strings

        public string ResourceTabHeaderNotes
        {
            get { return TasksPageCommonStrings.TabHeader_Notes; }
        }

        public string ResourceTabHeaderAttachments
        {
            get { return TasksPageCommonStrings.TabHeader_Attachments; }
        }

        public string ResourceFormLabelDescription
        {
            get { return TasksPageCommonStrings.FormLabel_Description; }
        }

        public string ResourceFormLabelDue
        {
            get { return TasksPageCommonStrings.FormLabel_Due; }
        }

        public string ResourceFormLabelCompleted
        {
            get { return TasksPageCommonStrings.FormLabel_Completed; }
        }

        public string ResourceFormLabelNoteText
        {
            get { return TasksPageCommonStrings.FormLabel_NoteText; }
        }

        public string ResourceNoNotesMessage
        {
            get { return TasksPageCommonStrings.NoNotesMessage; }
        }

        # endregion

    }
}