using System.Collections.ObjectModel; using System.Text.Json; using System.Text.Json.Serialization; using View_by_Distance.Shared.Models.Stateless.Methods; namespace View_by_Distance.Shared.Models; public record FilePath(long CreationTicks, string DirectoryFullPath, string ExtensionLowered, string FileNameFirstSegment, string FullName, int? Id, bool? HasIgnoreKeyword, bool? HasDateTimeOriginal, bool IsIntelligentIdFormat, long LastWriteTicks, long Length, string Name, string NameWithoutExtension, int? SortOrder) { public static FilePath Get(Properties.IPropertyConfiguration propertyConfiguration, FileHolder fileHolder, int? index) { if (fileHolder.CreationTime is null) throw new NullReferenceException(nameof(fileHolder.CreationTime)); if (fileHolder.LastWriteTime is null) throw new NullReferenceException(nameof(fileHolder.LastWriteTime)); if (fileHolder.Length is null) throw new NullReferenceException(nameof(fileHolder.Length)); FilePath result; int? id; int? sortOder; string fileNameFirstSegment = fileHolder.Name.Split('.')[0]; int sortOrderOnlyLengthIndex = propertyConfiguration.Offset.ToString().Length; string fileDirectoryName = fileHolder.DirectoryFullPath ?? throw new NullReferenceException(); bool isIntelligentIdFormat = IId.NameWithoutExtensionIsIntelligentIdFormat(propertyConfiguration, fileNameFirstSegment); bool isPaddedIntelligentIdFormat = IId.NameWithoutExtensionIsPaddedIntelligentIdFormat(propertyConfiguration, sortOrderOnlyLengthIndex, fileNameFirstSegment); bool fileNameFirstSegmentIsIdFormat = !isPaddedIntelligentIdFormat && !isIntelligentIdFormat && IId.NameWithoutExtensionIsIdFormat(propertyConfiguration, fileHolder); bool? hasIgnoreKeyword = !isIntelligentIdFormat && !isPaddedIntelligentIdFormat ? null : fileNameFirstSegment[^1] is '2' or '8'; bool? hasDateTimeOriginal = !isIntelligentIdFormat && !isPaddedIntelligentIdFormat ? null : fileNameFirstSegment[^1] is '1' or '9'; if (!fileNameFirstSegmentIsIdFormat && !isIntelligentIdFormat && !isPaddedIntelligentIdFormat) (id, sortOder) = (null, null); else if (isIntelligentIdFormat) (id, sortOder) = (IId.GetId(propertyConfiguration, fileNameFirstSegment), null); else if (isPaddedIntelligentIdFormat) { if (!int.TryParse(fileNameFirstSegment[..sortOrderOnlyLengthIndex], out int absoluteValueOfSortOrder)) (id, sortOder) = (null, null); else (id, sortOder) = (IId.GetId(propertyConfiguration, fileNameFirstSegment[sortOrderOnlyLengthIndex..]), absoluteValueOfSortOrder); } else if (fileNameFirstSegmentIsIdFormat) { if (index is null) throw new NullReferenceException(nameof(index)); if (!int.TryParse(fileNameFirstSegment, out int valueOfFileNameFirstSegment)) throw new NotSupportedException(); (id, sortOder) = (valueOfFileNameFirstSegment, propertyConfiguration.Offset + index); } else throw new NotSupportedException(); result = new(fileHolder.CreationTime.Value.Ticks, fileDirectoryName, fileHolder.ExtensionLowered, fileNameFirstSegment, fileHolder.FullName, id, hasIgnoreKeyword, hasDateTimeOriginal, isIntelligentIdFormat, fileHolder.LastWriteTime.Value.Ticks, fileHolder.Length.Value, fileHolder.Name, fileHolder.NameWithoutExtension, sortOder); return result; } public static ReadOnlyDictionary> GetFilesKeyValuePairs(ReadOnlyCollection> filePathsCollection) { Dictionary> results = []; List? collection; foreach (ReadOnlyCollection filePaths in filePathsCollection) { foreach (FilePath filePath in filePaths) { if (filePath.Id is null) continue; if (!results.TryGetValue(filePath.Id.Value, out collection)) { results.Add(filePath.Id.Value, []); if (!results.TryGetValue(filePath.Id.Value, out collection)) throw new Exception(); } collection.Add(filePath); } } return new(results); } public static ReadOnlyDictionary> GetKeyValuePairs(ReadOnlyCollection> filePathsCollection) { Dictionary> results = []; List? collection; Dictionary> keyValuePairs = []; foreach (ReadOnlyCollection filePaths in filePathsCollection) { if (filePaths.Count == 0) continue; foreach (FilePath filePath in filePaths) { if (filePath.Id is null) continue; if (!keyValuePairs.TryGetValue(filePath.Id.Value, out collection)) { keyValuePairs.Add(filePath.Id.Value, []); if (!keyValuePairs.TryGetValue(filePath.Id.Value, out collection)) throw new Exception(); } collection.Add(filePath); } } foreach (KeyValuePair> keyValuePair in keyValuePairs) results.Add(keyValuePair.Key, new(keyValuePair.Value)); return results.AsReadOnly(); } public override string ToString() { string result = JsonSerializer.Serialize(this, FilePathSourceGenerationContext.Default.FilePath); return result; } } [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(FilePath))] public partial class FilePathSourceGenerationContext : JsonSerializerContext { }