using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.ArrayHorizontal;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;

using OpenCvSharp;
using System.Runtime.InteropServices;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;

namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;

[Category("AI")]
[IgnoreOperation]
public class ArrayModelSamplerOperation:BaseOperation
{
    private Mat _lastImage;

    public RectangleElement SearchArea { get; set; }
    public string SampleStorage { get; set; } = "samples.storage";
    public Action Sample { get; set; }

    public ArrayModelSamplerOperation()
    {
        SearchArea = new RectangleElement()
        {
            Editable = true,
            Location = Vector2.One * 100,
            Size = new Vector2(100, 100),

        };
        Sample = SaveSample;
    }

    byte[] MatToBytes(Mat mat)
    {
        if (mat.Channels() == 3)
        {
            mat = mat.CvtColor(ColorConversionCodes.BGR2RGB);
        }
        

        IntPtr dataPtr = mat.Data;
        int dataSize = mat.Rows * mat.Cols * mat.ElemSize();
        byte[] byteArray = new byte[dataSize];

        Marshal.Copy(dataPtr, byteArray, 0, dataSize);

        return byteArray;
    }

    private void SaveSample()
    {



        Mat mask = new Mat(_lastImage.Size(), MatType.CV_8UC1, Scalar.Black);
        SearchArea.FillMat(mask);
        var imageBytes = MatToBytes(_lastImage);
        var maskBytes = MatToBytes(mask);
        // Append to the end of storage file
        
        var storagePath=Path.Combine("..\\Data\\AI", SampleStorage);



        using (var file = new System.IO.FileStream(storagePath, System.IO.FileMode.Append))
        {
            // Write size
            file.Write(BitConverter.GetBytes(_lastImage.Width), 0, 4);
            file.Write(BitConverter.GetBytes(_lastImage.Height), 0, 4);
            file.Write(imageBytes, 0, imageBytes.Length);
            // Write mask
            file.Write(maskBytes, 0, maskBytes.Length);
        }
    }

    protected override void InterpretInternal(Context context)
    {
        if (!CheckImageExists(context)) return;
        if(!CheckColorful(context)) return;

        context.GraphicsElements.Add(SearchArea);

        _lastImage=context.ActiveImage.ImageData;
        Result = true;
    }

    
}