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

namespace TaskIt.Modules.Settings.ViewModels
{
    [Export]
    public class SettingsPageViewModel : TaskItViewModelBase
    {
        private string _twitterUsername;
        private string _twitterPassword;
        private string _facebookUsername;
        private string _facebookPassword;

        public SettingsPageViewModel()
        {
            CurrentUser = Utils.CurrentUser;
            TwitterUsername = CurrentUser.TwitterUsername;
            TwitterPassword = CurrentUser.TwitterPassword;
            FacebookUsername = CurrentUser.FacebookUsername;
            FacebookPassword = CurrentUser.FacebookPassword;

            Messenger.Default.Register<SaveEntityArgs>(this, OnSaveItem);
        }

        protected void OnSaveItem(SaveEntityArgs args)
        {
            if (args.EntityType != EntityType.TaskItUser)
            {
                return;
            }
            CurrentUser.TwitterUsername = TwitterUsername;
            CurrentUser.TwitterPassword = TwitterPassword;
            CurrentUser.FacebookUsername = FacebookUsername;
            CurrentUser.FacebookPassword = FacebookPassword;
        }

        public override void OnSubmitCompleted(SubmitOperation so)
        {
            Utils.HideBusyIndicator();
            if (so.HasError)
            {
                Utils.ShowErrorMessage(ErrorStrings.Message_SubmitFailed, so.Error.Message);
                so.MarkErrorAsHandled();
                return;
            }

            Utils.DataContext.RejectChanges();
        }

        # region Properties

        public string TwitterUsername
        {
            get { return _twitterUsername; }
            set { _twitterUsername = value; this.OnPropertyChanged(p => p.TwitterUsername); }
        }

        public string TwitterPassword
        {
            get { return _twitterPassword; }
            set { _twitterPassword = value; this.OnPropertyChanged(p => p.TwitterPassword); }
        }

        public string FacebookUsername
        {
            get { return _facebookUsername; }
            set { _facebookUsername = value; this.OnPropertyChanged(p => p.FacebookUsername); }
        }

        public string FacebookPassword
        {
            get { return _facebookPassword; }
            set { _facebookPassword = value; this.OnPropertyChanged(p => p.FacebookPassword); }
        }

        public TaskItUser CurrentUser { get; set; }

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

        # endregion

        # region Icons

        public Uri IconTwitterLogoUri
        {
            get { return GetIconUri("TwitterLogo"); }
        }

        public Uri IconFacebookLogoUri
        {
            get { return GetIconUri("FacebookLogo"); }
        }

        # endregion

        # region Resource Strings

        public string ResourceHeaderTwitter
        {
            get { return SettingsStrings.Header_Twitter; }
        }

        public string ResourceHeaderFacebook
        {
            get { return SettingsStrings.Header_Facebook; }
        }

        # endregion

    }
}
