using Hawkeye.VisionBuilder.Workflow.Datatypes;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;

namespace Hawkeye.VisionBuilder.Workflow.Operations.Image;

[Category("Image")]
public class ImageSizeProportionOperation:BaseOperation
{
    public ImageSizeProportionOperation()
    {
        CanHaveProcessingError = false;
    }
    
    public double Width { get; set; } = 0.25;
    public double Height { get; set; } = 0.25;

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

        context.ActiveImage = new HawkeyeImage()
        {
            ImageData = context.ActiveImage!.ImageData.Resize(new Size(0,0), Width, Height)
        };

        Status = $"Image resized to {context.ActiveImage.ImageData.Width}x{context.ActiveImage.ImageData.Height}";
        Result = true;
    }

    public override void Save(BinaryWriter bw)
    {
        base.Save(bw);
        bw.Write(Width);
        bw.Write(Height);
    }

    public override void Load(BinaryReader br)
    {
        base.Load(br);
        Width = br.ReadDouble();
        Height = br.ReadDouble();
    }

    public override void Save(Dictionary<string, object> dict)
    {
        base.Save(dict);
        dict[nameof(Width)] = Width;
        dict[nameof(Height)] = Height;
    }

    public override void Load(Dictionary<string, object> dict)
    {
        base.Load(dict);
        if (dict.ContainsKey(nameof(Width)))
            Width = Convert.ToDouble(dict[nameof(Width)]);
        if (dict.ContainsKey(nameof(Height)))
            Height = Convert.ToDouble(dict[nameof(Height)]);
    }
}