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

namespace MonoGame.FourEngine
{
    public class SpriteDrawEventArgs : EventArgs
    {
        public object Sender { get; protected set; }

        /// <summary>
        /// The drawing bounds on screen
        /// </summary>
        public Rectangle DestinationRectangle { get; set; }

        /// <summary>
        /// An optional region on the texture which will be rendered. If null - draws full texture
        /// </summary>
        public Rectangle SourceRectangle { get; set; }
        
        /// <summary>
        /// A color mask
        /// </summary>
        public Color Color { get; set; }

        /// <summary>
        /// A rotation of this sprite
        /// </summary>
        public float Rotation { get; set; }

        /// <summary>
        /// Center of the rotation. 0,0 by default
        /// </summary>
        public Vector2 Origin { get; set; }

        /// <summary>
        /// Modificators for drawing. Can be combined
        /// </summary>
        public SpriteEffects SpriteEffects { get; set; }

        public SpriteDrawEventArgs(object sender)
        {
            Sender = sender;
        }

        public SpriteDrawEventArgs Rotate(float value, Vector2 center)
        {
            Origin = new Vector2(SourceRectangle.Width, SourceRectangle.Height) * center;
            DestinationRectangle = new Rectangle(
                DestinationRectangle.Location + Origin.ToPoint(),
                DestinationRectangle.Size);
            Rotation = value;
            return this;
        }

        public SpriteDrawEventArgs Translate(Point value)
        {
            DestinationRectangle = new Rectangle(
                DestinationRectangle.Location + value,
                DestinationRectangle.Size);
            return this;
        }

        public SpriteDrawEventArgs Opacity(float value)
        {
            Color = Color * value;
            return this;
        }

        public void Scale(float value)
        {
            var rect = DestinationRectangle;
            value = 1 - value;
            rect.Inflate(-(rect.Width / 2) * value, -(rect.Height / 2) * value);
            DestinationRectangle = rect;
        }
    }
}