using Avalonia.Input;
using Avalonia.Media;
using TraceCad.Core.Commands;
using TraceCad.Core.Geometry;
using TraceCad.Core.Model;

namespace TraceCad.App.Tools;

public sealed class ArcRadiusTool : ISketchTool
{
    public string Name => "Arc Radius";

    public string Prompt => "Arc Radius: choose an arc";

    public void PointerDown(ToolContext context, Point2 modelPoint)
    {
        if (context.HitTest(modelPoint, 10.0) is not ArcEntity arc)
        {
            context.SetStatus(Prompt);
            return;
        }

        var measurement = new SketchConstraint(
            Guid.NewGuid(),
            ConstraintType.Radius,
            new[] { arc.Id },
            arc.Radius);
        context.Commands.Execute(
            context.Document,
            new ConstraintEditCommand(Array.Empty<EntityReplacement>(), measurement));
        context.SetSelected(arc);
        context.SetStatus($"Arc Radius: {arc.Radius:0.###} mm");
        context.Invalidate();
    }

    public void PointerMove(ToolContext context, Point2 modelPoint)
    {
    }

    public void PointerUp(ToolContext context, Point2 modelPoint)
    {
    }

    public void KeyDown(ToolContext context, Key key)
    {
    }

    public void Cancel(ToolContext context)
    {
        context.SetStatus(Prompt);
        context.Invalidate();
    }

    public void RenderOverlay(DrawingContext context, ToolRenderContext renderContext)
    {
    }
}
