using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.BL;
using Tango.Core;
using Tango.Core.DI;
using Tango.Core.Helpers;
using Tango.PPC.Common;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Authentication;
using Tango.PPC.Common.MachineSetup;
using Tango.PPC.Common.Modules;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Common.Notifications;
using Tango.PPC.Common.Notifications.NotificationItems;
using Tango.PPC.Jobs;
using Tango.SharedUI;
using System.Data.Entity;
namespace Tango.PPC.UI.ViewModels
{
///
/// Represents the PPC loading splash screen view model.
///
///
public class LoadingViewVM : PPCViewModel
{
///
/// Gets or sets the module loader.
///
[TangoInject]
public IPPCModuleLoader ModuleLoader { get; set; }
private bool _isLoading;
///
/// Gets or sets a value indicating whether this instance is loading.
///
public bool IsLoading
{
get { return _isLoading; }
set { _isLoading = value; RaisePropertyChangedAuto(); }
}
///
/// Initializes a new instance of the class.
///
public LoadingViewVM(IPPCApplicationManager applicationManager)
{
if (!DesignMode)
{
Task.Delay(1000).ContinueWith((x) => { IsLoading = true; });
}
applicationManager.ApplicationInitializationError += ApplicationManager_ApplicationInitializationError;
}
private void ApplicationManager_ApplicationInitializationError(object sender, Exception ex)
{
InvokeUI(() =>
{
NavigationManager.NavigateWithObject(NavigationView.LoadingErrorView, ex);
IsLoading = false;
});
}
///
/// Called when the application has been started.
///
public async override void OnApplicationStarted()
{
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
var machine = await db.Machines.FirstAsync();
if (db.Users.Count() == 1 || machine.AutoLogin)
{
var user = await db.Users.FirstAsync();
LogManager.Log($"Application started. Single user/Auto login detected ({user.Email}). Skipping LoginView...");
await AuthenticationProvider.Login(user.Email, user.Password, false);
IsLoading = false;
}
else
{
LogManager.Log("Application started. Navigating to LoginView...");
await NavigationManager.NavigateTo(NavigationView.LoginView);
IsLoading = false;
}
}
}
}
}