using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;

namespace Hawkeye.VisionBuilder.Workflow.Operations.Memory;

[Category("Memory")]
public class ImageFromMemoryOperation:BaseOperation
{
    public ImageFromMemoryOperation()
    {
        CanHaveProcessingError = false;
    }

    public string SlotName { get; set; } = "Image";


    protected override void InterpretInternal(Context context)
    {
        if (context.Memory.ContainsKey(SlotName))
        {
            context.ActiveImage = context.Memory[SlotName];
            Status = "Image from slot "+SlotName;
            Result = true;
        }
        else
        {
            Status = "Slot not found";
            Result = false;
        }
    }

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

    public override void Load(BinaryReader br)
    {
        base.Load(br);
        SlotName = br.ReadString();
    }

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

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