using Microsoft.Graphics.Canvas;
using System.Numerics;
using Windows.Foundation;

namespace Sonex.Client.Controls.SceneCanvas;

public readonly struct SceneCanvasDrawContext
{
    private readonly Matrix3x2 _worldToScreen;
    private readonly Matrix3x2 _screenToWorld;

    internal SceneCanvasDrawContext(
        CanvasDrawingSession drawingSession,
        Rect visibleWorldBoundsMeters,
        Rect viewportPixels,
        float zoomPixelsPerMeter,
        Matrix3x2 worldToScreen,
        Matrix3x2 screenToWorld)
    {
        DrawingSession = drawingSession;
        VisibleWorldBoundsMeters = visibleWorldBoundsMeters;
        ViewportPixels = viewportPixels;
        ZoomPixelsPerMeter = zoomPixelsPerMeter;
        _worldToScreen = worldToScreen;
        _screenToWorld = screenToWorld;
    }

    public CanvasDrawingSession DrawingSession { get; }

    public Rect VisibleWorldBoundsMeters { get; }

    public Rect ViewportPixels { get; }

    public float ZoomPixelsPerMeter { get; }

    public Vector2 WorldToScreen(Vector2 worldMeters)
    {
        return Vector2.Transform(worldMeters, _worldToScreen);
    }

    public Vector2 ScreenToWorld(Vector2 screenPixels)
    {
        return Vector2.Transform(screenPixels, _screenToWorld);
    }
}
