namespace View_by_Distance.Shared.Models.Stateless.Methods; internal abstract class Mapping { private static void IfNotAlreadyFileMove(string file, int idValue, int normalizedPixelPercentageValue, string extensionLowered) { string? directoryName = Path.GetDirectoryName(file); if (string.IsNullOrEmpty(directoryName)) throw new Exception(); string checkFile = Path.Combine(directoryName, $"{IMapping.GetDeterministicHashCodeKey(idValue, normalizedPixelPercentageValue)}{extensionLowered}"); if (!File.Exists(checkFile)) File.Move(file, checkFile); } private static (int?, int?, List?) GetReversedDeterministicHashCodeKeysFromSegments(int locationDigits, bool keyValuePairsAny, Dictionary> keyValuePairs, string file, string[] segments) { int? id; List? faces; int? normalizedPixelPercentage; if (segments.Length != 3) { id = null; faces = null; normalizedPixelPercentage = null; } else if (!int.TryParse(segments[0], out int idValue) || !int.TryParse(ILocation.GetRightPadded(locationDigits, segments[1]), out int normalizedPixelPercentageValue)) { id = null; faces = null; normalizedPixelPercentage = null; } else { id = idValue; string extensionLowered = $".{segments[2]}"; normalizedPixelPercentage = normalizedPixelPercentageValue; if (segments[1].Length != locationDigits) IfNotAlreadyFileMove(file, idValue, normalizedPixelPercentageValue, extensionLowered); if (!keyValuePairsAny || !keyValuePairs.ContainsKey(idValue)) faces = null; else faces = keyValuePairs[idValue]; } return new(id, normalizedPixelPercentage, faces); } internal static (int?, int?, List?) GetReversedDeterministicHashCodeKey(int locationDigits, bool keyValuePairsAny, Dictionary> keyValuePairs, 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 { string[] segments = fileName.Split('.'); (id, normalizedPixelPercentage, faces) = GetReversedDeterministicHashCodeKeysFromSegments(locationDigits, keyValuePairsAny, keyValuePairs, file, segments); } return new(id, normalizedPixelPercentage, faces); } }