/// Author : Maxime Rohmer
/// Date : 09/06/2023
/// File : DriverDrsWindow.cs
/// Brief : Window containing DRS related method and infos
/// Version : Beta 1.0

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tesseract;

namespace TrackTrends
{
    public class DriverDrsWindow:Window
    {
        private static int EmptyDrsGreenValue = -1;
        private static Random rnd = new Random();
        public DriverDrsWindow(Bitmap image, Rectangle bounds,bool generateEngine = true) : base(image, bounds,generateEngine)
        {
            Name = "DRS";
        }
        /// <summary>
        /// Method that will decode the content of the window
        /// </summary>
        /// <returns>returns a boolean (true = DRS OPEN, false = DRS CLOSED)</returns>
        public override object DecodePng()
        {
            bool result = false;

            //DEBUG
            //WindowImage.Save("./DRS/"+rnd.Next(0,99999)+".png");

            int greenValue = GetGreenPixels();
            if (EmptyDrsGreenValue == -1)
                EmptyDrsGreenValue = greenValue;

            if (greenValue > EmptyDrsGreenValue + EmptyDrsGreenValue / 100 * 30)
                result = true;

            return result;
        }
        /// <summary>
        /// Method that will get the green pixel proportion in the image, this can be used to determin if the DRS has been actuated
        /// </summary>
        /// <returns>The number of clearely green pixels</returns>
        private unsafe int GetGreenPixels()
        {
            int tot = 0;

            Bitmap bmp = WindowImage;
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
            int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;

            unsafe
            {
                byte* ptr = (byte*)bmpData.Scan0.ToPointer();
                for (int y = 0; y < bmp.Height; y++)
                {
                    byte* currentLine = ptr + (y * bmpData.Stride);
                    for (int x = 0; x < bmp.Width; x++)
                    {
                        byte* pixel = currentLine + (x * bytesPerPixel);

                        byte blue = pixel[0];
                        byte green = pixel[1];
                        byte red = pixel[2];

                        if (green > blue * 1.5 && green > red * 1.5)
                        {
                            tot++;
                        }
                    }
                }
            }
            bmp.UnlockBits(bmpData);

            return tot;
        }
        /// <summary>
        /// This method is used to lock on where exactly the DRS window is
        /// </summary>
        /// <returns>Returns a rectangle containing the DRS</returns>
        public Rectangle GetBox()
        {
            var tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage));
            Engine.SetVariable("tessedit_char_whitelist", "");
            Page page = Engine.Process(tessImage);

            using (var iter = page.GetIterator())
            {
                iter.Begin();
                do
                {
                    Rect boundingBox;

                    // Get the bounding box for the current element
                    if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox))
                    {
                        page.Dispose();
                        return new Rectangle(boundingBox.X1, boundingBox.X2, boundingBox.Width, boundingBox.Height);
                    }
                } while (iter.Next(PageIteratorLevel.Word));

                page.Dispose();
                return new Rectangle(0, 0, 0, 0);
            }
        }
    }
}
