using System.ComponentModel.Composition;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;
using TaskIt.Core;
using TaskIt.Infrastructure;
using TaskIt.ViewModels;
using TaskIt.Web;

namespace TaskIt.Modules.Tasks.ViewModels
{
    [Export]
    public class TasksPageViewModel : TaskItViewModelBase
    {
        private int _dataSetsLoaded;

        public TasksPageViewModel()
        {
            LoadData();            
            Messenger.Default.Register<NewEntityArgs>(this, OnNewItem);
            Messenger.Default.Register<EditEntityArgs>(this, OnEditItem);
            Messenger.Default.Register<NavigateToModuleArgs>(this, OnNavigateToModule);
        }        

        private void LoadData()
        {
            var currentUserID = Utils.CurrentUserID;
            DataProvider.GetTaskCategories(OnTaskCategoriesLoaded);
            DataProvider.GetPriorities(OnPrioritiesLoaded);
            DataProvider.GetProjects(currentUserID, OnProjectsLoaded);
            DataProvider.GetContexts(currentUserID, OnContextsLoaded);
            DataProvider.GetContacts(currentUserID, OnContactsLoaded);
        }

        public void OnNavigateToModule(NavigateToModuleArgs args)
        {
            if (args.Module != ModuleEnum.Tasks)
            {
                return;
            }
            // TODO: Ross - This should show when I have page to page linking working
            MessageBox.Show("You have arrived");
        }

        private void OnNewItem(NewEntityArgs args)
        {
            Mode = ItemMode.Add;
            ShowForm(args.EntityType);
        }

        private void OnEditItem(EditEntityArgs args)
        {
            Mode = ItemMode.Edit;
            ShowForm(args.EntityType);
        }

        public void ShowForm(EntityType entityType)
        {
            switch (entityType)
            {
                case EntityType.Project:
                    NotifyViewProjectForm();
                    break;
                case EntityType.Context:
                    NotifyViewContextForm();
                    break;
                case EntityType.Contact:
                    NotifyViewContactForm();
                    break;
            }
        }

        void OnTaskCategoriesLoaded(LoadOperation<TaskCategory> loadOperation)
        {
            UpdateDataLoadCount();
        }

        void OnProjectsLoaded(LoadOperation<Project> loadOperation)
        {
            UpdateDataLoadCount();
        }

        void OnContextsLoaded(LoadOperation<Context> loadOperation)
        {
            UpdateDataLoadCount();
        }

        void OnContactsLoaded(LoadOperation<Contact> loadOperation)
        {
            UpdateDataLoadCount();
        }

        void OnPrioritiesLoaded(LoadOperation<Priority> loadOperation)
        {
            UpdateDataLoadCount();
        }

        private void UpdateDataLoadCount()
        {
            _dataSetsLoaded++;
            CheckDataLoadCount();
        }

        private void CheckDataLoadCount()
        {
            if (_dataSetsLoaded == 5)
            {
                Messenger.Default.Send(new DataLoadedArgs());
            }
        }

        # region UI Events

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

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

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

# endregion

    }
}
