using TraceCad.Core.Model;

namespace TraceCad.Core.Commands;

public sealed class ConstraintEditCommand(
    IReadOnlyList<EntityReplacement> replacements,
    SketchConstraint constraint) : IDocumentCommand
{
    public string Name => "Apply Constraint";

    public IReadOnlyList<EntityReplacement> Replacements { get; } = replacements;

    public SketchConstraint Constraint { get; } = constraint;

    public void Apply(SketchDocument document)
    {
        foreach (var replacement in Replacements)
        {
            document.ReplaceEntity(replacement.Replacement);
        }

        document.AddConstraint(Constraint);
    }

    public void Revert(SketchDocument document)
    {
        document.RemoveConstraint(Constraint.Id);
        for (var i = Replacements.Count - 1; i >= 0; i--)
        {
            document.ReplaceEntity(Replacements[i].Original);
        }
    }
}

public sealed record EntityReplacement(Entity Original, Entity Replacement);
