using TraceCad.Core.Geometry;
using TraceCad.Core.Model;

namespace TraceCad.Core.Commands;

public sealed class RotateDocumentCommand(double angleDeg, Point2 pivot) : IDocumentCommand
{
    public string Name => "Rotate Document";

    public double AngleDeg { get; } = angleDeg;

    public Point2 Pivot { get; } = pivot;

    public void Apply(SketchDocument document) => Rotate(document, AngleDeg);

    public void Revert(SketchDocument document) => Rotate(document, -AngleDeg);

    private void Rotate(SketchDocument document, double angleDeg)
    {
        for (var i = 0; i < document.Entities.Count; i++)
        {
            document.Entities[i] = GeometryTransform.Rotate(document.Entities[i], Pivot, angleDeg);
        }

        if (document.Reference is not null)
        {
            document.Reference = GeometryTransform.Rotate(document.Reference, Pivot, angleDeg);
        }
    }
}
