using System.Windows.Input;
using Chronos.App.Infrastructure;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Jobs;
using Chronos.Protocol;
using Chronos.Ui.Controls;
using Chronos.Ui.Mvvm;
namespace Chronos.App.Jobs;
///
/// One row of Jobs › Scheduled (owner review 2, R10): the on/off switch, name, action, when it runs, the next run and the
/// last result, and its commands (Run now, Edit, then Reset for a system schedule or Delete for one the user created).
///
public sealed class ScheduleRowViewModel : ObservableObject
{
private readonly SchedulesViewModel _owner;
private ScheduleDto _schedule;
private Job? _lastJob;
private long _lastJobStamp;
private bool _isEnabled;
internal ScheduleRowViewModel(ScheduleDto schedule, SchedulesViewModel owner)
{
_schedule = schedule;
_owner = owner;
_isEnabled = schedule.Enabled;
RunNowCommand = new AsyncRelayCommand(() => owner.RunNowAsync(this), () => IsAvailable);
EditCommand = new RelayCommand(() => owner.Edit(this));
DeleteCommand = new AsyncRelayCommand(() => owner.DeleteAsync(this), () => !IsSystem);
ResetCommand = new AsyncRelayCommand(() => owner.ResetAsync(this), () => IsSystem);
}
public ScheduleDto Schedule => _schedule;
public Guid Id => _schedule.Id;
/// A system schedule: one of the host's timers, reset instead of deleted.
public bool IsSystem => _schedule.SystemKey is not null;
/// The job the last run queued, when it is known.
public Job? LastJob => _lastJob;
public string Name => ScheduleText.Name(_schedule);
public string NameToolTip => IsSystem ? Loc.L("Tooltips.Jobs.Schedule.System", Name) : Name;
public string ActionText => ScheduleText.Action(_schedule.Action);
/// What the action does, or why this PC does not run it.
public string ActionToolTip => IsAvailable ? ScheduleText.ActionToolTip(_schedule.Action) : ScheduleText.Unavailable(_owner.UnavailableReason(_schedule.Action));
/// This PC runs the schedule's action.
public bool IsAvailable => _owner.IsAvailable(_schedule.Action);
public string WhenText => ScheduleText.When(_schedule.Recurrence);
/// When it runs, whether a missed run is caught up, and the priority of its jobs.
public string WhenToolTip => string.Join(Loc.Separator, WhenText,
Loc.L(_schedule.CatchUp ? "Tooltips.Jobs.Schedule.CatchUpOn" : "Tooltips.Jobs.Schedule.CatchUpOff"),
Loc.L("Tooltips.Jobs.Schedule.PriorityIs", ScheduleText.Priority(Priority)));
public string NextText => ScheduleText.Next(_schedule.NextRun, _schedule.Enabled);
public string NextToolTip => !_schedule.Enabled ? Loc.L("Tooltips.Jobs.Schedule.NextOff")
: _schedule.NextRun is { } next ? Loc.L("Tooltips.Jobs.Schedule.Next", Formatting.Date(next) + " " + Formatting.TimeOfDay(next))
: Loc.L("Tooltips.Jobs.Schedule.NextPending");
public string LastText => ScheduleText.Outcome(_schedule.LastOutcome, _lastJob);
public string LastToolTip => ScheduleText.OutcomeToolTip(_schedule.LastOutcome, _schedule.LastRun, _lastJob);
public StatusKind LastKind => ScheduleText.OutcomeKind(_schedule.LastOutcome, _lastJob);
/// Off, a schedule never runs by itself (Run now still runs it). Turning it on or off saves at once.
public bool IsEnabled
{
get => _isEnabled;
set
{
if (Set(ref _isEnabled, value)) _ = _owner.SetEnabledAsync(this, value);
}
}
public string EnabledToolTip => Loc.L(_isEnabled ? "Tooltips.Jobs.Schedule.EnabledOn" : "Tooltips.Jobs.Schedule.EnabledOff");
/// "Index backup · Index backup · Daily at 03:00": the row's name for screen readers.
public string AccessibleName => string.Join(Loc.Separator, Name, ActionText, WhenText);
public ICommand RunNowCommand { get; }
public ICommand EditCommand { get; }
public ICommand DeleteCommand { get; }
public ICommand ResetCommand { get; }
public string RunNowToolTip => IsAvailable ? Loc.L("Tooltips.Jobs.Schedule.RunNow") : ScheduleText.Unavailable(_owner.UnavailableReason(_schedule.Action));
/// "Run now: Index backup", what each row button is called for screen readers.
public string RunNowName => Loc.L("Jobs.ActionFor", Loc.L("Jobs.Schedule.RunNow"), Name);
public string EditName => Loc.L("Jobs.ActionFor", Loc.L("Jobs.Schedule.Edit"), Name);
public string DeleteName => Loc.L("Jobs.ActionFor", Loc.L("Jobs.Schedule.Delete"), Name);
public string ResetName => Loc.L("Jobs.ActionFor", Loc.L("Jobs.Schedule.Reset"), Name);
public string EnabledName => Loc.L("Jobs.ActionFor", Loc.L("Jobs.Schedule.Enabled"), Name);
private JobPriority Priority => JobWire.TryParsePriority(_schedule.Priority, out var priority) ? priority : JobPriority.Background;
/// A newer copy of the schedule (an edit, a run, a correction of its next run).
internal void Update(ScheduleDto schedule)
{
if (schedule.LastJobId != _schedule.LastJobId)
{
// A read of the previous job no longer applies.
_lastJob = null;
_lastJobStamp++;
}
_schedule = schedule;
_isEnabled = schedule.Enabled;
OnAllPropertiesChanged();
}
/// The job the last run queued changed (live, from the engine): the last result follows its state.
internal void SetLastJob(Job? job)
{
if (job is not null && job.Id != _schedule.LastJobId) return;
_lastJobStamp++;
ShowLastJob(job);
}
///
/// A read of the last job from the index starts; its result applies only while nothing newer arrived (a live change,
/// another job, a later read): see .
///
internal long BeginLastJobRead() => ++_lastJobStamp;
/// The last job as the read begun at found it; ignored when that read is stale.
internal void SetLoadedLastJob(Job? job, long read)
{
if (read != _lastJobStamp || (job is not null && job.Id != _schedule.LastJobId)) return;
ShowLastJob(job);
}
private void ShowLastJob(Job? job)
{
_lastJob = job;
OnPropertyChanged(nameof(LastJob));
OnPropertyChanged(nameof(LastText));
OnPropertyChanged(nameof(LastToolTip));
OnPropertyChanged(nameof(LastKind));
}
/// The switch goes back to the stored state (the change was refused).
internal void RevertEnabled()
{
_isEnabled = _schedule.Enabled;
OnPropertyChanged(nameof(IsEnabled));
OnPropertyChanged(nameof(EnabledToolTip));
}
internal void Refresh() => OnAllPropertiesChanged();
}
/// A job a schedule queued, in the selected schedule's last runs: kind, target, when, state, and Show in queue.
public sealed class ScheduleRunViewModel : ObservableObject
{
private Job _job;
internal ScheduleRunViewModel(Job job, Func show)
{
_job = job;
ShowCommand = new AsyncRelayCommand(() => show(_job.Id));
}
public Job Job => _job;
public Guid Id => _job.Id;
public string Kind => JobText.Kind(_job.Kind);
public string Target => _job.TargetLabel ?? string.Empty;
/// When the run queued it ("Today 03:00").
public string WhenText => ScheduleText.Moment(_job.Created);
public string StateText => JobText.State(new JobEntry(_job));
public string StateToolTip => string.Join(Loc.Separator, StateText,
_job.Error is { Length: > 0 } error ? error : Loc.L("Tooltips.Jobs.State." + _job.State));
public StatusKind StateKind => _job.State switch
{
JobState.Running => StatusKind.Running,
JobState.Queued => StatusKind.Queued,
JobState.Paused => StatusKind.Paused,
JobState.Done => StatusKind.Done,
JobState.Failed => StatusKind.Failed,
_ => StatusKind.Offline,
};
/// "Verify · Archive · 2024: done": the row for screen readers.
public string AccessibleName => Loc.L("Jobs.Row.State", JobText.Name(_job), StateText);
public string ShowName => Loc.L("Jobs.ActionFor", Loc.L("Jobs.Schedule.ShowJob"), JobText.Name(_job));
public ICommand ShowCommand { get; }
internal void Update(Job job)
{
_job = job;
OnAllPropertiesChanged();
}
internal void Refresh() => OnAllPropertiesChanged();
}