using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Builders;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Integration.Operation;
using Tango.PPC.Common;
using Tango.PPC.Common.Notifications;
using Tango.PPC.Common.Notifications.NotificationItems;
using Tango.PPC.Jobs.Messages;
using Tango.PPC.Jobs.NavigationObjects;
using Tango.PPC.Jobs.Views;
namespace Tango.PPC.Jobs.ViewModels
{
///
/// Represents the main view ViewModel.
///
///
public class MainViewVM : PPCViewModel
{
private NotificationItem _last_failed_job_notification;
///
/// Called when the application has been started.
///
public override void OnApplicationStarted()
{
MachineProvider.MachineOperator.PrintingCompleted += MachineOperator_PrintingCompleted;
MachineProvider.MachineOperator.PrintingFailed += MachineOperator_PrintingFailed;
MachineProvider.MachineOperator.ResumingJob += MachineOperator_ResumingJob;
}
private async void MachineOperator_ResumingJob(object sender, ResumingJobEventArgs e)
{
LogManager.Log($"Trying to resume job '{e.JobGuid}'...");
try
{
var job = await new JobBuilder(ObservablesContext.CreateDefault()).Set(e.JobGuid)
.WithConfiguration()
.WithRML()
.WithUser()
.WithSegments()
.WithBrushStops()
.BuildAsync();
e.Approve(job);
InvokeUI(() =>
{
NavigationManager.NavigateTo(nameof(JobProgressView));
if (_last_failed_job_notification != null)
{
_last_failed_job_notification.Close();
_last_failed_job_notification = null;
}
});
}
catch (Exception ex)
{
LogManager.Log(ex, "An error occurred while trying to resume the job.");
InvokeUI(() =>
{
NotificationProvider.ShowError("An error occurred while trying to resume a job in progress.");
});
}
}
///
/// Handles the PrintingFailed event of the MachineOperator.
///
/// The source of the event.
/// The instance containing the event data.
private void MachineOperator_PrintingFailed(object sender, PrintingFailedEventArgs e)
{
_last_failed_job_notification = NotificationProvider.PushNotification(new MessageNotificationItem(
String.Format("'{0}' failed.", e.Job.Name),
String.Format("The job '{1}' has failed due to unexpected error.{0}{2}{0}{0}Tap to view this job details.", Environment.NewLine, e.Job.Name, e.Exception), MessageNotificationItem.MessageNotificationItemTypes.Error, () =>
{
NavigationManager.NavigateWithObject(e.Job);
NavigationManager.ClearHistoryExcept();
}));
}
///
/// Handles the PrintingCompleted event of the MachineOperator.
///
/// The source of the event.
/// The instance containing the event data.
private void MachineOperator_PrintingCompleted(object sender, PrintingEventArgs e)
{
LogManager.Log($"'{e.Job.Name}' printing complete. Job designation is {e.Job.Designation}.");
if (e.Job.Designation == BL.Enumerations.JobDesignations.SampleDye)
{
NotificationProvider.PushNotification(new MessageNotificationItem(String.Format("'{0}' sample dye completed successfully", e.Job.Name), "Tap to approve or repeat.", MessageNotificationItem.MessageNotificationItemTypes.Success, () =>
{
NavigationManager.NavigateWithObject(new JobNavigationObject() { Job = e.Job, Intent = JobNavigationIntent.SampleDye });
NavigationManager.ClearHistoryExcept();
}));
}
else if (e.Job.Designation == BL.Enumerations.JobDesignations.FineTuning)
{
NotificationProvider.PushNotification(new MessageNotificationItem(String.Format("'{0}' fine tuning completed successfully", e.Job.Name), "Tap to approve or repeat.", MessageNotificationItem.MessageNotificationItemTypes.Success, () =>
{
NavigationManager.NavigateWithObject(new JobNavigationObject() { Job = e.Job, Intent = JobNavigationIntent.FineTuning });
NavigationManager.ClearHistoryExcept();
}));
}
else
{
NotificationProvider.PushNotification(new MessageNotificationItem(String.Format("'{0}' completed successfully", e.Job.Name), "Tap to view this job details.", MessageNotificationItem.MessageNotificationItemTypes.Success, () =>
{
NavigationManager.NavigateWithObject(new JobNavigationObject() { Job = e.Job });
NavigationManager.ClearHistoryExcept();
}));
}
}
public override void OnNavigatedTo()
{
base.OnNavigatedTo();
RaiseMessage();
}
}
}