aa/People/Models/Stateless/ReadOnlyCollectionsLogic.cs

110 lines
6.3 KiB
C#

using System.Collections.ObjectModel;
using View_by_Distance.Metadata.Models;
using View_by_Distance.Shared.Models;
using View_by_Distance.Shared.Models.Properties;
using View_by_Distance.Shared.Models.Stateless;
namespace View_by_Distance.People.Models.Stateless;
internal static class ReadOnlyCollectionsLogic
{
private static void SetPersonCollectionsAfterSetSkipCollections(PeopleSettings peopleSettings, ReadOnlyCollection<PersonContainer> personContainers, Dictionary<string, string> personKeyFormattedToNewestPersonKeyFormatted, List<string> personKeyFormattedCollection)
{
string personKeyFormatted;
string newestPersonKeyFormatted;
foreach (PersonContainer personContainer in personContainers)
{
if (personContainer.Key is null || personContainer.Birthdays is null || personContainer.Birthdays.Length == 0)
continue;
foreach (PersonBirthday personBirthday in personContainer.Birthdays)
{
personKeyFormatted = IPersonBirthday.GetFormatted(peopleSettings.PersonBirthdayFormat, personBirthday);
personKeyFormattedCollection.Add(personKeyFormatted);
if (personContainer.Birthdays.Length < 1)
continue;
newestPersonKeyFormatted = IPersonBirthday.GetFormatted(peopleSettings.PersonBirthdayFormat, personContainer.Key.Value);
_ = personKeyFormattedToNewestPersonKeyFormatted.TryAdd(personKeyFormatted, newestPersonKeyFormatted);
}
}
}
private static void SetSkipCollections(ResultSettings resultSettings, ICompareSettings compareSettings, ReadOnlyCollection<PersonContainer> personContainers, Dictionary<int, List<FilePathAndWholePercentages>> skipCollection, Dictionary<int, List<FilePathAndWholePercentages>> skipNotSkipCollection, ReadOnlyCollection<string> skipNotSkipDirectories)
{
string checkFile;
int? wholePercentages;
List<FilePath> distinct = [];
List<string> distinctFiles = [];
List<string> distinctFileName = [];
FilePathAndWholePercentages filePathAndWholePercentages;
bool skipNotSkipDirectoriesAny = skipNotSkipDirectories.Count > 0;
string a2PeopleSingletonDirectory = Path.GetFullPath(IResult.GetResultsDateGroupDirectory(resultSettings, nameof(A2_People), resultSettings.ResultSingleton));
string[] checkDirectories = string.IsNullOrEmpty(a2PeopleSingletonDirectory) ? [] : (from l in skipNotSkipDirectories select Path.GetFullPath($"{a2PeopleSingletonDirectory}{l}")).ToArray();
foreach (PersonContainer personContainer in personContainers)
{
foreach (FilePath personDisplayDirectoryAllFilePath in personContainer.DisplayDirectoryAllFilePaths)
{
if (personDisplayDirectoryAllFilePath.ExtensionLowered != compareSettings.FacesFileNameExtension)
continue;
if (distinctFiles.Contains(personDisplayDirectoryAllFilePath.FullName))
continue;
distinctFiles.Add(personDisplayDirectoryAllFilePath.FullName);
distinct.Add(personDisplayDirectoryAllFilePath);
}
}
foreach (FilePath filePath in distinct)
{
if (distinctFileName.Contains(filePath.Name))
{
checkFile = $"{filePath.FullName}.dup";
if (File.Exists(checkFile))
continue;
File.Move(filePath.FullName, checkFile);
continue;
}
if (filePath.Id is null)
continue;
wholePercentages = IMapping.GetWholePercentages(compareSettings, filePath);
if (wholePercentages is null)
continue;
if (!skipNotSkipDirectoriesAny || !checkDirectories.Any(filePath.FullName.StartsWith))
{
if (!skipCollection.ContainsKey(filePath.Id.Value))
skipCollection.Add(filePath.Id.Value, []);
filePathAndWholePercentages = new(filePath, wholePercentages.Value);
skipCollection[filePath.Id.Value].Add(filePathAndWholePercentages);
}
else
{
if (!skipNotSkipCollection.ContainsKey(filePath.Id.Value))
skipNotSkipCollection.Add(filePath.Id.Value, []);
filePathAndWholePercentages = new(filePath, wholePercentages.Value);
skipNotSkipCollection[filePath.Id.Value].Add(filePathAndWholePercentages);
}
}
}
internal static ReadOnlyCollections GetReadOnlyCollections(ResultSettings resultSettings, PeopleSettings peopleSettings, DistanceSettings distanceSettings, ICompareSettings compareSettings, ReadOnlyCollection<PersonContainer> personContainers, ReadOnlyCollection<long> personKeys)
{
ReadOnlyCollections result;
List<string> personKeyFormattedCollection = [];
Dictionary<int, List<FilePathAndWholePercentages>> skipCollection = [];
Dictionary<string, string> personKeyFormattedToNewestPersonKeyFormatted = [];
Dictionary<int, List<FilePathAndWholePercentages>> skipNotSkipCollection = [];
ReadOnlyCollection<string> skipNotSkipDirectories = distanceSettings.SkipNotSkipDirectories.Where(l => !string.IsNullOrEmpty(l)).ToArray().AsReadOnly();
SetSkipCollections(resultSettings, compareSettings, personContainers, skipCollection, skipNotSkipCollection, skipNotSkipDirectories);
SetPersonCollectionsAfterSetSkipCollections(peopleSettings, personContainers, personKeyFormattedToNewestPersonKeyFormatted, personKeyFormattedCollection);
ReadOnlyCollection<PersonKeyFormattedAndKeyTicksAndDisplayDirectoryName> jLinkResolvedDirectories = IPeople.GetJLinkResolvedDirectories(resultSettings, peopleSettings);
ReadOnlyCollection<long> jLinkResolvedPersonKeys = jLinkResolvedDirectories.Select(l => l.KeyTicks).ToArray().AsReadOnly();
result = new(jLinkResolvedPersonKeys,
personContainers,
personKeyFormattedCollection.AsReadOnly(),
personKeyFormattedToNewestPersonKeyFormatted.AsReadOnly(),
personKeys,
skipCollection.AsReadOnly(),
skipNotSkipCollection.AsReadOnly(),
skipNotSkipDirectories);
return result;
}
}