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

using OpenCvSharp;

namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;

[Category("Morphology")]
public class CutOperation:BaseOperation
{

    private readonly WorkflowList _workflowList;
    public Guid ReferencePoint { get; set; } = new Guid();
    
    public RectangleElement? SearchArea { get; set; } = null;

    public CutOperation(WorkflowList workflowList)
    {
        _workflowList = workflowList;
        SearchArea = new RectangleElement()
        {
            Editable = true,
            Location = Vector2.One * 5,
            Size = new Vector2(100, 100),
            IsGood = true
        };

        CanHaveProcessingError = false;

    }

    public override void SetParameters(Dictionary<string, object> parameters)
    {
        base.SetParameters(parameters);

        SearchArea.SetPivot(_workflowList.GetOriginById(ReferencePoint).Origin.Location);
    }

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

       


        SearchArea.MovePivot(_workflowList.GetOriginById(ReferencePoint).Origin.Location);
        context.GraphicsElements.Add(SearchArea);

        var img = context.ActiveImage.ImageData;
        var mat = new Mat(img.Size(), img.Type(), Scalar.Black);
        SearchArea.FillMat(mat);

        var cut = img.BitwiseAnd(mat).ToMat();

        context.ActiveImage=new HawkeyeImage()
        {
            ImageData = cut
        };

        Status="Cut";
        Result = true;

    }

    public override void Save(BinaryWriter bw)
    {
        base.Save(bw);
        bw.Write(ReferencePoint.ToString());

        SearchArea.Save(bw);
    }

    public override void Load(BinaryReader br)
    {
        base.Load(br);
        ReferencePoint = new Guid(br.ReadString());
        SearchArea = new RectangleElement();

        SearchArea.Load(br);
    }

    public override void Save(Dictionary<string, object> dict)
    {
        base.Save(dict);
        dict[nameof(ReferencePoint)] = ReferencePoint.ToString();
        
        // Save SearchArea properties manually
        dict["SearchArea_Editable"] = SearchArea.Editable;
        dict["SearchArea_IsGood"] = SearchArea.IsGood;
        dict["SearchArea_LocationX"] = SearchArea.Location.X;
        dict["SearchArea_LocationY"] = SearchArea.Location.Y;
        dict["SearchArea_OffsetX"] = SearchArea.Offset.X;
        dict["SearchArea_OffsetY"] = SearchArea.Offset.Y;
        dict["SearchArea_SizeX"] = SearchArea.Size.X;
        dict["SearchArea_SizeY"] = SearchArea.Size.Y;
    }

    public override void Load(Dictionary<string, object> dict)
    {
        base.Load(dict);
        if (dict.ContainsKey(nameof(ReferencePoint)))
            ReferencePoint = new Guid(dict[nameof(ReferencePoint)].ToString());
        
        // Load SearchArea properties manually
        if (dict.ContainsKey("SearchArea_Editable") || dict.ContainsKey("SearchArea_IsGood") || dict.ContainsKey("SearchArea_LocationX"))
        {
            SearchArea = new RectangleElement();
            if (dict.ContainsKey("SearchArea_Editable"))
                SearchArea.Editable = Convert.ToBoolean(dict["SearchArea_Editable"]);
            if (dict.ContainsKey("SearchArea_IsGood"))
                SearchArea.IsGood = Convert.ToBoolean(dict["SearchArea_IsGood"]);
            if (dict.ContainsKey("SearchArea_LocationX") && dict.ContainsKey("SearchArea_LocationY"))
                SearchArea.Location = new Vector2(Convert.ToSingle(dict["SearchArea_LocationX"]), Convert.ToSingle(dict["SearchArea_LocationY"]));
            if (dict.ContainsKey("SearchArea_OffsetX") && dict.ContainsKey("SearchArea_OffsetY"))
                SearchArea.Offset = new Vector2(Convert.ToSingle(dict["SearchArea_OffsetX"]), Convert.ToSingle(dict["SearchArea_OffsetY"]));
            if (dict.ContainsKey("SearchArea_SizeX") && dict.ContainsKey("SearchArea_SizeY"))
                SearchArea.Size = new Vector2(Convert.ToSingle(dict["SearchArea_SizeX"]), Convert.ToSingle(dict["SearchArea_SizeY"])); 
        }
    }
}