2023-03-29 22:45:03 -07:00

73 lines
3.6 KiB
C#

using System.Text;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Person
{
// ...
internal static (Models.PersonBirthday?, string) Get(string personBirthdayFormat, string personDisplayDirectory, string personKeyDirectory, DateTime birthday)
{
Models.PersonBirthday? personBirthday = new(birthday);
string personKeyFormatted = IPersonBirthday.GetFormatted(personBirthdayFormat, personBirthday);
string convertedPersonKeyDirectory = Path.Combine(personDisplayDirectory, personKeyFormatted);
if (!Directory.Exists(convertedPersonKeyDirectory))
Directory.Move(personKeyDirectory, convertedPersonKeyDirectory);
return new(personBirthday, personKeyFormatted);
}
private static void WriteGedFile(string? personKeyFormatted, Models.PersonBirthday personBirthday, Models.PersonName name, string[] matches)
{
string[] pGedFiles = (from l in matches where l.EndsWith(".pged") select l).ToArray();
if (!pGedFiles.Any())
{
StringBuilder stringBuilder = new();
_ = stringBuilder.Append("0 @I").Append(personKeyFormatted).AppendLine("@ INDI");
_ = stringBuilder.Append("1 NAME ").Append(name.First.Value).Append(" /").Append(name.Last.Value).AppendLine("/");
if (!string.IsNullOrEmpty(name.First.Value))
_ = stringBuilder.Append("2 GIVN ").AppendLine(name.First.Value);
if (!string.IsNullOrEmpty(name.Last.Value))
_ = stringBuilder.Append("2 SURN ").AppendLine(name.Last.Value);
if (!string.IsNullOrEmpty(name.Alias.Value))
{
_ = stringBuilder.Append("2 NICK ").AppendLine(name.Alias.Value);
if (name.Alias.Value.Contains(" Jr"))
_ = stringBuilder.Append("2 NSFX ").AppendLine("Jr");
else if (name.Alias.Value.Contains(" Sr"))
_ = stringBuilder.Append("2 NSFX ").AppendLine("Sr");
}
_ = stringBuilder.AppendLine("1 SEX U");
if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday))
{
_ = stringBuilder.AppendLine("1 BIRT");
_ = stringBuilder.Append("2 DATE ").AppendLine(personBirthday.Value.ToString("dd MMM yyyy"));
}
string? directory = Path.GetDirectoryName(matches[0]);
if (directory is null)
throw new Exception();
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
File.WriteAllText(Path.Combine(directory, $"{personKeyFormatted}.pged"), stringBuilder.ToString());
}
}
internal static Models.Person GetPerson(string[] personDisplayDirectoryAllFiles, string? personKeyFormatted, long personKey, Models.PersonBirthday personBirthday, string[] segments)
{
Models.Person result;
const int zero = 0;
List<Models.PersonURL> urls = new();
Models.PersonId id = new(personKey);
List<Models.PersonEmail> emails = new();
List<Models.PersonNumber> numbers = new();
List<Models.PersonComment> comments = new();
List<Models.PersonAddress> addresses = new();
Models.PersonName name = PersonName.Create(segments[zero]);
string[] matches = (from l in personDisplayDirectoryAllFiles where !string.IsNullOrEmpty(personKeyFormatted) && l.Contains(personKeyFormatted) select l).ToArray();
if (matches.Any())
WriteGedFile(personKeyFormatted, personBirthday, name, matches);
result = new(id, personBirthday, name, comments, urls, numbers, emails, addresses);
return result;
}
}