2023-11-12 00:20:02 -07:00

106 lines
5.0 KiB
C#

using System.Collections.ObjectModel;
using System.Text.Json;
namespace View_by_Distance.Instance.Models;
/// <summary>
// List<string>
/// </summary>
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<string, List<string>> GetDayToRelativePaths(ReadOnlyCollection<Shared.Models.Mapping> mappingCollection, string dateFormat, ReadOnlyDictionary<int, List<long>> idToPersonKeys)
{
Dictionary<string, List<string>> results = [];
string key;
DateTime dateTime;
List<long>? personKeys;
List<string>? 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<long, List<int>> personKeyToIds, ReadOnlyCollection<int>? notNineCollection, ReadOnlyCollection<Shared.Models.Mapping> mappingCollection)
{
string key;
string json;
string jsonFile;
Random random = new();
List<string>? collection;
string dateFormat = "MM-dd";
List<string> relativePaths = [];
List<int> distinctCollection = [];
DateTime dateTime = new(2024, 1, 1); //Leap year
ReadOnlyDictionary<int, List<long>> idToPersonKeys = Map.Models.Stateless.Methods.IMapLogic.GetIdToPersonKeys(personKeyToIds);
ReadOnlyDictionary<string, List<string>> 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.ImageFileHolder.DirectoryName is null)
continue;
if (notNineCollection is not null && notNineCollection.Contains(mapping.MappingFromItem.Id))
continue;
if (mapping.MappingFromItem.Keywords is not null && mapping.MappingFromItem.Keywords.Any(l => validKeyWordsToIgnoreInRandom.Contains(l)))
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;
}
}
}
}