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.Help.Assets.Resources;
using TaskIt.Web;

namespace TaskIt.Modules.Help.ViewModels
{
    [Export]
    public class WebinarsViewModel : TasksWidgetViewModelBase
    {
        private Task _selectedItem;

        public WebinarsViewModel()
        {
            LoadData();
            Messenger.Default.Register<ModuleEnum>(this, OnPageEnter);
        }

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

        private void LoadData()
        {
            ShowBusyIndicator();
            DataProvider.GetCompletedWebinars(OnWebinarsLoaded);
        }

        void OnWebinarsLoaded(LoadOperation<Task> lo)
        {
            if (lo.HasError)
            {
                Utils.ShowErrorMessage(ErrorStrings.Message_LoadingWebinarsFailed, lo.Error.Message); 
                lo.MarkErrorAsHandled();
                return;
            }

            Webinars = lo.Entities;
            this.OnPropertyChanged(m => m.Webinars);
            HideBusyIndicator();
        }

        # region Properties

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

        public Task SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; SourceUrl = _selectedItem.Description; }
        }

        # endregion

    }
}