
using OpenCvSharp;

namespace Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.FixedRectangle;

public class FixedRectangleElement : IGraphicsElement
{
    public bool Editable { get; set; }
    public bool IsGood { get; set; }
    public Vector2 Location { get; set; }
    public Vector2 Offset { get; set; }
    public Vector2 Size { get; set; }

    public void Draw(ICanvas canvas)
    {
        canvas.DrawRectangle(Offset + Location, Size, IsGood);
        canvas.DrawNode(Offset + Location);
        
    }

    public void FillMat(Mat mat)
    {
        var location = Offset + Location;
        Cv2.Rectangle(mat,
                       new Rect((int)location.X, (int)location.Y, (int)Size.X,
                                          (int)Size.Y),
                                  Scalar.White, -1);
    }

    public INode? GetNode(Vector2 position, float thDistance)
    {
        if ((Location + Offset - position).Length() < thDistance)
        {
            return new FixedRectangleAnchorNode(this);
        }
        
        return null;
    }

    public void SetPivot(Vector2 pivot)
    {
        Location += Offset;
        Offset = pivot;
        Location -= Offset;
    }

    public void MovePivot(Vector2 pivot)
    {
        Offset = pivot;
    }

    public FixedRectangleElement()
    {
        Location = Vector2.Zero;
        Size = Vector2.Zero;
        Offset = Vector2.Zero;
    }
}