using Client.Helpers;
using Client.View;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WpfAsyncPack.Command;

namespace Client.ViewModel
{
    public class LoginViewModel : ViewModelBase
    {

        public string UserName { get; set; }
        public string Password { get; set; }
        public ICommand LoginCommand { get; set; } 
        public LoginViewModel()
        {
            LoginCommand = new AsyncCommand<IClosable>(LoginAsync);
        }


        private async Task LoginAsync(IClosable window)
        {
            var userService = new ISServiceRef.UserServiceClient();

            userService.ClientCredentials.UserName.UserName = UserName;
            userService.ClientCredentials.UserName.Password = Password;
            userService.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
            try
            {
                var user = await userService.LoginAsync(UserName);
                user.Password = Password;
                UserManager.Instance.CurrentUser = user;
                var mainWindow = new MainWindow();
                window.Close();
                mainWindow.Show();
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.LoginFailed, string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }
    }
}
