using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
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.Web;

namespace TaskIt.Modules.Tasks.ViewModels
{
    [Export]
    public class ContextViewModel : TasksViewModelBase
    {
        private Context _selectedItem;
        private ContextNote _selectedNote;

        public ContextViewModel()
        {       
            Messenger.Default.Register<NewEntityArgs>(this, OnNewItem);
            Messenger.Default.Register<EditEntityArgs>(this, OnEditItem);
            Messenger.Default.Register<SaveEntityArgs>(this, OnSaveItem);
            Messenger.Default.Register<NoteSaveCompletedArgs>(this, OnNoteSaveCompleted);  
        }

        protected void OnNewItem(NewEntityArgs args)
        {
            if (IsContext(args))
            {

                Mode = ItemMode.Add;
                SelectedItem = new Context
                {
                    Name = CommonStrings.Watermark_Name,
                    UserID = Utils.CurrentUserID,
                    CreatedDate = DateTime.Now
                };
            }

            if (IsContextNote(args))
            {
                SelectedNote = new ContextNote
                {
                    Text = CommonStrings.Watermark_Name,
                    UserID = Utils.CurrentUserID,
                    ContextID = SelectedItem.ID,
                    CreatedDate = DateTime.Now
                };
            }
        }

        protected void OnEditItem(EditEntityArgs args)
        {
            if (!IsContext(args))
            {
                return;
            }

            Mode = ItemMode.Edit;
            SelectedItem = Contexts.Where(p => p.ID == args.ID).FirstOrDefault();
        }

        protected void OnSaveItem(SaveEntityArgs args)
        {
            if (IsContext(args) && Mode == ItemMode.Add)
            {
                Utils.DataContext.Contexts.Add(SelectedItem);
            }
            else if (IsContextNote(args))
            {
                Utils.DataContext.ContextNotes.Add(SelectedNote);
            }
        }

        protected void OnNoteSaveCompleted(NoteSaveCompletedArgs args)
        {
            if (!IsContextNote(args))
            {
                return;
            }
            HasNotes = true;
            this.OnPropertyChanged(p => p.Notes);
        }

        public override void OnLoadNotes()
        {
            base.OnLoadNotes();
            DataProvider.GetContextNotes(SelectedItem.ID, OnNotesLoaded);
        }

        void OnNotesLoaded(LoadOperation<ContextNote> lo)
        {
            if (lo.HasError)
            {
                Utils.ShowErrorMessage(TasksErrorStrings.Message_LoadingNotesFailed, lo.Error.Message);
                lo.MarkErrorAsHandled();
                return;
            }

            this.OnPropertyChanged(p => p.Notes);
            Utils.HideBusyIndicator();
        }

        # region Properties

        private static bool IsContext(EntityArgs args)
        {
            return args.EntityType == EntityType.Context;
        }

        private static bool IsContextNote(EntityArgs args)
        {
            return args.EntityType == EntityType.ContextNote;
        }

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

        public ContextNote SelectedNote
        {
            get { return _selectedNote; }
            set { _selectedNote = value; this.OnPropertyChanged(p => p.SelectedNote); }
        }

        public EntityType EntityType
        {
            get { return EntityType.Context; }
        }

        public EntityType NoteEntityType
        {
            get { return EntityType.ContextNote; }
        }

        public IOrderedEnumerable<ContextNote> Notes
        {
            get { return Utils.DataContext.ContextNotes.OrderByDescending(n => n.CreatedDate); }
        }

        # endregion

        # region Resource Strings

        public string ResourceMessageSaveBeforeAddingNotes
        {
            get { return ContextsStrings.Message_SaveBeforeAddingNotes; }
        }

        # endregion

    }
}