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

namespace Hawkeye.VisionBuilder.Workflow.Operations.Image;

[Category("Image")]
public class GetChannelOperation: BaseOperation
{
    public int Channel { get; set; }

    public GetChannelOperation()
    {
        CanHaveProcessingError = false;
    }

    protected override void InterpretInternal(Context context)
    {
        var channel = context.ActiveImage.ImageData.ExtractChannel(Channel);
        context.ActiveImage = new HawkeyeImage() {ImageData = channel};
    }

    public override string ToString()
    {
        return $"Get channel {Channel}";
    }

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

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

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

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