using Avalonia;
using TraceCad.Core.Geometry;

namespace TraceCad.App;

public sealed class ViewTransform
{
    public double Scale { get; private set; } = 3.0;

    public Vector Offset { get; private set; } = new(80.0, 80.0);

    public Point ModelToScreen(Point2 model) =>
        new((model.X * Scale) + Offset.X, (model.Y * Scale) + Offset.Y);

    public Point2 ScreenToModel(Point screen) =>
        new((screen.X - Offset.X) / Scale, (screen.Y - Offset.Y) / Scale);

    public void Pan(Vector screenDelta)
    {
        Offset += screenDelta;
    }

    public void ZoomAt(Point screenPoint, double factor)
    {
        var before = ScreenToModel(screenPoint);
        Scale = Math.Clamp(Scale * factor, 0.05, 1000.0);
        Offset = new Vector(screenPoint.X - (before.X * Scale), screenPoint.Y - (before.Y * Scale));
    }

    public void Reset()
    {
        Scale = 3.0;
        Offset = new Vector(80.0, 80.0);
    }
}
