124 lines
6.2 KiB
C#

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())
{
string? directory = Path.GetDirectoryName(matches[0]);
if (directory is null)
throw new Exception();
List<string> pGedLines = new();
string nameLine = $"1 NAME {name.First.Value} /{name.Last.Value}/";
pGedLines.Add($"0 @I{personKeyFormatted}@ INDI");
pGedLines.Add(nameLine);
if (!string.IsNullOrEmpty(name.First.Value))
pGedLines.Add($"2 GIVN {name.First.Value}");
if (!string.IsNullOrEmpty(name.Last.Value))
pGedLines.Add($"2 SURN {name.Last.Value}");
if (!string.IsNullOrEmpty(name.Alias.Value))
{
pGedLines.Add($"2 NICK {name.Alias.Value}");
if (name.Alias.Value.Contains(" Jr"))
pGedLines.Add("2 NSFX Jr");
else if (name.Alias.Value.Contains(" Sr"))
pGedLines.Add("2 NSFX Sr");
}
if (personKeyFormatted[^2..] == "23")
pGedLines.Add("1 SEX F");
else if (personKeyFormatted[^2..] == "22")
pGedLines.Add("1 SEX M");
else if (personKeyFormatted[^2..] == "21")
pGedLines.Add("1 SEX U");
else
{
string? sexLine = null;
// string[] sourceLines = File.ReadAllLines("D:/1) Images A/Images-9b89679-Results/A2) People/9b89679/([])/FamilyEcho/638157228251558818-RootsMagic-Export.ged");
string[] sourceLines = File.ReadAllLines("D:/1) Images A/Images-9b89679-Results/A2) People/9b89679/([])/638157314010628679.ged");
for (int i = 0; i < sourceLines.Length; i++)
{
if (sourceLines[i] != nameLine)
continue;
for (int j = i + 1; j < sourceLines.Length; j++)
{
i = j;
if (sourceLines[j].StartsWith("0 @I"))
break;
if (sourceLines[j].StartsWith("1 SEX "))
{
sexLine = sourceLines[j];
break;
}
}
if (sexLine is not null)
break;
}
if (sexLine is null)
pGedLines.Add("1 SEX U");
else
{
pGedLines.Add($"1 SEX {sexLine[6]}");
string sex = sexLine[6] is 'F' ? "23" : sexLine[6] is 'M' ? "22" : sexLine[6] is 'U' ? "21" : throw new NotImplementedException();
if (directory.EndsWith("00"))
directory = string.Concat(directory[..^2], sex);
else if (directory.EndsWith("01"))
directory = string.Concat(directory[..^2], sex);
else if (directory.EndsWith("04")) // Cameran
directory = string.Concat(directory[..^2], sex);
else if (directory.EndsWith("05")) // Daisy
directory = string.Concat(directory[..^2], sex);
else if (directory.EndsWith("18")) // Meghan
directory = string.Concat(directory[..^2], sex);
else if (directory.EndsWith("20")) // Joey
directory = string.Concat(directory[..^2], sex);
else
throw new NotImplementedException();
personKeyFormatted = $"{personKeyFormatted[..^2]}{sex}";
}
}
if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday))
{
pGedLines.Add("1 BIRT");
pGedLines.Add($"2 DATE {personBirthday.Value:dd MMM yyyy}");
}
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
string text = string.Join(Environment.NewLine, pGedLines);
_ = IPath.WriteAllText(Path.Combine(directory, $"{personKeyFormatted}.pged"), text, updateDateWhenMatches: false, compareBeforeWrite: true);
}
}
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 (!string.IsNullOrEmpty(personKeyFormatted) && matches.Any())
WriteGedFile(personKeyFormatted, personBirthday, name, matches);
result = new(id, personBirthday, name, comments, urls, numbers, emails, addresses);
return result;
}
}