using System;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using OpenNest.Data;
namespace OpenNest.Desktop;
///
/// The per-user settings File > New reads and advances: the local nest counter, the date it
/// last counted from (the counter restarts each year) and the Made By the user last typed.
///
public interface INewNestUserSettings
{
int NestNumber { get; set; }
DateTime LastNestCreatedDate { get; set; }
/// The Made By last typed in Nest Info; blank when none.
string MadeBy { get; }
/// Persists and .
void Save();
}
/// A new nest and the warning to show with it (unreadable nest defaults), or null.
public sealed record NewNest(Nest Nest, string Warning);
///
/// Creates File > New nests: nest defaults, clamps, the default machine and its support grid,
/// then the year's next name (). In Database mode the nest server
/// allocates the number so every PC shares one sequence; when it cannot, the user's
/// is the local fallback, kept above every number
/// the server handed out and reset each year.
///
public sealed class NewNestSource
{
private static readonly TimeSpan ServerTimeout = TimeSpan.FromSeconds(3);
private readonly MachineSettingsSource machines;
private readonly INewNestUserSettings user;
private readonly Func databaseRepository;
private readonly Func now;
///
/// The nest server's repository in Database mode, or null in File mode; read on every new
/// nest. Null itself means File mode always.
///
/// The clock naming the nest; defaults to .
public NewNestSource(
MachineSettingsSource machines,
INewNestUserSettings user,
Func databaseRepository = null,
Func now = null)
{
this.machines = machines ?? throw new ArgumentNullException(nameof(machines));
this.user = user ?? throw new ArgumentNullException(nameof(user));
this.databaseRepository = databaseRepository;
this.now = now ?? (() => DateTime.Now);
}
///
/// The author a new nest starts with: the name the user last typed in Nest Info's Made By,
/// else their user name. Only new nests get it; opening an existing nest's info leaves a
/// blank Made By blank.
///
public string DefaultMadeBy => MadeByOrUserName(user.MadeBy);
/// trimmed, or the user name when it is blank.
public static string MadeByOrUserName(string remembered) =>
string.IsNullOrWhiteSpace(remembered) ? Environment.UserName : remembered.Trim();
///
/// The drawing set release group the nest is for: its units, with the saved plate defaults
/// converted to them, its material and thickness, and the machine's spacing for those. Null
/// uses the defaults as saved.
///
public async Task CreateAsync(SetReleaseGroup release = null)
{
var nest = CreateDefaultNest(release, out var warning);
var created = now();
nest.DateCreated = created;
nest.DateLastModified = created;
nest.MadeBy = DefaultMadeBy;
nest.Name = NestNames.Format(created.Year, await AllocateNumberAsync(created).ConfigureAwait(false));
return new NewNest(nest, warning);
}
private Nest CreateDefaultNest(SetReleaseGroup release, out string warning)
{
var nest = new Nest();
var defaults = machines.LoadNestDefaults(out warning);
(release != null ? defaults.ConvertedTo(release.Units) : defaults).ApplyTo(nest);
// After the defaults so the rail and grid are converted to the new nest's units.
var machine = MachineSettingsSource.DefaultMachine(defaults, machines.LoadMachines());
MachineSettingsSource.LoadClampSettings(machine).ApplyToNewNest(nest);
machines.AssignMachine(nest, machine);
if (release != null)
{
nest.Material = new Material(release.MaterialName, release.MaterialGrade);
nest.Thickness = release.Thickness;
MachineCuttingDefaults.ApplySpacingTo(nest, machine);
}
return nest;
}
private async Task AllocateNumberAsync(DateTime created)
{
if (created.Year != user.LastNestCreatedDate.Year)
user.NestNumber = 1;
user.LastNestCreatedDate = created.Date;
try
{
var repository = databaseRepository?.Invoke();
if (repository != null)
{
using var timeout = new CancellationTokenSource(ServerTimeout);
var number = await repository.AllocateNestNumberAsync(created.Year, timeout.Token).ConfigureAwait(false);
user.NestNumber = System.Math.Max(user.NestNumber, number + 1);
user.Save();
return number;
}
}
catch (Exception ex) when (ex is HttpRequestException or IOException
or OperationCanceledException or NotSupportedException or ArgumentException)
{
// Offline, an invalid server URL or an older server: fall back to this PC's counter.
}
var local = user.NestNumber;
user.NestNumber = local + 1;
user.Save();
return local;
}
}
///
/// kept in the desktop user-settings file
/// () under the WinForms names and text, so both apps count from
/// one sequence: NestNumber as an invariant integer (1 when missing or unreadable),
/// LastNestCreatedDate as System.Configuration's invariant date text, and MadeBy as text. Each
/// read goes to the file and each change is written at once, so has nothing
/// left to do.
///
public sealed class NewNestUserSettingsFile(string path) : INewNestUserSettings
{
public const string NestNumberName = "NestNumber";
public const string LastNestCreatedDateName = "LastNestCreatedDate";
public const string MadeByName = "MadeBy";
/// The WinForms default, which never matches this year so the counter restarts.
private static readonly DateTime DefaultDate = new(2015, 4, 8, 19, 30, 0);
public int NestNumber
{
get => UserSettingsFile.Read(path).TryGetValue(NestNumberName, out var text)
&& int.TryParse(text?.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var number)
? number
: 1;
set => UserSettingsFile.Update(path, values =>
values[NestNumberName] = value.ToString(CultureInfo.InvariantCulture));
}
public DateTime LastNestCreatedDate
{
get => UserSettingsFile.Read(path).TryGetValue(LastNestCreatedDateName, out var text)
&& DateTime.TryParse(text?.Trim(), CultureInfo.InvariantCulture, DateTimeStyles.None, out var date)
? date
: DefaultDate;
set => UserSettingsFile.Update(path, values => values[LastNestCreatedDateName] = FormatDate(value));
}
public string MadeBy => UserSettingText.ReadString(UserSettingsFile.Read(path), MadeByName, "");
public void Save()
{
}
/// As System.Configuration's invariant DateTimeConverter writes it.
public static string FormatDate(DateTime value) =>
value.TimeOfDay == TimeSpan.Zero
? value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
: value.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
}