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? normalizedPixelPercentage;
        if (segments.Length < 4 || $".{segments[3]}" != facesFileNameExtension)
        {
            id = null;
            extensionLowered = null;
            normalizedPixelPercentage = null;
            needsFacesFileNameExtension = null;
        }
        else
        {
            id = segments[0];
            extensionLowered = $".{segments[2]}";
            normalizedPixelPercentage = segments[1];
            needsFacesFileNameExtension = segments.Length == 3;
        }
        return new(id, normalizedPixelPercentage, extensionLowered, needsFacesFileNameExtension);
    }

    private static (int?, int?, List<Models.Mapping>?) GetReversedDeterministicHashCodeKeysFromSegments(string facesFileNameExtension, bool idToMappingCollectionAny, Dictionary<int, List<Models.Mapping>> idToMappingCollection, string fileName)
    {
        int? id;
        int? normalizedPixelPercentage;
        List<Models.Mapping>? mappingCollection;
        (string? Id, string? NormalizedPixelPercentage, string? ExtensionLowered, bool? Check) segments = GetSegments(facesFileNameExtension, fileName);
        if (string.IsNullOrEmpty(segments.Id) || string.IsNullOrEmpty(segments.NormalizedPixelPercentage) || string.IsNullOrEmpty(segments.ExtensionLowered) || segments.Check is null)
        {
            id = null;
            mappingCollection = null;
            normalizedPixelPercentage = null;
        }
        else if (!int.TryParse(segments.Id, out int idValue) || !int.TryParse(segments.NormalizedPixelPercentage, out int normalizedPixelPercentageValue))
        {
            id = null;
            mappingCollection = null;
            normalizedPixelPercentage = null;
        }
        else
        {
            id = idValue;
            normalizedPixelPercentage = normalizedPixelPercentageValue;
            if (!idToMappingCollectionAny || !idToMappingCollection.ContainsKey(idValue))
                mappingCollection = null;
            else
                mappingCollection = idToMappingCollection[idValue];
        }
        return new(id, normalizedPixelPercentage, mappingCollection);
    }

    internal static (int?, int?, List<Models.Mapping>?) GetReversedDeterministicHashCodeKey(string facesFileNameExtension, bool idToMappingCollectionAny, Dictionary<int, List<Models.Mapping>> idToMappingCollection, string file)
    {
        int? id;
        int? normalizedPixelPercentage;
        List<Models.Mapping>? mappingCollection;
        string fileName = Path.GetFileName(file);
        if (fileName.Length < 2 || fileName[1..].Contains('-'))
        {
            id = null;
            mappingCollection = null;
            normalizedPixelPercentage = null;
        }
        else
            (id, normalizedPixelPercentage, mappingCollection) = GetReversedDeterministicHashCodeKeysFromSegments(facesFileNameExtension, idToMappingCollectionAny, idToMappingCollection, fileName);
        return new(id, normalizedPixelPercentage, mappingCollection);
    }

    internal static int GetAreaPermille(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 * 1000, 0);
        return result;
    }

}