using System.Drawing;

namespace View_by_Distance.Shared.Models.Stateless.Methods;

internal abstract class Location
{

    internal static bool Check(int bottom, int left, int right, int top, int zCount, bool throwException)
    {
        bool result = true;
        if (left < 0)
            result = false;
        if (right < 0)
            result = false;
        if (right < left)
            result = false;
        if (top < 0)
            result = false;
        if (bottom < 0)
            result = false;
        if (bottom < top)
            result = false;
        if (zCount < 0)
            result = false;
        if (throwException && !result)
            throw new Exception();
        return result;
    }

    internal static bool Check(int bottom, int height, int left, int right, int top, int width, int zCount, bool throwException)
    {
        bool result = true;
        if (bottom > height)
            result = false;
        if (left > width)
            result = false;
        if (right > width)
            result = false;
        if (top > height)
            result = false;
        if (zCount < 0)
            result = false;
        if (result)
            result = Check(bottom, left, right, top, zCount, throwException);
        if (throwException && !result)
            throw new Exception();
        return result;
    }

    internal static string GetLeftPadded(int locationDigits, string value)
    {
        string result;
        if (value.Length == locationDigits)
            result = value;
        else if (value.Length > locationDigits)
            result = value[..locationDigits];
        else
            result = value.PadLeft(locationDigits, '0');
        return result;
    }

    internal static (decimal?, decimal?, decimal?, decimal?) GetHeightLeftTopWidth(int bottom, int height, int left, int right, int top, int width, int zCount)
    {
        (decimal?, decimal?, decimal?, decimal?) result;
        bool verified = Check(bottom, height, left, right, top, width, zCount, throwException: false);
        decimal t = top;
        decimal l = left;
        decimal w = right - l;
        decimal h = bottom - t;
        decimal xHeightPercentageFactored = h / height;
        decimal xLeftPercentageFactored = l / width;
        decimal xTopPercentageFactored = t / height;
        decimal xWidthPercentageFactored = w / width;
        if (!verified)
            result = new(null, null, null, null);
        else
            result = new(xHeightPercentageFactored, xLeftPercentageFactored, xTopPercentageFactored, xWidthPercentageFactored);
        return result;
    }

    internal static int GetNormalizedRectangle(int bottom, int height, int left, int locationDigits, int right, int top, int width, int zCount)
    {
        int result;
        string check;
        bool verified = Check(bottom, height, left, right, top, width, zCount, throwException: false);
        int checksum = left > top ? 4 : 8;
        if (!verified)
            check = string.Concat(checksum, new string('0', locationDigits - 1));
        else
        {
            decimal factor = 100;
            int factorMinusOne = (int)factor - 1;
            // int.MaxPercentage = 21 47 48 36 47;
            int length = (locationDigits - 1) / 4;
            // Rectangle rectangle=new(x, y, width, h);
            decimal x = left / (decimal)width * factor;
            decimal y = top / (decimal)height * factor;
            decimal w = (right - left) / (decimal)width * factor;
            decimal h = (bottom - top) / (decimal)height * factor;
            string xPadded = x < factor ? ILocation.GetLeftPadded(length, (int)x) : ILocation.GetLeftPadded(length, factorMinusOne);
            string yPadded = y < factor ? ILocation.GetLeftPadded(length, (int)y) : ILocation.GetLeftPadded(length, factorMinusOne);
            string widthPadded = w < factor ? ILocation.GetLeftPadded(length, (int)w) : ILocation.GetLeftPadded(length, factorMinusOne);
            string heightPadded = h < factor ? ILocation.GetLeftPadded(length, (int)h) : ILocation.GetLeftPadded(length, factorMinusOne);
            check = string.Concat(checksum, xPadded, yPadded, widthPadded, heightPadded);
        }
        long value = long.Parse(check);
        if (value > int.MaxValue)
            throw new Exception();
        result = (int)value;
        return result;
    }

    internal static (int?, int?) GetXY(int locationDigits, int locationFactor, int width, int height, string normalizedRectangle)
    {
        int? x;
        int? y;
        int center = 2;
        decimal factor = locationFactor;
        int each = (locationDigits - 1) / center;
        string segmentA = normalizedRectangle[..each];
        string segmentB = normalizedRectangle[each..^1];
        if (!int.TryParse(segmentA, out int xNormalized) || !int.TryParse(segmentB, out int yNormalized))
        {
            x = null;
            y = null;
        }
        else
        {
            decimal xValue = xNormalized / factor * width;
            decimal yValue = yNormalized / factor * height;
            x = (int)Math.Round(xValue, 0);
            y = (int)Math.Round(yValue, 0);
        }
        return new(x, y);
    }

    internal static int GetConfidencePercent(int faceConfidencePercent, double[] rangeFaceConfidence, double confidence)
    {
        int result = (int)(confidence / rangeFaceConfidence[1] * faceConfidencePercent);
        return result;
    }

    private static Rectangle? GetRectangle(int locationDigits, int height, string normalizedRectangle, int width)
    {
        Rectangle? result;
        int length = (locationDigits - 1) / 4;
        string[] segments = new string[]
        {
            normalizedRectangle[..1],
            normalizedRectangle.Substring(1, length),
            normalizedRectangle.Substring(3, length),
            normalizedRectangle.Substring(5, length),
            normalizedRectangle.Substring(7, length)
        };
        if (string.Join(string.Empty, segments) != normalizedRectangle)
            result = null;
        else
        {
            if (!int.TryParse(segments[1], out int xNormalized) || !int.TryParse(segments[2], out int yNormalized) || !int.TryParse(segments[3], out int wNormalized) || !int.TryParse(segments[4], out int hNormalized))
                result = null;
            else
            {
                decimal factor = 100;
                result = new((int)(xNormalized / factor * width), (int)(yNormalized / factor * height), (int)(wNormalized / factor * width), (int)(hNormalized / factor * height));
            }
        }
        return result;
    }

    internal static Rectangle GetRectangle(Rectangle checkRectangle, int locationDigits, int locationFactor, int normalizedRectangleValue, int height, int width, bool useOldWay)
    {
        Rectangle? result;
        string normalizedRectangle = normalizedRectangleValue.ToString();
        if (normalizedRectangle.Length != locationDigits)
            throw new NotImplementedException();
        if (!useOldWay)
        {
            result = GetRectangle(locationDigits, height, normalizedRectangle, width);
            if (result is null)
                throw new NullReferenceException(nameof(result));
        }
        else
        {
            (int? x, int? y) = GetXY(locationDigits, locationFactor, width, height, normalizedRectangle);
            if (x is null || y is null)
                throw new Exception();
            result = new(x.Value - (checkRectangle.Width / 2), y.Value - (checkRectangle.Height / 2), checkRectangle.Width, checkRectangle.Height);
        }
        return result.Value;
    }

}