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

namespace Hawkeye.VisionBuilder.Workflow.Operations.Basic;

[Category("Basic")]
public class CannyOperation:BaseOperation
{
    public int Threshold1 { get; set; } = 100;
    public int Threshold2 { get; set; } = 200;

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

        context.ActiveImage = new HawkeyeImage()
        {
            ImageData = context.ActiveImage.ImageData.Canny(Threshold1, Threshold2)
        };

        Status = $"Canny with Threshold1={Threshold1} and Threshold2={Threshold2}";
        Result = true;
    }

    public override void Save(BinaryWriter bw)
    {
        base.Save(bw);
        bw.Write(Threshold1);
        bw.Write(Threshold2);
    }

    public override void Load(BinaryReader br)
    {
        base.Load(br);
        Threshold1 = br.ReadInt32();
        Threshold2 = br.ReadInt32();
    }

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

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