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

namespace RecrownedAthenaeum.Render
{
    /// <summary>
    /// A generic camera. Functions in 3D.
    /// </summary>
    public class BasicCamera
    {
        /// <summary>
        /// The scale for the world.
        /// </summary>
        public float worldScale = 1f;

        /// <summary>
        /// Current position in the world.
        /// </summary>
        public Vector3 position;

        /// <summary>
        /// The place the 3D camera is looking at.
        /// </summary>
        public Vector3 lookAt;

        /// <summary>
        /// The direction up is for the camera.
        /// </summary>
        public Vector3 upDirection;

        /// <summary>
        /// The transform matrix representing the world (rotation and translations of the original world).
        /// </summary>
        public Matrix worldMatrix;

        /// <summary>
        /// The view matrix that describes where the camera looks.
        /// </summary>
        public Matrix ViewMatrix { get; protected set; }

        /// <summary>
        /// The projection matrix.
        /// </summary>
        public Matrix projectionMatrix;

        /// <summary>
        /// A basic effect that will be updated with the correct matrice information everytime <see cref="Apply"/> is called. Can be null and thus will not be used.
        /// </summary>
        public BasicEffect basicEffect;

        /// <summary>
        /// Constructs 3D camera with an orthographic projection matrix with dimensions of graphics devices viewport. All changes to matrices should have apply called after changes.
        /// </summary>
        /// <param name="basicEffect">A basic effect that will be updated with the correct matrice information everytime <see cref="Apply"/> is called. Can be null and thus will not be used.</param>
        public BasicCamera(BasicEffect basicEffect = null)
        {
            worldMatrix = Matrix.Identity;
            lookAt = Vector3.Forward;
            upDirection = Vector3.Up;

            projectionMatrix = Matrix.Identity;
            this.basicEffect = basicEffect;
            Apply();
        }

        /// <summary>
        /// Applies the changes to the fields and properties of the camera.
        /// </summary>
        public virtual void Apply()
        {
            ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection);
            worldMatrix *= Matrix.CreateScale(worldScale);

            if (basicEffect != null)
            {
                basicEffect.World = worldMatrix;
                basicEffect.Projection = projectionMatrix;
                basicEffect.View = ViewMatrix;
            }
        }

        /// <summary>
        /// Moves camera by the given amount.
        /// </summary>
        /// <param name="move">A <see cref="Vector3"/> that contains how much in each direction to move.</param>
        public void MoveCamera(Vector3 move)
        {
            position += move;
            Apply();
        }
    }
}
