namespace View_by_Distance.Shared.Models.Stateless.Methods;

internal abstract class Mapping
{

    internal static (string?, string?, bool?) GetSegments(string facesFileNameExtension, FilePath filePath)
    {
        string? extensionLowered;
        string? wholePercentages;
        bool? needsFacesFileNameExtension;
        string[] segments = filePath.Name.Split('.');
        if (segments.Length < 4 || $".{segments[3]}" != facesFileNameExtension)
        {
            extensionLowered = null;
            wholePercentages = null;
            needsFacesFileNameExtension = null;
        }
        else
        {
            extensionLowered = $".{segments[2]}";
            wholePercentages = segments[1];
            needsFacesFileNameExtension = segments.Length == 3;
        }
        return new(wholePercentages, extensionLowered, needsFacesFileNameExtension);
    }

    private static int? GetConvertedFromSegments(string facesFileNameExtension, FilePath filePath)
    {
        int? result;
        (string? WholePercentages, string? ExtensionLowered, bool? Check) segments = GetSegments(facesFileNameExtension, filePath);
        if (string.IsNullOrEmpty(segments.WholePercentages) || string.IsNullOrEmpty(segments.ExtensionLowered) || segments.Check is null)
            result = null;
        else if (!int.TryParse(segments.WholePercentages, out int wholePercentages))
            result = null;
        else
            result = wholePercentages;
        return result;
    }

    internal static int? GetWholePercentages(string facesFileNameExtension, FilePath filePath)
    {
        int? wholePercentages;
        if (filePath.Name.Length < 2 || filePath.Name[1..].Contains('-'))
            wholePercentages = null;
        else
            wholePercentages = GetConvertedFromSegments(facesFileNameExtension, filePath);
        return wholePercentages;
    }

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

}