using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using Commands1.ViewModels;
using Commands2.Core;
using Commands2.Web;
using Commands2.Web.Services;
using GalaSoft.MvvmLight.Command;

namespace Commands2.ViewModels
{
    public class TasksViewModel : ViewModelBase
    {
        private TaskCategory _selectedTaskCategory;
        private Task _selectedTask;

        public TasksViewModel()
        {
            DataContext = new DataContext();
            LoadData();
        }

        private void LoadData()
        {
            LoadTaskCategories();
        }

        public LoadOperation<TaskCategory> LoadTaskCategories()
        {
            // Get the data via WCF RIA Services. When the call has returned, called OnTaskCategoriesLoaded.
            return DataContext.Load(DataContext.GetTaskCategoriesQuery(), OnTaskCategoriesLoaded, false);
        }

        void OnTaskCategoriesLoaded(LoadOperation<TaskCategory> lo)
        {
            TaskCategories = lo.Entities;

            // Notify the UI that TaskCategories property has changed
            this.OnPropertyChanged(m => m.TaskCategories);
        }

        public LoadOperation<Task> LoadTasks()
        {
            // Get the data via WCF RIA Services. When the call has returned, called OnTasksLoaded.            
            return DataContext.Load(DataContext.GetTasksByCategoryQuery(SelectedTaskCategory.ID), OnTasksLoaded, false);
        }

        void OnTasksLoaded(LoadOperation<Task> lo)
        {
            Tasks = lo.Entities;

            // Notify the UI that Tasks property has changed
            this.OnPropertyChanged(m => m.Tasks);
            this.OnPropertyChanged(m => m.ShowNoTasksMessage);
            this.OnPropertyChanged(m => m.ShowGridView);
        }

        public void OnMoveTo(TaskCategoryEnum taskCategoryEnum)
        {
            // Set the TaskCategoryID to that of the item that was selected from the RadContextMenu
            SelectedTask.TaskCategoryID = Convert.ToInt32(taskCategoryEnum);
            DataContext.SubmitChanges(MoveToCompleted, null);
        }

        public virtual void MoveToCompleted(SubmitOperation so)
        {
            // Only show the tasks for the selected category
            Tasks = Tasks.Where(t => t.TaskCategoryID == SelectedTaskCategory.ID);

            // Inform the UI that the Tasks collection and HasTasks property has changed
            this.OnPropertyChanged(m => m.Tasks);
            this.OnPropertyChanged(m => m.ShowNoTasksMessage);
            this.OnPropertyChanged(m => m.ShowGridView);
        }

        # region Commands

        private RelayCommand<TaskCategoryEnum> _moveToCommand;
        public RelayCommand<TaskCategoryEnum> MoveToCommand
        {
            get { return _moveToCommand ?? (_moveToCommand = new RelayCommand<TaskCategoryEnum>(OnMoveTo)); }
        }

        # endregion

        # region Properties

        public DataContext DataContext { get; set; }

        public IEnumerable<TaskCategory> TaskCategories { get; private set; }

        public TaskCategory SelectedTaskCategory
        {
            get { return _selectedTaskCategory; }
            set
            {
                _selectedTaskCategory = value; 
                this.OnPropertyChanged(m => m.SelectedTaskCategory);
                this.OnPropertyChanged(m => m.ShowSelectMessage);
                this.OnPropertyChanged(m => m.IsMoveToInboxCommandAvailable);
                this.OnPropertyChanged(m => m.IsMoveToActionsCommandAvailable);
                this.OnPropertyChanged(m => m.IsMoveToWaitingCommandAvailable);
                this.OnPropertyChanged(m => m.IsMoveToSomedayCommandAvailable);
                this.OnPropertyChanged(m => m.IsMoveToTicklersCommandAvailable);
                this.OnPropertyChanged(m => m.IsMoveToReferenceCommandAvailable);                
                LoadTasks();
            }
        }

        public IEnumerable<Task> Tasks { get; private set; }

        public Task SelectedTask
        {
            get { return _selectedTask; }
            set
            {
                _selectedTask = value;                
                this.OnPropertyChanged(m => m.SelectedTask);
            }
        }

        public bool ShowSelectMessage
        {
            get { return _selectedTaskCategory == null; }
        }

        public bool ShowNoTasksMessage
        {
            get { return !ShowSelectMessage && !ShowGridView; }
        }

        public bool ShowGridView
        {
            get { return Tasks != null && Tasks.Count() > 0; }
        }

        # endregion

        # region bools that determine whether or not context menu items should be visible

        public bool IsMoveToInboxCommandAvailable
        {
            get { return SelectedTaskCategory.ID != 1; }
        }

        public bool IsMoveToActionsCommandAvailable
        {
            get { return SelectedTaskCategory.ID != 2; }
        }

        public bool IsMoveToWaitingCommandAvailable
        {
            get { return SelectedTaskCategory.ID != 3; }
        }

        public bool IsMoveToSomedayCommandAvailable
        {
            get { return SelectedTaskCategory.ID != 4; }
        }

        public bool IsMoveToTicklersCommandAvailable
        {
            get { return SelectedTaskCategory.ID != 5; }
        }

        public bool IsMoveToReferenceCommandAvailable
        {
            get { return SelectedTaskCategory.ID != 6; }
        }

        # endregion

        # region TaskCategoryEnums

        public TaskCategoryEnum TaskCategoryInbox
        {
            get { return TaskCategoryEnum.Inbox; }
        }

        public TaskCategoryEnum TaskCategoryActions
        {
            get { return TaskCategoryEnum.Actions; }
        }

        public TaskCategoryEnum TaskCategoryWaiting
        {
            get { return TaskCategoryEnum.Waiting; }
        }

        public TaskCategoryEnum TaskCategorySomeday
        {
            get { return TaskCategoryEnum.Someday; }
        }

        public TaskCategoryEnum TaskCategoryTicklers
        {
            get { return TaskCategoryEnum.Ticklers; }
        }

        public TaskCategoryEnum TaskCategoryReference
        {
            get { return TaskCategoryEnum.Reference; }
        }

        # endregion

    }
}
