using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace AlienAttack.MonoGame.View;

public class ViewTransform(Game game, GraphicsDeviceManager graphics)
{
    public readonly GameWindow Window = game.Window;
    public readonly ContentManager Content = game.Content;
    public readonly GraphicsDeviceManager Graphics = graphics;
    public readonly GraphicsDevice GraphicsDevice = game.GraphicsDevice;
    public readonly SpriteBatch SpriteBatch;

    public float ScaleX => (float)WindowWidth / Graphics.PreferredBackBufferWidth;
    public float ScaleY => (float)WindowHeight / Graphics.PreferredBackBufferHeight;
    public float ScaleMin => MathHelper.Min(ScaleX, ScaleY);

    public float OffsetX => KeepAspectRatio ? MathHelper.Max(ScaleX - ScaleY, 0) * ScreenWidth : 0;
    public float OffsetY => KeepAspectRatio ? MathHelper.Max(ScaleY - ScaleX, 0) * ScreenHeight : 0;

    public bool KeepAspectRatio = true;

    public int ScreenWidth
    {
        get
        {
            return Graphics.PreferredBackBufferWidth;
        }
    }

    public int ScreenHeight
    {
        get
        {
            return Graphics.PreferredBackBufferHeight;
        }
    }

    public int WindowWidth
    {
        get
        {
            return Window.ClientBounds.Width;
        }
    }

    public int WindowHeight
    {
        get
        {
            return Window.ClientBounds.Height;
        }
    }

    public Matrix ViewMatrix
    {
        get
        {
            if (KeepAspectRatio)
            {
                return Matrix.CreateScale(ScaleMin, ScaleMin, 1.0f) * Matrix.CreateTranslation(OffsetX / 2, OffsetY / 2, 0);
            }
            else
            {
                return Matrix.CreateScale(ScaleX, ScaleY, 1.0f);
            }
        }
    }
}