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.ColorConversion;
using Tango.Core.Commands;
using Tango.Integration.Operation;
using Tango.PPC.Common;
using Tango.PPC.Common.Messages;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Jobs.Messages;
using Tango.PPC.Jobs.NavigationObjects;
using Tango.PPC.Jobs.Views;
namespace Tango.PPC.Jobs.ViewModels
{
///
/// Represents the job summary view model.
///
///
public class JobSummeryViewVM : PPCViewModel, INavigationObjectReceiver
{
private ObservablesContext _context;
private bool _canStartJob;
private bool _startingJob;
private bool _isPreparingJob;
///
/// Gets or sets a value indicating whether the job is currently preparing.
///
public bool IsPreparingJob
{
get { return _isPreparingJob; }
set { _isPreparingJob = value; RaisePropertyChangedAuto(); }
}
private Job _job;
///
/// Gets or sets the job.
///
public Job Job
{
get { return _job; }
set { _job = value; RaisePropertyChangedAuto(); }
}
private TimeSpan _estimatedDuration;
///
/// Gets or sets the duration of the estimated.
///
public TimeSpan EstimatedDuration
{
get { return _estimatedDuration; }
set { _estimatedDuration = value; RaisePropertyChangedAuto(); }
}
private int _btsrSpoolTension;
public int BtsrSpoolTension
{
get { return _btsrSpoolTension; }
set { _btsrSpoolTension = value; RaisePropertyChangedAuto(); }
}
///
/// Gets or sets the dye command.
///
public RelayCommand DyeCommand { get; set; }
///
/// Gets or sets the edit command.
///
public RelayCommand EditCommand { get; set; }
///
/// Initializes a new instance of the class.
///
public JobSummeryViewVM()
{
DyeCommand = new RelayCommand(StartJob, () =>
{
return _canStartJob;
});
EditCommand = new RelayCommand(EditJob);
}
///
/// Edits the job.
///
private void EditJob()
{
LogManager.Log("Edit command pressed.");
NavigationManager.NavigateTo(false, nameof(JobView));
}
///
/// Starts the job.
///
private async void StartJob()
{
if (_startingJob) return;
_startingJob = true;
_canStartJob = false;
InvalidateRelayCommands();
NavigationManager.IsBackEnabled = false;
try
{
LogManager.Log("Start job command pressed. Starting job and navigating to job progress view...");
await PrintingManager.Print(Job, _context);
await NavigationManager.NavigateTo(false, nameof(JobProgressView));
_startingJob = false;
}
catch (InsufficientLiquidQuantityException)
{
//Ignore..
}
catch (OperationCanceledException)
{
//Ignore..
}
catch (Exception ex)
{
LogManager.Log(ex, "Could not start the current job.");
await NotificationProvider.ShowError($"{ex.Message}");
}
finally
{
_startingJob = false;
_canStartJob = true;
NavigationManager.IsBackEnabled = true;
InvalidateRelayCommands();
}
}
///
/// Called when the application has been started.
///
public override void OnApplicationStarted()
{
}
public async void OnNavigatedToWithObject(JobSummeryNavigationObject obj)
{
_canStartJob = false;
InvalidateRelayCommands();
_context = obj.Context;
Job = obj.Job;
IsPreparingJob = true;
Job = await new JobBuilder(_context).Set(Job.Guid)
.WithConfiguration()
.WithRML()
.WithUser()
.WithSegments()
.WithBrushStops()
.BuildAsync();
if (Job.Rml != null && _context != null)
{
BtsrSpoolTension = await Job.Rml.GetRequiredBtsrSpoolTension(_context, Job.SpoolType);
}
await Task.Factory.StartNew(() =>
{
try
{
IColorConverter _converter = new DefaultColorConverter();
EstimatedDuration = Job.GetEstimatedDuration(_converter.GetRecommendedProcessParameters(Job, true));
}
catch (Exception ex)
{
LogManager.Log(ex, "Error calculating recommended process parameters.");
}
});
IsPreparingJob = false;
_canStartJob = true;
InvalidateRelayCommands();
}
}
}