using Microsoft.Extensions.Configuration; using Phares.Shared; using Serilog; using System.Text.Json; using View_by_Distance.Person.Models; using View_by_Distance.Shared.Models; using View_by_Distance.Shared.Models.Stateless.Methods; namespace View_by_Distance.Person; public class Person { private readonly AppSettings _AppSettings; private readonly string _WorkingDirectory; private readonly IsEnvironment _IsEnvironment; private readonly Configuration _Configuration; private readonly IConfigurationRoot _ConfigurationRoot; private readonly Property.Models.Configuration _PropertyConfiguration; public Person(List args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, Shared.Models.Methods.IConsole console) { if (isSilent) { } if (console is null) { } _AppSettings = appSettings; _IsEnvironment = isEnvironment; long ticks = DateTime.Now.Ticks; _WorkingDirectory = workingDirectory; _ConfigurationRoot = configurationRoot; ILogger? log = Log.ForContext(); Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot); Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration); _PropertyConfiguration = propertyConfiguration; _Configuration = configuration; propertyConfiguration.Update(); string? comparePathRoot = Path.GetDirectoryName(configuration.SaveDirectory); if (comparePathRoot is null || comparePathRoot == propertyConfiguration.RootDirectory) throw new Exception("Nested isn't allowed!"); if (!Directory.Exists(configuration.SaveDirectory)) _ = Directory.CreateDirectory(configuration.SaveDirectory); log.Information(propertyConfiguration.RootDirectory); Verify(); Loop(ticks, log); } private void Verify() { if (_AppSettings is null) { } if (_IsEnvironment is null) { } if (_Configuration is null) { } if (_ConfigurationRoot is null) { } if (_WorkingDirectory is null) { } if (_PropertyConfiguration is null) { } } private void Loop(long ticks, ILogger log) { Uri uri; HttpClient httpClient = new(); string fileName; Task task; int age; string json; string code; string? day; string alias; string? line; string? year; string? month; bool deceased; ConsoleKey sex; long personKey; string? lastName; string middleName; string? firstName; DateTime? dateTime; PersonName personName; string checkDirectory; ConsoleKey? consoleKey; string? approximateYears; string personKeyFormatted; string personDisplayDirectory; log.Information($"Ready to create / update a person? [{ticks}]"); for (int y = 0; y < int.MaxValue; y++) { log.Information("Press \"Y\" key to continue, \"N\" key exit or close console"); consoleKey = System.Console.ReadKey().Key; log.Information(". . ."); if (consoleKey is not ConsoleKey.Y and not ConsoleKey.N) break; firstName = null; for (int f = 0; f < 5; f++) { log.Information("Enter persons first name (minimum length of two characters)"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line) || line.Length < 2) continue; firstName = line; break; } if (firstName is null) continue; lastName = null; for (int f = 0; f < 5; f++) { log.Information("Enter persons last name (minimum length of two characters)"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line) || line.Length < 2) continue; lastName = line; break; } if (lastName is null) continue; log.Information("Enter persons middle name (press enter if they don't have a middle name)"); line = System.Console.ReadLine(); log.Information(". . ."); middleName = string.IsNullOrEmpty(line) ? string.Empty : line; log.Information("Enter persons alias (press enter if they don't have a alias)"); line = System.Console.ReadLine(); log.Information(". . ."); alias = string.IsNullOrEmpty(line) ? string.Empty : line; personName = new(new(firstName), new(middleName), new(lastName), new(alias)); json = JsonSerializer.Serialize(personName, new JsonSerializerOptions { WriteIndented = true }); log.Information("Is the person \"M\" (Male), \"F\" (Female) or \"U\" (Unknown)"); consoleKey = System.Console.ReadKey().Key; log.Information(". . ."); if (consoleKey is not ConsoleKey.M and not ConsoleKey.F and not ConsoleKey.U) continue; sex = consoleKey.Value; log.Information("Is the person deceased \"Y\" or \"N\""); consoleKey = System.Console.ReadKey().Key; log.Information(". . ."); if (consoleKey is not ConsoleKey.Y and not ConsoleKey.N) continue; deceased = consoleKey == ConsoleKey.Y; dateTime = null; approximateYears = null; for (int f = 0; f < 5; f++) { day = null; year = null; month = null; approximateYears = null; log.Information("Enter persons birthday month (press enter if not known) [MMMM || MMM || MM || M]"); line = System.Console.ReadLine(); log.Information(". . ."); if (!string.IsNullOrEmpty(line)) month = line; else { log.Information("Enter persons approximate age"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _)) continue; approximateYears = line; } log.Information("Enter persons birthday day (press enter if not known)"); line = System.Console.ReadLine(); log.Information(". . ."); if (!string.IsNullOrEmpty(line)) day = line; else { log.Information("Enter persons approximate age"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _)) continue; approximateYears = line; } log.Information("Enter persons birthday year (press enter if not known)"); line = System.Console.ReadLine(); log.Information(". . ."); if (!string.IsNullOrEmpty(line)) year = line; else { log.Information("Enter persons approximate age"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _)) continue; approximateYears = line; } if (month is null || day is null || year is null) dateTime = null; else { dateTime = IPersonBirthday.GetDate(month, day, year); if (dateTime is not null) { (age, _) = IAge.GetAge(new DateTime(ticks).Ticks, dateTime.Value); approximateYears = age.ToString(); } } if (approximateYears is null && (dateTime is null || dateTime == DateTime.MinValue)) continue; break; } if (approximateYears is null) continue; personDisplayDirectory = Path.Combine(_Configuration.SaveDirectory, ticks.ToString(), $"{personName.First.Value} {personName.Last.Value}~{approximateYears}"); if (!Directory.Exists(personDisplayDirectory)) _ = Directory.CreateDirectory(personDisplayDirectory); log.Information("Enter url to download an image (press enter if none)"); line = System.Console.ReadLine(); log.Information(". . ."); if (!string.IsNullOrEmpty(line)) { uri = new(line); if (uri.HostNameType != UriHostNameType.Dns) continue; task = httpClient.GetByteArrayAsync(uri); fileName = Path.Combine(personDisplayDirectory, Path.GetFileName(uri.LocalPath)); File.WriteAllBytes(fileName, task.Result); } if (dateTime is null) personKeyFormatted = "2"; else { personKey = dateTime.Value.Ticks; if (deceased) code = sex is ConsoleKey.M ? "05" : sex is ConsoleKey.F ? "04" : sex is ConsoleKey.U ? "02" : throw new NotImplementedException(); else code = sex is ConsoleKey.M ? "15" : sex is ConsoleKey.F ? "14" : sex is ConsoleKey.U ? "03" : throw new NotImplementedException(); personKeyFormatted = $"{IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personKey)[..^2]}{code}"; } checkDirectory = Path.Combine(personDisplayDirectory, personKeyFormatted); log.Information($"Working directory <{checkDirectory}>"); if (!Directory.Exists(checkDirectory)) _ = Directory.CreateDirectory(checkDirectory); _ = IPath.WriteAllText(Path.Combine(checkDirectory, $"{personKeyFormatted}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true); } log.Information(". . ."); } }