using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Align;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Edge;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
using Hawkeye.VisionBuilder.Workflow.Links;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;


namespace Hawkeye.VisionBuilder.Workflow.Operations.PositionDetection.Edges;
[Category("Simple/Edges")]
public class EdgeIntersectionOperation : BaseOperation, IHaveOrigin
{
    private readonly WorkflowList _workflowList;
    private string _status;
    public override string ToString()
    {
        return _status;
    }

    public EdgeIntersectionOperation(WorkflowList workflowList)
    {
        _workflowList = workflowList;
    }

    public Guid ReferenceEdge1 { get; set; }

    public Guid ReferenceEdge2 { get; set; }

    protected override void InterpretInternal(Context context)
    {
        var reference1 = _workflowList.GetById(ReferenceEdge1);
        var reference2 = _workflowList.GetById(ReferenceEdge2);
        if (reference1 == NoOrigin.Instance)
        {
            _status = $"{nameof(ReferenceEdge1)} must be set";
            Result = false;
            return;
        }
        if (reference2 == NoOrigin.Instance)
        {
            _status = $"{nameof(ReferenceEdge2)} must be set";
            Result = false;
            return;
        }
        if (!(reference1 as BaseOperation).Result)
        {
            _status = $"{nameof(ReferenceEdge1)} detection failed";
            Result = false;
            return;
        }
        if (!(reference2 as BaseOperation).Result)
        {
            _status = $"{nameof(ReferenceEdge2)} detection failed";
            Result = false;
            return;
        }

        var edge1 = reference1 as FindEdgeOperation;
        var edge2 = reference2 as FindEdgeOperation;


        //context.GraphicsElements.Add(new EdgeElement()
        //{
        //    Location = edge1.EdgeOrigin.Location,
        //    Rotation = edge1.EdgeOrigin.Rotation
        //});
        //context.GraphicsElements.Add(new EdgeElement()
        //{
        //    Location = edge2.EdgeOrigin.Location,
        //    Rotation = edge2.EdgeOrigin.Rotation
        //});

        if (edge1.EdgeOrigin.Intersection(edge2.EdgeOrigin, out var x, out var y))
        {
            Result = true;
            Origin = new OriginElement()
            {
                Location = new Vector2(x, y)
            };
            _status = $"{Origin.Location}";
            context.GraphicsElements.Add(new AlignElement() { Location = Origin.Location });
            return;
        }
        _status = $"Edges are parallel";
        Result = false;

    }

    public override void Save(BinaryWriter bw)
    {
        bw.Write(Id.ToString());
        bw.Write(ReferenceEdge1.ToString());
        bw.Write(ReferenceEdge2.ToString());
    }

    public override void Load(BinaryReader br)
    {
        Id = new Guid(br.ReadString());
        ReferenceEdge1 = new Guid(br.ReadString());
        ReferenceEdge2 = new Guid(br.ReadString());

    }

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

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

    public OriginElement Origin { get; set; } = OriginElement.Default;
}