using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.PPC.Common;
using Tango.PPC.Common.Navigation;
using SimpleValidator.Extensions;
using System.ComponentModel.DataAnnotations;
using Tango.SharedUI.Helpers;
using Tango.PPC.Common.Authentication;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Logging;
using Tango.PPC.UI.Views;
using System.Diagnostics;
namespace Tango.PPC.UI.ViewModels
{
///
/// Represents the PPC login view ViewModel.
///
///
public class LoginViewVM : PPCViewModel
{
public RelayCommand LoginCommand { get; set; }
#region Properties
private String _email;
///
/// Gets or sets the email.
///
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Please enter a valid email address")]
public String Email
{
get { return _email; }
set { _email = value; RaisePropertyChangedAuto(); }
}
private String _password;
///
/// Gets or sets the password.
///
[Required(ErrorMessage = "Password is required")]
public String Password
{
get { return _password; }
set { _password = value; RaisePropertyChangedAuto(); }
}
private bool _isLoading;
///
/// Gets or sets a value indicating whether the application is busy with loading modules.
///
public bool IsLoading
{
get { return _isLoading; }
set { _isLoading = value; RaisePropertyChangedAuto(); }
}
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
public LoginViewVM()
{
LoginCommand = new RelayCommand(Login);
Email = "roy@twine-s.com";
Password = "1234";
}
#endregion
#region Override Methods
///
/// Called when the application has been started.
///
public override void OnApplicationStarted()
{
AuthenticationProvider.CurrentUserChanged += AuthenticationProvider_CurrentUserChanged;
}
public override async void OnApplicationReady()
{
base.OnApplicationReady();
await Task.Delay(500);
if (!AuthenticationProvider.AuthenticationRequired)
{
LogManager.Log("Application is ready! Navigating to home module...");
if (BuildProvider.IsEureka && !Debugger.IsAttached)
{
var secondsPassed = (DateTime.Now - LoadingView.VideoStartTime).TotalSeconds;
var secondsToHold = Math.Max(10 - secondsPassed, 0);
await Task.Delay(TimeSpan.FromSeconds(secondsToHold));
}
await NavigationManager.NavigateTo(NavigationView.HomeModule);
IsLoading = false;
}
else if (AuthenticationProvider.CurrentUser != null && AuthenticationProvider.CurrentUser.HasPermission(Permissions.RunPPC))
{
LogManager.Log("Application is ready! Navigating to home module...");
await NavigationManager.NavigateTo(NavigationView.HomeModule);
IsLoading = false;
}
else
{
LogManager.Log("Application is ready! The logged in user does not have permission to run the application!", LogCategory.Warning);
await NavigationManager.NavigateTo(NavigationView.NoPermissionsView);
IsLoading = false;
}
}
#endregion
#region Event Handlers
///
/// Handles the event.
///
/// The sender.
/// The user.
private void AuthenticationProvider_CurrentUserChanged(object sender, User user)
{
if (user == null)
{
Password = String.Empty;
}
}
#endregion
#region Private Methods
///
/// Login to the application using the user name and password.
///
private async void Login()
{
LogManager.Log("Login command pressed.");
if (Validate())
{
IsLoading = true;
UIHelper.DoEvents();
var user = await AuthenticationProvider.Login(Email, Password);
if (user == null)
{
IsLoading = false;
await NotificationProvider.ShowWarning("Email or password are incorrect.");
}
}
else
{
LogManager.Log("Invalid user credentials.");
}
}
#endregion
}
}