2022-08-02 19:41:36 -07:00

142 lines
6.1 KiB
C#

using System.Text.Json.Serialization;
namespace View_by_Distance.Property.Models;
public class PropertyHolder
{
protected readonly bool? _Abandoned;
protected readonly bool? _Changed;
protected List<object> _Faces;
protected readonly FileInfo? _ImageFileInfo;
protected readonly string _ImageFileNameWithoutExtension;
protected readonly int _G;
protected DateTime? _MinimumDateTime;
protected bool? _Moved;
protected List<(bool?, string, TimeSpan?)> _Named;
protected readonly bool? _NoJson;
protected A_Property? _Property;
protected readonly int _R;
protected readonly string _RelativePath;
protected FileInfo? _ResizedFileInfo;
protected readonly string _SourceDirectory;
protected readonly string _SourceDirectoryFile;
protected bool? _ValidImageFormatExtension;
public bool? Abandoned => _Abandoned;
public bool? Changed => _Changed;
public List<object> Faces => _Faces;
public FileInfo? ImageFileInfo => _ImageFileInfo;
public string ImageFileNameWithoutExtension => _ImageFileNameWithoutExtension;
public int G => _G;
public DateTime? MinimumDateTime => _MinimumDateTime;
public bool? Moved => _Moved;
public bool? NoJson => _NoJson;
public List<(bool? IsWrongYear, string PersonKey, TimeSpan? TimeSpan)> Named => _Named;
public A_Property? Property => _Property;
public int R => _R;
public string RelativePath => _RelativePath;
public FileInfo? ResizedFileInfo => _ResizedFileInfo;
public string SourceDirectory => _SourceDirectory;
public string SourceDirectoryFile => _SourceDirectoryFile;
public bool? ValidImageFormatExtension => _ValidImageFormatExtension;
public PropertyHolder()
{
_G = -1;
_R = -1;
_Faces = new();
_Named = new();
_RelativePath = string.Empty;
_SourceDirectory = string.Empty;
_SourceDirectoryFile = string.Empty;
_ImageFileNameWithoutExtension = string.Empty;
}
[JsonConstructor]
public PropertyHolder(int g, string sourceDirectory, string sourceDirectoryFile, string relativePath, int r, FileInfo? imageFileInfo, A_Property? property, bool? abandoned, bool? changed, bool? moved, bool? validImageFormatExtension)
{
_G = g;
_R = r;
_Faces = new();
_Moved = moved;
_Named = new();
_Changed = changed;
_Property = property;
_Abandoned = abandoned;
_NoJson = abandoned is null;
_RelativePath = relativePath;
_ImageFileInfo = imageFileInfo;
_SourceDirectory = sourceDirectory;
_SourceDirectoryFile = sourceDirectoryFile;
_ValidImageFormatExtension = validImageFormatExtension;
_MinimumDateTime = Stateless.A_Property.GetMinimumDateTime(property);
if (imageFileInfo is null)
_ImageFileNameWithoutExtension = string.Empty;
else
_ImageFileNameWithoutExtension = Path.GetFileNameWithoutExtension(imageFileInfo.FullName);
if (imageFileInfo is not null && imageFileInfo.Extension is ".json")
throw new ArgumentException("Can not be a *.json file!");
if (!sourceDirectoryFile.EndsWith(".json") && !sourceDirectoryFile.EndsWith(".old"))
throw new ArgumentException("Must be a *.json or *.old file!");
}
internal void SetValidImageFormatExtension(bool isValidImageFormatExtension) => _ValidImageFormatExtension = isValidImageFormatExtension;
internal void SetMoved(bool moved) => _Moved = moved;
public static string GetWrongYearFlag(bool? isWrongYear) => isWrongYear is null ? "#" : isWrongYear.Value ? "~" : "=";
public void SetResizedFileInfo(FileInfo fileInfo) => _ResizedFileInfo = fileInfo;
public bool Any() => (_Abandoned.HasValue && _Abandoned.Value) || (_Changed.HasValue && _Changed.Value) || (_Moved.HasValue && _Moved.Value) || (_NoJson.HasValue && _NoJson.Value);
public void Update(A_Property property)
{
_Property = property;
_MinimumDateTime = Stateless.A_Property.GetMinimumDateTime(property);
}
public static void AddToFaces(PropertyHolder[] filteredPropertyHolderCollection, object[] faces)
{
foreach (PropertyHolder propertyHolder in filteredPropertyHolderCollection)
propertyHolder.Faces.AddRange(faces);
}
public static void AddToNamed(PropertyLogic propertyLogic, PropertyHolder[] filteredPropertyHolderCollection)
{
bool? isWrongYear;
TimeSpan? timeSpan;
DateTime? birthDate;
string[] personKeys;
DateTime minimumDateTime;
PropertyHolder propertyHolder;
for (int i = 0; i < filteredPropertyHolderCollection.Length; i++)
{
propertyHolder = filteredPropertyHolderCollection[i];
if (propertyHolder.ImageFileInfo is null)
continue;
if (propertyHolder.Property?.Id is null || propertyHolder.MinimumDateTime is null || propertyHolder.ResizedFileInfo is null)
continue;
if (!propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.ContainsKey(propertyHolder.Property.Id.Value))
continue;
minimumDateTime = Stateless.A_Property.GetMinimumDateTime(propertyHolder.Property);
personKeys = propertyLogic.NamedFaceInfoDeterministicHashCodeIndices[propertyHolder.Property.Id.Value];
(isWrongYear, _) = propertyHolder.Property.IsWrongYear(propertyHolder.ImageFileInfo.FullName, minimumDateTime);
foreach (string personKey in personKeys)
{
if (isWrongYear is null || isWrongYear.Value || personKey[..2] is not "19" and not "20")
timeSpan = null;
else
{
birthDate = Shared.Models.Stateless.Methods.IPersonBirthday.Get(personKey);
if (birthDate is null)
timeSpan = null;
else
timeSpan = new(minimumDateTime.Ticks - birthDate.Value.Ticks);
}
propertyHolder.Named.Add(new(isWrongYear, personKey, timeSpan));
}
}
}
}