using System.Text.Json; using System.Text.Json.Serialization; namespace View_by_Distance.Shared.Models; public class Item : Properties.IItem { protected List _Faces; protected readonly bool? _FileSizeChanged; protected readonly FilePath _FilePath; protected bool? _IsArchive; protected bool? _IsNotUniqueAndNeedsReview; protected bool _IsUniqueFileName; protected bool _IsValidImageFormatExtension; protected bool? _LastWriteTimeChanged; protected bool? _Moved; protected Property? _Property; protected readonly string _RelativePath; protected FileHolder? _ResizedFileHolder; protected readonly FileHolder _SourceDirectoryFileHolder; public List Faces => _Faces; public bool? FileSizeChanged => _FileSizeChanged; public FilePath FilePath => _FilePath; public bool? IsArchive => _IsArchive; public bool? IsNotUniqueAndNeedsReview => _IsNotUniqueAndNeedsReview; public bool IsUniqueFileName => _IsUniqueFileName; public bool IsValidImageFormatExtension => _IsValidImageFormatExtension; public bool? LastWriteTimeChanged => _LastWriteTimeChanged; public bool? Moved => _Moved; public Property? Property => _Property; public string RelativePath => _RelativePath; public FileHolder? ResizedFileHolder => _ResizedFileHolder; public FileHolder SourceDirectoryFileHolder => _SourceDirectoryFileHolder; [JsonConstructor] public Item(List faces, FilePath filePath, bool? fileSizeChanged, bool? isArchive, bool? isNotUniqueAndNeedsReview, bool isUniqueFileName, bool isValidImageFormatExtension, bool? lastWriteTimeChanged, bool? moved, Property? property, string relativePath, FileHolder? resizedFileHolder, FileHolder sourceDirectoryFileHolder) { _Faces = faces; _FilePath = filePath; _FileSizeChanged = fileSizeChanged; _IsArchive = isArchive; _IsNotUniqueAndNeedsReview = isNotUniqueAndNeedsReview; _IsUniqueFileName = isUniqueFileName; _IsValidImageFormatExtension = isValidImageFormatExtension; _LastWriteTimeChanged = lastWriteTimeChanged; _Moved = moved; _Property = property; _RelativePath = relativePath; _ResizedFileHolder = resizedFileHolder; _SourceDirectoryFileHolder = sourceDirectoryFileHolder; } public static Item Get(FilePath filePath, FileHolder sourceDirectoryFileHolder, string relativePath, bool? isArchive, bool? isNotUniqueAndNeedsReview, bool isUniqueFileName, bool isValidImageFormatExtension, Property? property, bool? abandoned, bool? fileSizeChanged, bool? lastWriteTimeChanged) { Item result; if (relativePath.EndsWith(".json")) throw new ArgumentException("Can not be a *.json file!"); if (filePath.ExtensionLowered is ".json") throw new ArgumentException("Can not be a *.json file!"); result = new([], filePath, fileSizeChanged, isArchive, isNotUniqueAndNeedsReview, isUniqueFileName, isValidImageFormatExtension, lastWriteTimeChanged, null, property, relativePath, null, sourceDirectoryFileHolder); return result; } public static Item Get(FilePath filePath, FileHolder sourceDirectoryFileHolder, string relativePath, bool isValidImageFormatExtension) => Get(filePath, sourceDirectoryFileHolder, relativePath, null, null, false, isValidImageFormatExtension, null, null, null, null); public override string ToString() { string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); return result; } public void SetMoved(bool moved) => _Moved = moved; public void SetResizedFileHolder(string filenameExtension, FileHolder fileHolder) { if (fileHolder.ExtensionLowered == filenameExtension) _ResizedFileHolder = fileHolder; else if (fileHolder.ExtensionLowered.Length > 3 && fileHolder.ExtensionLowered[3] == 'e' && fileHolder.ExtensionLowered.Remove(3, 1) == filenameExtension) _ResizedFileHolder = fileHolder; else _ResizedFileHolder = fileHolder; } public bool Any() => !_SourceDirectoryFileHolder.Exists || (_FileSizeChanged.HasValue && _FileSizeChanged.Value) || (_LastWriteTimeChanged.HasValue && _LastWriteTimeChanged.Value) || (_Moved.HasValue && _Moved.Value); public void Update(Property property) => _Property = property; }