using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight.Messaging;
using TaskIt.Core;
using TaskIt.Infrastructure;
using TaskIt.Modules.Dashboard.Assets.Resources;
using TaskIt.ViewModels;
using TaskIt.Web;

namespace TaskIt.Modules.Dashboard.ViewModels
{
    [Export]
    public class OverdueTasksViewModel : WidgetViewModelBase
    {
        public OverdueTasksViewModel()
        {
            LoadData();
            Messenger.Default.Register<ModuleEnum>(this, OnPageEnter);
        }

        private void OnPageEnter(ModuleEnum args)
        {
            if (args == ModuleEnum.Dashboard)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            ShowBusyIndicator();
            DataProvider.GetOverdueTasks(Utils.CurrentUserID, OnDataLoaded);            
        }

        void OnDataLoaded(LoadOperation<Task> lo)
        {
            if (lo.HasError)
            {
                Utils.ShowErrorMessage(DashboardErrorStrings.Message_LoadingTasksOverviewFailed, lo.Error.Message);               
                lo.MarkErrorAsHandled();
                return;
            }

            OverdueTasks = lo.Entities;
            this.OnPropertyChanged(p => p.OverdueTasks);
            HideBusyIndicator();
        }

        # region Properties

        public IEnumerable<Task> OverdueTasks { get; set; }

        # endregion

    }
}
