using TraceCad.Core.Commands;
using TraceCad.Core.Geometry;
using TraceCad.Core.Model;
using Xunit;

namespace TraceCad.Tests;

public sealed class TransformTests
{
    [Fact]
    public void RotateDocumentRotatesEntitiesAndReference()
    {
        var document = SketchDocument.CreateDefault();
        document.AddEntity(new LineEntity(
            Guid.NewGuid(),
            Layer.Cut.Name,
            new Point2(1, 0),
            new Point2(1, 1)));
        document.Reference = new ReferenceImage(
            "reference.png",
            0.5,
            true,
            new ReferenceTransform(1, 0, 0.125, 0.125, 0));

        var command = new RotateDocumentCommand(90, Point2.Origin);
        command.Apply(document);

        var line = Assert.IsType<LineEntity>(document.Entities[0]);
        Assert.Equal(0.0, line.Start.X, 9);
        Assert.Equal(1.0, line.Start.Y, 9);
        Assert.Equal(-1.0, line.End.X, 9);
        Assert.Equal(1.0, line.End.Y, 9);
        Assert.NotNull(document.Reference);
        Assert.Equal(0.0, document.Reference.Transform.OriginX, 9);
        Assert.Equal(1.0, document.Reference.Transform.OriginY, 9);
        Assert.Equal(90.0, document.Reference.Transform.RotationDeg, 9);
    }

    [Fact]
    public void RotateDocumentReverts()
    {
        var document = SketchDocument.CreateDefault();
        var line = new LineEntity(
            Guid.NewGuid(),
            Layer.Cut.Name,
            new Point2(1, 0),
            new Point2(1, 1));
        document.AddEntity(line);

        var command = new RotateDocumentCommand(37, new Point2(5, 5));
        command.Apply(document);
        command.Revert(document);

        var reverted = Assert.IsType<LineEntity>(document.Entities[0]);
        Assert.Equal(line.Start.X, reverted.Start.X, 9);
        Assert.Equal(line.Start.Y, reverted.Start.Y, 9);
        Assert.Equal(line.End.X, reverted.End.X, 9);
        Assert.Equal(line.End.Y, reverted.End.Y, 9);
    }
}
