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; int hours; bool alive; string json; string? day; string alias; string? line; string? year; string? month; ConsoleKey sex; long personKey; string? lastName; string middleName; string? firstName; DateTime? dateTime; string checkDirectory; PersonName? personName; DateTime parseDateTime; 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++) { personName = null; 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; else if (consoleKey is 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.Trim(); break; } if (firstName is null) continue; lastName = null; for (int f = 0; f < 5; f++) { personName = IPerson.GetPersonName(firstName); if (!string.IsNullOrEmpty(personName.First.Value) && !string.IsNullOrEmpty(personName.Last.Value)) lastName = personName.Last.Value; else { personName = null; 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.Trim(); } break; } if (lastName is null) continue; if (personName is not null) middleName = personName.Middle.Value; else { 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 alive \"Y\" or \"N\""); consoleKey = System.Console.ReadKey().Key; log.Information(". . ."); if (consoleKey is not ConsoleKey.Y and not ConsoleKey.N) continue; alive = 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)) { log.Information("Enter persons approximate age"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _)) continue; approximateYears = line.Trim(); } else { month = line.Trim(); if (DateTime.TryParse(month, out parseDateTime) && parseDateTime != DateTime.MinValue) { month = parseDateTime.Month.ToString(); day = parseDateTime.Day.ToString(); year = parseDateTime.Year.ToString(); } else { log.Information("Enter persons birthday day (press enter if not known)"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line)) { log.Information("Enter persons approximate age"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _)) continue; approximateYears = line.Trim(); } else { day = line.Trim(); log.Information("Enter persons birthday year (press enter if not known)"); line = System.Console.ReadLine(); log.Information(". . ."); if (!string.IsNullOrEmpty(line)) year = line.Trim(); else { log.Information("Enter persons approximate age"); line = System.Console.ReadLine(); log.Information(". . ."); if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _)) continue; approximateYears = line.Trim(); } } } } 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.Trim()); 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 { hours = IPersonBirthday.GetHour(alive, sex); personKey = dateTime.Value.AddHours(hours).Ticks; personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personKey); } 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($"Person saved ({personName.First.Value} {personName.Last.Value})"); } log.Information(". . ."); } }