namespace View_by_Distance.Shared.Models.Stateless.Methods; internal abstract class Mapping { internal static (string?, string?, string?, bool?) GetSegments(int locationDigits, string facesFileNameExtension, string fileName) { string[] segments = fileName.Split('.'); string? id; string? extensionLowered; bool? needsFacesFileNameExtension; string? normalizedPixelPercentage; if (segments.Length < 4 || $".{segments[3]}" != facesFileNameExtension) { id = null; extensionLowered = null; normalizedPixelPercentage = null; needsFacesFileNameExtension = null; } else { id = segments[0]; extensionLowered = $".{segments[2]}"; needsFacesFileNameExtension = segments.Length == 3; normalizedPixelPercentage = segments[1].Length == locationDigits ? segments[1] : null; } return new(id, normalizedPixelPercentage, extensionLowered, needsFacesFileNameExtension); } private static (int?, int?, List?) GetReversedDeterministicHashCodeKeysFromSegments(int locationDigits, string facesFileNameExtension, bool idToFacesAny, Dictionary> idToFaces, string fileName) { int? id; List? faces; int? normalizedPixelPercentage; (string? Id, string? NormalizedPixelPercentage, string? ExtensionLowered, bool? Check) segments = GetSegments(locationDigits, facesFileNameExtension, fileName); if (string.IsNullOrEmpty(segments.Id) || string.IsNullOrEmpty(segments.NormalizedPixelPercentage) || string.IsNullOrEmpty(segments.ExtensionLowered) || segments.Check is null) { id = null; faces = null; normalizedPixelPercentage = null; } else if (!int.TryParse(segments.Id, out int idValue) || !int.TryParse(segments.NormalizedPixelPercentage, out int normalizedPixelPercentageValue)) { id = null; faces = null; normalizedPixelPercentage = null; } else { id = idValue; normalizedPixelPercentage = segments.NormalizedPixelPercentage.Length == locationDigits ? normalizedPixelPercentageValue : null; if (!idToFacesAny || !idToFaces.ContainsKey(idValue)) faces = null; else faces = idToFaces[idValue]; } return new(id, normalizedPixelPercentage, faces); } internal static (int?, int?, List?) GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, bool idToFacesAny, Dictionary> idToFaces, string file) { int? id; List? faces; int? normalizedPixelPercentage; string fileName = Path.GetFileName(file); if (fileName.Length < 2 || fileName[1..].Contains('-')) { id = null; faces = null; normalizedPixelPercentage = null; } else (id, normalizedPixelPercentage, faces) = GetReversedDeterministicHashCodeKeysFromSegments( locationDigits, facesFileNameExtension, idToFacesAny, idToFaces, fileName); return new(id, normalizedPixelPercentage, faces); } }