using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.Integration.Operation;
using Tango.PPC.Common;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Jobs.AppBarItems;
using Tango.PPC.Jobs.AppButtons;
using Tango.PPC.Jobs.Dialogs;
using Tango.PPC.Jobs.NavigationObjects;
using Tango.PPC.Jobs.Views;
namespace Tango.PPC.Jobs.ViewModels
{
///
/// Represents the job progress view model.
///
///
public class JobProgressViewVM : PPCViewModel
{
private StopPrintingButton _stop_job_btn;
private JobHandler _handler;
#region Properties
private Job _job;
///
/// Gets or sets the job.
///
public Job Job
{
get { return _job; }
set { _job = value; RaisePropertyChangedAuto(); }
}
private RunningJobStatus _runningJobStatus;
///
/// Gets or sets the running job status.
///
public RunningJobStatus RunningJobStatus
{
get { return _runningJobStatus; }
set { _runningJobStatus = value; RaisePropertyChangedAuto(); }
}
#endregion
#region Commands
///
/// Gets or sets the go to job command.
///
///
/// The go to job command.
///
public RelayCommand GoToJobCommand { get; set; }
#endregion
public JobProgressViewVM()
{
_stop_job_btn = new StopPrintingButton();
_stop_job_btn.Pressed += _stop_job_btn_Pressed;
GoToJobCommand = new RelayCommand(GoToJob);
}
private void _stop_job_btn_Pressed()
{
if (_handler != null)
{
_handler.Cancel();
}
}
private void GoToJob()
{
NavigationManager.NavigateWithObject(new JobNavigationObject() { Job = _handler.Job });
NavigationManager.ClearHistoryExcept();
}
#region Override Methods
///
/// Called when the application has been started.
///
public override void OnApplicationStarted()
{
MachineProvider.MachineOperator.PrintingStarted += MachineOperator_PrintingStarted;
}
///
/// Called when the navigation system has navigated from this VM view.
///
public override void OnNavigatedFrom()
{
base.OnNavigatedFrom();
if (MachineProvider.MachineOperator.IsPrinting && _handler != null && !_handler.IsCanceled)
{
NotificationProvider.PushAppBarItem().Pressed += (_, __) =>
{
NotificationProvider.CurrentAppBarItem.Close();
NavigationManager.NavigateTo(nameof(JobProgressView));
};
}
}
///
/// Called when the navigation system has navigated to this VM view.
///
public override void OnNavigatedTo()
{
base.OnNavigatedTo();
if (NotificationProvider.HasAppBarItem && NotificationProvider.CurrentAppBarItem is JobProgressAppBarItem)
{
NotificationProvider.CurrentAppBarItem.Close();
}
_stop_job_btn.Push();
}
#endregion
#region Event Handlers
///
/// Handles the PrintingStarted event of the MachineOperator.
///
/// The source of the event.
/// The instance containing the event data.
private void MachineOperator_PrintingStarted(object sender, PrintingEventArgs e)
{
_handler = e.JobHandler;
Job = e.Job;
e.JobHandler.StatusChanged += JobHandler_StatusChanged;
e.JobHandler.SpoolChangeRequired += JobHandler_SpoolChangeRequired;
e.JobHandler.Stopped += JobHandler_Stopped;
_stop_job_btn.Push();
}
///
/// Handles the SpoolChangeRequired event of the JobHandler.
///
/// The source of the event.
/// The instance containing the event data.
private void JobHandler_SpoolChangeRequired(object sender, SpoolChangeRequiredEventArgs e)
{
InvokeUI(async () =>
{
if ((await NotificationProvider.ShowDialog(new SpoolChangeViewVM(e))).DialogResult)
{
e.Confirm();
}
else
{
e.Abort();
}
});
}
///
/// Handles the Stopped event of the JobHandler.
///
/// The source of the event.
/// The instance containing the event data.
private void JobHandler_Stopped(object sender, EventArgs e)
{
if (NotificationProvider.HasAppBarItem && NotificationProvider.CurrentAppBarItem is JobProgressAppBarItem)
{
NotificationProvider.CurrentAppBarItem.Close();
}
_stop_job_btn.Pop();
}
///
/// Handles the JobHandler StatusChanged event.
///
/// The sender.
/// The e.
private void JobHandler_StatusChanged(object sender, RunningJobStatus e)
{
InvokeUI(() =>
{
RunningJobStatus = e;
});
}
#endregion
}
}