using OpenCvSharp;

namespace InterpreterLibrary.Interpreters
{
    internal static class Utils
    {
        private static bool IsBlackPixel(byte pixelColor)
        {
            return pixelColor == 0;
        }

        public static void FuzzyTrim(Mat bitmap, Rect rectangle)
        {

            var y1 = rectangle.Top;
            var y2 = rectangle.Bottom;
            const int maxIterations = 3;
            const double threshold = 0.5;

            const byte white = byte.MaxValue;
            var indexer = bitmap.GetGenericIndexer<byte>();
            
            for (var y = rectangle.Top; y < rectangle.Bottom; ++y)
            {
                y1 = y;
                var blackPixels = 0;
                for (var x = rectangle.Left; x < rectangle.Right; ++x)
                {
                    if (IsBlackPixel(indexer[y, x]))
                    {
                        ++blackPixels;
                    }
                }
                if ((double)blackPixels / rectangle.Width > threshold)
                {
                    for (var x = rectangle.Left; x < rectangle.Right; ++x)
                    {
                        indexer[y, x] = white;
                    }
                }
                else
                {
                    break;
                }
            }

            
            for (var y = rectangle.Bottom - 1; y >= rectangle.Top; --y)
            {
                y2 = y + 1;
                var blackPixels = 0;
                for (var x = rectangle.Left; x < rectangle.Right; ++x)
                {
                    if (IsBlackPixel(indexer[y, x]))
                    {
                        ++blackPixels;
                    }
                }
                if ((double)blackPixels / rectangle.Width > threshold)
                {
                    for (var x = rectangle.Left; x < rectangle.Right; ++x)
                    {
                        indexer[y, x] = white;
                    }
                }
                else
                {
                    break;
                }
            }

            
            for (var x = rectangle.Left; x < rectangle.Left + maxIterations; ++x)
            {
                var blackPixels = 0;
                for (var y = y1; y < y2; ++y)
                {
                    if (IsBlackPixel(indexer[y, x]))
                    {
                        ++blackPixels;
                    }
                }
                if ((double)blackPixels / rectangle.Height > threshold)
                {
                    for (var y = rectangle.Top; y < rectangle.Bottom; ++y)
                    {
                        indexer[y, x] = white;
                    }
                }
                else
                {
                    break;
                }
            }

            
            for (var x = rectangle.Right - 1; x >= rectangle.Right - 1 - maxIterations; --x)
            {
                var blackPixels = 0;
                for (var y = y1; y < y2; ++y)
                {
                    if (IsBlackPixel(indexer[y, x]))
                    {
                        ++blackPixels;
                    }
                }

                if ((double)blackPixels / rectangle.Height > threshold)
                {
                    for (var y = rectangle.Top; y < rectangle.Bottom; ++y)
                    {
                        indexer[y, x] = white;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
}
