using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecrownedAthenaeum.Render
{
    /// <summary>
    /// A simple utility object that will start and end a scissor setup given a <see cref="ConsistentSpriteBatch"/> to work with.
    /// </summary>
    public class BasicScissor
    {
        ConsistentSpriteBatch spriteBatch;
        RasterizerState scissorRasterizer;
        Rectangle scissorRectangle;

        RasterizerState originalRasterizer;
        Rectangle originalScissor;

        /// <summary>
        /// Initializes the basic scissor.
        /// </summary>
        public BasicScissor()
        {
            scissorRasterizer = new RasterizerState();
            scissorRasterizer.MultiSampleAntiAlias = false;
            scissorRasterizer.ScissorTestEnable = true;

        }

        /// <summary>
        /// Begins the <see cref="ConsistentSpriteBatch"/> with scissoring in mind.
        /// </summary>
        /// <param name="scissorRectangle">The rectangle to use to outline the scissor.</param>
        /// <param name="consistentSpriteBatch">The consistent sprite batch to use for this process.</param>
        public void Begin(Rectangle scissorRectangle, ConsistentSpriteBatch consistentSpriteBatch)
        {
            originalRasterizer = consistentSpriteBatch.rasterizerState;
            this.scissorRectangle = scissorRectangle;

            spriteBatch = consistentSpriteBatch;
            spriteBatch.Begin(rasterizerState: scissorRasterizer);
        }

        /// <summary>
        /// Ends the scissor state of the spritebatch.
        /// </summary>
        public void End()
        {
            originalScissor = spriteBatch.GraphicsDevice.ScissorRectangle;
            spriteBatch.GraphicsDevice.ScissorRectangle = scissorRectangle;
            spriteBatch.End();
            spriteBatch.rasterizerState = originalRasterizer;
            spriteBatch.GraphicsDevice.ScissorRectangle = originalScissor;
        }
    }
}
