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.Logging;
using Tango.PMR.Printing;
using Tango.PPC.Common;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Common.Notifications;
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(); }
}
private bool _isDisplayJobOutline;
///
/// Gets or sets a value indicating whether to display the job outline.
///
public bool IsDisplayJobOutline
{
get { return _isDisplayJobOutline; }
set { _isDisplayJobOutline = value; RaisePropertyChangedAuto(); }
}
private JobTicket _jobOutlineTicket;
///
/// Gets or sets the job outline ticket.
///
public JobTicket JobOutlineTicket
{
get { return _jobOutlineTicket; }
set { _jobOutlineTicket = value; RaisePropertyChangedAuto(); }
}
#endregion
#region Commands
///
/// Gets or sets the go to job command.
///
///
/// The go to job command.
///
public RelayCommand GoToJobCommand { get; set; }
///
/// Gets or sets the display job outline command.
///
public RelayCommand DisplayJobOutlineCommand { get; set; }
///
/// Gets or sets the hide job outline command.
///
public RelayCommand HideJobOutlineCommand { get; set; }
#endregion
public JobProgressViewVM()
{
_stop_job_btn = new StopPrintingButton();
_stop_job_btn.Pressed += _stop_job_btn_Pressed;
GoToJobCommand = new RelayCommand(GoToJob);
DisplayJobOutlineCommand = new RelayCommand(DisplayJobOutline);
HideJobOutlineCommand = new RelayCommand(HideJobOutline);
}
#region Private Methods
private void HideJobOutline()
{
IsDisplayJobOutline = false;
}
private void DisplayJobOutline()
{
JobOutlineTicket = _handler.JobTicket;
IsDisplayJobOutline = true;
}
private void GoToJob()
{
NavigationManager.NavigateWithObject(new JobNavigationObject() { Job = _handler.Job });
NavigationManager.ClearHistoryExcept();
}
#endregion
#region Override Methods
///
/// Called when the application has been started.
///
public override void OnApplicationStarted()
{
MachineProvider.MachineOperator.PrintingStarted += MachineOperator_PrintingStarted;
MachineProvider.MachineOperator.PrintingEnded += MachineOperator_PrintingEnded;
}
///
/// Called when the navigation system has navigated to this VM view.
///
public override void OnNavigatedTo()
{
base.OnNavigatedTo();
IsDisplayJobOutline = false;
if (_handler != null && !_handler.Status.IsFailed)
{
_stop_job_btn.Push();
}
}
#endregion
#region Event Handlers
private void _stop_job_btn_Pressed()
{
if (_handler != null)
{
_handler.Cancel();
}
}
///
/// 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;
e.JobHandler.CanCancelChanged += JobHandler_CanCancelChanged;
_stop_job_btn.Push();
_stop_job_btn.IsEnabled = true;
}
private void MachineOperator_PrintingEnded(object sender, PrintingEventArgs e)
{
LogManager.Log("Printing ended, popping job stop button...");
if (_stop_job_btn != null)
{
_stop_job_btn.Pop();
}
else
{
LogManager.Log("Job stop button instance was null!", LogCategory.Warning);
}
}
///
/// 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 (_handler != null)
{
_handler.StatusChanged -= JobHandler_StatusChanged;
_handler.SpoolChangeRequired -= JobHandler_SpoolChangeRequired;
_handler.Stopped -= JobHandler_Stopped;
_handler.CanCancelChanged -= JobHandler_CanCancelChanged;
}
}
///
/// Handles the JobHandler StatusChanged event.
///
/// The sender.
/// The e.
private void JobHandler_StatusChanged(object sender, RunningJobStatus e)
{
InvokeUI(() =>
{
RunningJobStatus = e;
});
}
///
/// Handles the CanCancelChanged event of the JobHandler control.
///
/// The source of the event.
/// The instance containing the event data.
private void JobHandler_CanCancelChanged(object sender, EventArgs e)
{
_stop_job_btn.IsEnabled = _handler.CanCancel;
}
#endregion
}
}