using System.IO;
using System.Windows.Input;
using Chronos.App.Infrastructure;
using Chronos.Core.Localization;
using Chronos.Data.Workspaces;
using Chronos.Ui.Mvvm;
namespace Chronos.App.Settings;
///
/// One home's folder in Settings › Workspace › Home folders (owner review 5, #66): "This PC" for the projects of this PC's
/// archive (local), one row per server for its projects (servers\<id>), with Open.
///
public sealed class HomeFolderRow(string name, string path, string toolTip, Action open)
{
public string Name { get; } = name;
/// The folder as shown (a capture's own folders stand for the real ones).
public string Path { get; } = AppEnvironment.Shown(path);
public string ToolTip { get; } = toolTip;
public ICommand OpenCommand { get; } = new RelayCommand(() => open(path));
/// The rows of the workspace's homes: this PC first, then each server by name; none while the workspace is not made.
public static IReadOnlyList Of(WorkspaceHomes homes, IReadOnlyList known, Action open)
{
ArgumentNullException.ThrowIfNull(homes);
ArgumentNullException.ThrowIfNull(known);
ArgumentNullException.ThrowIfNull(open);
if (!homes.IsSplit) return [new HomeFolderRow(Loc.L("Settings.Workspace.ThisPc"), homes.Root, Loc.L("Tooltips.Settings.Workspace.ThisPc", AppEnvironment.Shown(homes.Root)), open)];
var rows = new List
{
new(Loc.L("Settings.Workspace.ThisPc"), homes.LocalRoot, Loc.L("Tooltips.Settings.Workspace.ThisPc", AppEnvironment.Shown(homes.LocalRoot)), open),
};
foreach (var home in homes.Homes().Where(h => h is { IsLocal: false, IsLegacy: false }).OrderBy(h => NameOf(h, known), StringComparer.CurrentCultureIgnoreCase))
{
var name = NameOf(home, known);
rows.Add(new HomeFolderRow(name, home.Store.Root,
Loc.L("Tooltips.Settings.Workspace.ServerHome", name, AppEnvironment.Shown(home.Store.Root)), open));
}
return rows;
}
/// The server's name as this PC knows it, else as its folder records it, else the folder's name.
private static string NameOf(WorkspaceHome home, IReadOnlyList known) =>
(home.Server?.Id is { } id ? known.FirstOrDefault(k => k.Id == id)?.Name : null)
?? home.Server?.Name ?? home.Server?.Address ?? System.IO.Path.GetFileName(home.Store.Root);
}
///
/// A server this PC signed in to, in Settings › Server › Known servers (owner review 5, #66): its name, last address, id
/// (short, the whole one in the tooltip), whether this PC is signed in to it now, and its folders: the archive of its
/// downloaded originals and its share of the cache, each with Open.
///
public sealed class KnownServerRow
{
private readonly string? _originals;
private readonly string _cache;
public KnownServerRow(KnownServer server, bool signedIn, string? originalsRoot, string cacheFolder, Action open)
{
ArgumentNullException.ThrowIfNull(server);
ArgumentNullException.ThrowIfNull(open);
Server = server;
IsSignedIn = signedIn;
_originals = originalsRoot;
_cache = cacheFolder;
OpenOriginalsCommand = new RelayCommand(() => open(_originals!), () => _originals is not null && Directory.Exists(_originals));
OpenCacheCommand = new RelayCommand(() => open(_cache), () => Directory.Exists(_cache));
}
public KnownServer Server { get; }
public string Name => Server.Name is { Length: > 0 } name ? name : Server.LastAddress ?? Server.Id.ToString("N");
public string Address => Server.LastAddress ?? "—";
/// The first 8 hex digits of the id; the tooltip has it whole.
public string ShortId => Server.Id.ToString("N")[..8];
public string IdToolTip => Loc.L("Tooltips.Settings.Server.ServerId", Server.Id.ToString("D"));
public bool IsSignedIn { get; }
public string StateText => Loc.L(IsSignedIn ? "Settings.Server.SignedIn" : "Settings.Server.Offline");
public string StateToolTip => Loc.L(IsSignedIn ? "Tooltips.Settings.Server.SignedIn" : "Tooltips.Settings.Server.Offline");
public string OriginalsPath => _originals is null ? "—" : AppEnvironment.Shown(_originals);
public string CachePath => AppEnvironment.Shown(_cache);
public ICommand OpenOriginalsCommand { get; }
public ICommand OpenCacheCommand { get; }
}