You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.9 KiB

using System.IO;
using System.Text.Json;
namespace Housing.Services;
public sealed class LoginSelectionOptions
{
private const string DefaultContent = """
{
"loginAccounts": [
{
"id": "su",
"password": "su"
}
],
"makers": [
"AMO",
"ABC"
],
"models": [
"MODEL-1"
],
"variant1Values": [],
"variant2Values": [],
"lineValues": [
"LINE-1"
],
"jigNoValues": [
"JIG-1"
]
}
""";
public IReadOnlyList<string> Makers { get; init; } = Array.Empty<string>();
public IReadOnlyList<string> Models { get; init; } = Array.Empty<string>();
public IReadOnlyList<string> Variant1Values { get; init; } = Array.Empty<string>();
public IReadOnlyList<string> Variant2Values { get; init; } = Array.Empty<string>();
public IReadOnlyList<string> LineValues { get; init; } = Array.Empty<string>();
public IReadOnlyList<string> JigNoValues { get; init; } = Array.Empty<string>();
public IReadOnlyList<LoginAccountOption> LoginAccounts { get; init; } = Array.Empty<LoginAccountOption>();
public static LoginSelectionOptions Load(string filePath)
{
EnsureFileExists(filePath);
try
{
var json = File.ReadAllText(filePath);
var data = JsonSerializer.Deserialize<LoginSelectionOptionsData>(
json,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? new LoginSelectionOptionsData();
return new LoginSelectionOptions
{
LoginAccounts = NormalizeLoginAccounts(data.LoginAccounts),
Makers = NormalizeValues(data.Makers),
Models = NormalizeValues(data.Models),
Variant1Values = NormalizeValues(data.Variant1Values, includeBlank: true),
Variant2Values = NormalizeValues(data.Variant2Values, includeBlank: true),
LineValues = NormalizeValues(data.LineValues),
JigNoValues = NormalizeValues(data.JigNoValues)
};
}
catch
{
return new LoginSelectionOptions
{
Variant1Values = new[] { string.Empty },
Variant2Values = new[] { string.Empty }
};
}
}
public static void CreateDefaultFile(string filePath)
{
var directory = Path.GetDirectoryName(filePath);
if (!string.IsNullOrWhiteSpace(directory))
{
Directory.CreateDirectory(directory);
}
File.WriteAllText(filePath, DefaultContent);
}
private static void EnsureFileExists(string filePath)
{
if (File.Exists(filePath))
{
return;
}
CreateDefaultFile(filePath);
}
private static IReadOnlyList<string> NormalizeValues(
IReadOnlyList<string>? source,
bool includeBlank = false)
{
var values = new List<string>();
if (includeBlank)
{
values.Add(string.Empty);
}
if (source is null)
{
return values;
}
foreach (var rawValue in source)
{
var value = rawValue.Trim();
if (string.IsNullOrWhiteSpace(value))
{
continue;
}
if (!values.Any(existing => string.Equals(existing, value, StringComparison.OrdinalIgnoreCase)))
{
values.Add(value);
}
}
return values;
}
private static IReadOnlyList<LoginAccountOption> NormalizeLoginAccounts(
IReadOnlyList<LoginAccountOptionData>? source)
{
if (source is null)
{
return Array.Empty<LoginAccountOption>();
}
var accounts = new List<LoginAccountOption>();
foreach (var account in source)
{
var id = account.Id?.Trim() ?? string.Empty;
var password = account.Password ?? string.Empty;
if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(password))
{
continue;
}
if (accounts.Any(existing => string.Equals(existing.Id, id, StringComparison.OrdinalIgnoreCase)))
{
continue;
}
accounts.Add(new LoginAccountOption(id, password));
}
return accounts;
}
private sealed class LoginSelectionOptionsData
{
public List<LoginAccountOptionData>? LoginAccounts { get; set; }
public List<string>? Makers { get; set; }
public List<string>? Models { get; set; }
public List<string>? Variant1Values { get; set; }
public List<string>? Variant2Values { get; set; }
public List<string>? LineValues { get; set; }
public List<string>? JigNoValues { get; set; }
}
private sealed class LoginAccountOptionData
{
public string? Id { get; set; }
public string? Password { get; set; }
}
}
public sealed record LoginAccountOption(string Id, string Password);