using System.Windows.Input;
using Titano.Core.Localization;
using Titano.Ui.Mvvm;
using Titano.Uploader.Text;
namespace Titano.Uploader.Shell;
/// An entry of a watch folder chip's menu: an interval choice, a separator (no text) or "Remove".
public sealed record WatchMenuOption(string? Text, string? ToolTip, ICommand? Command, bool IsChecked)
{
public bool IsSeparator => Text is null;
}
/// One watch folder chip: path, interval, reachability.
public sealed class WatchFolderViewModel : ObservableObject
{
/// Intervals offered in the chip menu.
public static readonly int[] IntervalChoices = [30, 60, 300, 900];
private readonly Action _setInterval;
private readonly Action _remove;
private int _intervalSeconds;
private bool? _isAvailable;
public WatchFolderViewModel(string path, int intervalSeconds, bool isNetwork, Action setInterval, Action remove)
{
Path = path;
IsNetwork = isNetwork;
_intervalSeconds = intervalSeconds;
_setInterval = setInterval;
_remove = remove;
}
public string Path { get; }
public bool IsNetwork { get; }
public int IntervalSeconds
{
get => _intervalSeconds;
set
{
if (!Set(ref _intervalSeconds, value)) return;
OnPropertyChanged(nameof(Text));
OnPropertyChanged(nameof(IntervalText));
OnPropertyChanged(nameof(ToolTip));
OnPropertyChanged(nameof(MenuOptions));
}
}
/// Null until the first check finished.
public bool? IsAvailable
{
get => _isAvailable;
set
{
if (Set(ref _isAvailable, value)) OnPropertyChanged(nameof(ToolTip));
}
}
/// "D:\Cam\Roof · every 1 min": the chip's accessible name.
public string Text => Loc.L("Uploader.Watch.Chip", Path, Interval(IntervalSeconds));
/// "every 1 min", shown after the path, which the chip draws left to right on its own and may shorten.
public string IntervalText => Loc.L("Uploader.Watch.Interval", Interval(IntervalSeconds));
/// The full path (the chip may shorten it), then the folder's state and interval.
public string ToolTip => LeftToRight(Path) + Loc.Separator + (IsAvailable == false ? Loc.L("Tooltips.Uploader.WatchFolderMissing", Interval(IntervalSeconds))
: IsNetwork ? Loc.L("Tooltips.Uploader.WatchFolderNetwork", Interval(IntervalSeconds))
: Loc.L("Tooltips.Uploader.WatchFolder", Interval(IntervalSeconds)));
public IReadOnlyList MenuOptions =>
[
.. IntervalChoices.Select(s => new WatchMenuOption(
Loc.L("Uploader.Watch.Every", Interval(s)),
Loc.L("Tooltips.Uploader.WatchInterval"),
new RelayCommand(() => _setInterval(this, s)),
s == IntervalSeconds)),
new WatchMenuOption(null, null, null, false),
new WatchMenuOption(Loc.L("Uploader.Watch.Remove"), Loc.L("Tooltips.Uploader.RemoveWatchFolder"), new RelayCommand(() => _remove(this)), false),
];
private static string Interval(int seconds) => UploaderText.Duration(TimeSpan.FromSeconds(seconds), Loc.Culture);
/// Embeds a path left to right (LRE … PDF) so a right-to-left tooltip does not reorder its separators.
private static string LeftToRight(string text) => "\u202A" + text + "\u202C";
}