using Hawkeye.VisionBuilder.Workflow.Configuration;
using Hawkeye.VisionBuilder.Workflow.Datatypes;
using Hawkeye.VisionBuilder.Workflow.Links;


using OpenCvSharp;
using OpenCvSharp.Extensions;
using Rectangle = Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle.RectangleElement;

namespace Hawkeye.VisionBuilder.Workflow.Operations;

public class GetImageOperation:BaseOperation,IHaveImage
{
    private readonly WorkflowList _workflowList;

    
    
    public GetImageOperation(WorkflowList workflowList)
    {
        _workflowList = workflowList;
    }

    public HawkeyeImage? Image { get; set; }


    protected override void InterpretInternal(Context context)
    {

        var image = _workflowList.ImageSource.GetImage(context.CancellationToken).Result;
        
        
       
        
        context.ActiveImage = new HawkeyeImage(){ImageData = image};
        context.LastCameraImage = context.ActiveImage;
        Image = context.ActiveImage;

        if (CheckImageExists(context))
        {
            Status= $"Image size: {context.ActiveImage.ImageData.Width}x{context.ActiveImage.ImageData.Height}";
            Result = true;
        }
        else
        {
            Status = "No image";
            Result = false;
        }
        
    }


    /// <summary>
    /// Save the operation to the binary writer
    /// </summary>
    /// <param name="bw"></param>
    public override void Save(BinaryWriter bw)
    {
        bw.Write(Id.ToString());
        bw.Write(Label);
    }

    /// <summary>
    /// Load the operation from the binary reader
    /// </summary>
    /// <param name="br"></param>
    public override void Load(BinaryReader br)
    {
        Id = new Guid(br.ReadString());
        Label = br.ReadString();
    }

    /// <summary>
    /// Save the operation to the dictionary
    /// </summary>
    /// <param name="dict"></param>
    public override void Save(Dictionary<string, object> dict)
    {
        base.Save(dict);
    }

    /// <summary>
    /// Load the operation from the dictionary
    /// </summary>
    /// <param name="dict"></param>
    public override void Load(Dictionary<string, object> dict)
    {
        base.Load(dict);
    }
}