namespace View_by_Distance.Shared.Models.Stateless.Methods; internal abstract class Mapping { internal static (string?, string?, string?, bool?) GetSegments(string facesFileNameExtension, string fileName) { string[] segments = fileName.Split('.'); string? id; string? extensionLowered; bool? needsFacesFileNameExtension; string? normalizedRectangle; if (segments.Length < 4 || $".{segments[3]}" != facesFileNameExtension) { id = null; extensionLowered = null; normalizedRectangle = null; needsFacesFileNameExtension = null; } else { id = segments[0]; extensionLowered = $".{segments[2]}"; normalizedRectangle = segments[1]; needsFacesFileNameExtension = segments.Length == 3; } return new(id, normalizedRectangle, extensionLowered, needsFacesFileNameExtension); } private static (int?, int?, List?) GetConvertedsFromSegments(string facesFileNameExtension, bool idToMappingCollectionAny, Dictionary> idToMappingCollection, string fileName) { int? id; int? normalizedRectangle; List? mappingCollection; (string? Id, string? NormalizedRectangle, string? ExtensionLowered, bool? Check) segments = GetSegments(facesFileNameExtension, fileName); if (string.IsNullOrEmpty(segments.Id) || string.IsNullOrEmpty(segments.NormalizedRectangle) || string.IsNullOrEmpty(segments.ExtensionLowered) || segments.Check is null) { id = null; mappingCollection = null; normalizedRectangle = null; } else if (!int.TryParse(segments.Id, out int idValue) || !int.TryParse(segments.NormalizedRectangle, out int normalizedRectangleValue)) { id = null; mappingCollection = null; normalizedRectangle = null; } else { id = idValue; normalizedRectangle = normalizedRectangleValue; if (!idToMappingCollectionAny || !idToMappingCollection.ContainsKey(idValue)) mappingCollection = null; else mappingCollection = idToMappingCollection[idValue]; } return new(id, normalizedRectangle, mappingCollection); } internal static (int?, int?, List?) GetConverted(string facesFileNameExtension, bool idToMappingCollectionAny, Dictionary> idToMappingCollection, string file) { int? id; int? normalizedRectangle; List? mappingCollection; string fileName = Path.GetFileName(file); if (fileName.Length >= 2 && !fileName[1..].Contains('-')) (id, normalizedRectangle, mappingCollection) = GetConvertedsFromSegments(facesFileNameExtension, idToMappingCollectionAny, idToMappingCollection, fileName); else { id = null; mappingCollection = null; normalizedRectangle = null; } return new(id, normalizedRectangle, mappingCollection); } internal static int GetAreaPermille(int faceAreaPermille, int bottom, int height, int left, int right, int top, int width) { int result; double area = width * height; double locationArea = (right - left) * (bottom - top); result = (int)Math.Round(locationArea / area * faceAreaPermille, 0); return result; } }