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

namespace Fireball
{
    class Sprite
    {
        //The current position of the Sprite
        public Vector2 Position;

        //The texture object used when drawing the sprite
        protected Texture2D mSpriteTexture;

        //The asset name for the Sprite's Texture
        public string AssetName;

        //The Size of the Sprite (with scale applied)
        public Rectangle Size;

        //The amount to increase/decrease the size of the original sprite. 
        public float Scale = 1.0f;

        //When the scale is modified through the property, the Size of the 
        //sprite is recalculated with the new scale applied.
        /*public float Scale
        {
            get { return mScale; }
            set
            {
                mScale = value;

                //Recalculate the Size of the Sprite with the new scale
                Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
            }
        }*/

        public Sprite(string theAssetName, Vector2 initPos)
        {
            AssetName = theAssetName;
            Position = initPos;
        }

        public bool Intersects(Sprite c, int offset)
        {
            if (Position.X + mSpriteTexture.Width * Scale - offset < c.Position.X) return false;
            if (c.Position.X + c.mSpriteTexture.Width * c.Scale - offset < Position.X) return false;
            if (Position.Y + mSpriteTexture.Height * Scale - offset < c.Position.Y) return false;
            if (c.Position.Y + c.mSpriteTexture.Height * c.Scale - offset < Position.Y) return false;
            return true;
        }
        /*
        public void changeSize(int sizeFactor)
        {
            Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale))
        }
        */
        //Load the texture for the sprite using the Content Pipeline
        public void LoadContent(ContentManager theContentManager)
        {
            mSpriteTexture = theContentManager.Load<Texture2D>(AssetName);
            Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
        }

        //Draw the sprite to the screen
        public virtual void Draw(SpriteBatch theSpriteBatch)
        {
            theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
                Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
        }

        public virtual void Draw(SpriteBatch theSpriteBatch, Texture2D theSpriteTexture, SpriteEffects mEffect)
        {
            theSpriteBatch.Draw(theSpriteTexture, Position, new Rectangle(0, 0, theSpriteTexture.Width, theSpriteTexture.Height),
                Color.White, 0.0f, Vector2.Zero, Scale, mEffect, 0);
        }

        public virtual void Draw(SpriteBatch theSpriteBatch, SpriteEffects mEffect)
        {
            theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
                Color.White, 0.0f, Vector2.Zero, Scale, mEffect, 0);
        }

        public virtual void Draw(SpriteBatch theSpriteBatch, float depth)
        {
            theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
                Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, depth);
        }

        public virtual void Draw(SpriteBatch theSpriteBatch, SpriteEffects mEffect, float depth)
        {
            theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
                Color.White, 0.0f, Vector2.Zero, Scale, mEffect, depth);
        }

        //Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.

        public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
        {
            Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
        }
    }
}
