using System.Collections.ObjectModel; using System.Text.Json; namespace View_by_Distance.Instance.Models; /// // List /// internal class F_Random { private readonly Configuration _Configuration; private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions; internal F_Random(Configuration configuration) { _Configuration = configuration; _WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = false }; } public override string ToString() { string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); return result; } private static ReadOnlyDictionary> GetDayToRelativePaths(ReadOnlyCollection mappingCollection, string dateFormat, ReadOnlyDictionary> idToPersonKeys) { Dictionary> results = []; string key; DateTime dateTime; List? personKeys; List? relativePaths; foreach (Shared.Models.Mapping mapping in mappingCollection) { if (mapping.MappingFromItem.ImageFileHolder.DirectoryName is null || mapping.MappingFromPerson is null) continue; if (!idToPersonKeys.TryGetValue(mapping.MappingFromItem.Id, out personKeys)) continue; if (Shared.Models.Stateless.Methods.IPersonBirthday.IsCounterPersonYear(mapping.MappingFromPerson.PersonKey)) continue; if (!personKeys.Contains(mapping.MappingFromPerson.PersonKey)) continue; dateTime = new(mapping.MappingFromPerson.PersonKey); key = dateTime.ToString(dateFormat); if (!results.TryGetValue(key, out relativePaths)) { results.Add(key, []); if (!results.TryGetValue(key, out relativePaths)) throw new Exception(); } relativePaths.Add(mapping.MappingFromItem.RelativePath); } return new(results); } internal void Random(Property.Models.Configuration configuration, int radomUseBirthdayMinimum, string[] validKeyWordsToIgnoreInRandom, string outputResolution, ReadOnlyDictionary> personKeyToIds, ReadOnlyCollection mappingCollection) { string key; string json; string jsonFile; Random random = new(); List? collection; string dateFormat = "MM-dd"; List relativePaths = []; List distinctCollection = []; DateTime dateTime = new(2024, 1, 1); //Leap year ReadOnlyDictionary> idToPersonKeys = Map.Models.Stateless.Methods.IMapLogic.GetIdToPersonKeys(personKeyToIds); ReadOnlyDictionary> dayToRelativePaths = GetDayToRelativePaths(mappingCollection, dateFormat, idToPersonKeys); string fRandomCollectionDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(F_Random), "[]"); string[] files = Directory.GetFiles(fRandomCollectionDirectory, "*", SearchOption.TopDirectoryOnly); foreach (string file in files) File.Delete(file); foreach (Shared.Models.Mapping mapping in mappingCollection) { if (distinctCollection.Contains(mapping.MappingFromItem.Id)) continue; if (mapping.MappingFromItem.Keywords is not null && mapping.MappingFromItem.Keywords.Any(l => validKeyWordsToIgnoreInRandom.Contains(l))) continue; if (mapping.MappingFromItem.ImageFileHolder.DirectoryName is null) continue; relativePaths.Add(mapping.MappingFromItem.RelativePath); distinctCollection.Add(mapping.MappingFromItem.Id); } if (relativePaths.Count > 0) { for (int i = 0; i < 366; i++) { random = new(i); key = dateTime.AddDays(i).ToString(dateFormat); if (dayToRelativePaths.TryGetValue(key, out collection) && collection.Count > radomUseBirthdayMinimum) collection = (from l in collection orderby random.NextDouble() select l).ToList(); else collection = (from l in relativePaths orderby random.NextDouble() select l).ToList(); jsonFile = Path.Combine(fRandomCollectionDirectory, $"{key}.json"); json = JsonSerializer.Serialize(collection, _WriteIndentedJsonSerializerOptions); _ = Shared.Models.Stateless.Methods.IPath.WriteAllText(jsonFile, json, updateDateWhenMatches: false, compareBeforeWrite: false); if (!_Configuration.SaveFullYearOfRandomFiles) break; } } } }