using System;
using System.Xml.Serialization;

namespace Icarus.Logic.Physics
{
	/// <summary>
	/// Summary description for PhysicsRectangle.
	/// </summary>
	[XmlInclude(typeof(Physics.RectangleFloor))]
    [XmlInclude(typeof(Physics.RectangleWall))]
	public class PhysicsRectangle
	{

        #region Declarations

        /// <summary>
        /// X value of upper-left corner of rectangle
        /// </summary>
        private int iX;

        /// <summary>
        /// Y value of upper-left corner of rectangle
        /// </summary>
        private int iY;

        /// <summary>
        /// Height of this rectangle
        /// </summary>
        private int iHeight;

        /// <summary>
        /// Width of this rectangle
        /// </summary>
        private int iWidth;

        /// <summary>
        /// Text Description of this object for level editing
        /// </summary>
        private string strDescription = "";

        #endregion

        #region Properties/Feilds

        /// <summary>
        /// X value of upper-left corner of rectangle
        /// </summary>
        public int X
        {
            get { return this.iX; }
            set { this.iX = value; }
        }

        /// <summary>
        /// Y value of upper-left corner of rectangle
        /// </summary>
        public int Y
        {
            get { return this.iY; }
            set { this.iY = value; }
        }

        /// <summary>
        /// Width of this rectangle
        /// </summary>
        public int Width
        {
            get { return this.iWidth; }
            set { this.iWidth = value; }
        }

        /// <summary>
        /// Height of this rectangle
        /// </summary>
        public int Height
        {
            get { return this.iHeight; }
            set { this.iHeight = value; }
        }

        /// <summary>
        /// A text Description of this object for level editing
        /// </summary>
        public string Description
        {
            get { return this.strDescription; }
            set { this.strDescription = value; }
        }

        #endregion

        #region Contruction

		public PhysicsRectangle()
		{
			
		}

        public PhysicsRectangle(int x, int y, int width, int height)
        {
            this.iX = x;
            this.iY = y;
            this.iWidth = width;
            this.iHeight = height;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Determines if this rectangle and the passed rect intersect.
        /// </summary>
        /// <param name="rect">The rectangle to check for intersection with</param>
        /// <returns>True - intersection; False - no intersection</returns>
        public bool IntersectsWith(PhysicsRectangle rect)
        {
            int iWidth, iHeight;
            if(this.iX > rect.iX) iWidth = rect.Width;
            else iWidth = this.Width;
            if(this.iY > rect.iY) iHeight = rect.Height;
            else iHeight = this.Height;
            if (Math.Abs(this.iX - rect.iX) < iWidth
                && Math.Abs(this.iY - rect.iY) < iHeight)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Changes this rectangle making the method call to the intersection
        /// of the rectangle making the call and the rect passed.
        /// </summary>
        /// <param name="rect">The rectan</param>
        public void Intersect(PhysicsRectangle rect)
        {
            int iX, iY, iWidth, iHeight;

            if(this.iX > rect.iX) iWidth = rect.Width;
            else iWidth = this.Width;
            if(this.iY > rect.iY) iHeight = rect.Height;
            else iHeight = this.Height;
            if(Math.Abs(this.iX - rect.iX) < iWidth
                && Math.Abs(this.iY - rect.iY) < iHeight)
            {  
                //Find X
                if(this.X > rect.X)
                {
                    iX = this.X;
                }
                else
                {
                    iX = rect.X;
                }

                //Find width
                if(this.X + this.Width > rect.X + rect.Width)
                {
                    iWidth = rect.X + rect.Width - iX;
                }
                else
                {
                    iWidth = this.X + this.Width - iX;
                }


                //Find Y
                if(this.Y > rect.Y)
                {
                    iY = rect.Y;
                }
                else
                {
                    iY = this.Y;
                }

                //Find height
                if(this.Y + this.Height > rect.Y + rect.Height)
                {
                    iHeight = rect.Y + rect.Height - iY;
                }
                else
                {
                    iHeight = this.Y + this.Height - iY;
                }

                this.iX = iX;
                this.iY = iY;
                this.iWidth = iWidth;
                this.iHeight = iHeight;
            }
        }


        /// <summary>
        /// Performs the necessary operations on a PO
        /// that has collided with this rectangle
        /// </summary>
        /// <param name="po">Physics Object that collided with rectangle</param>
        /// <returns>Action to be taken after collision occurs(ie: remove, doNothing)</returns>
        public virtual int intersectionAction(PhysicsObject po)
        {
            
//            po.Velocity.i = 0.0f;
//            po.Velocity.j = 0.0f;
//            po.Velocity.k = 0.0f;

            return (int)PhysicsSettings.enumActions.DoNothing;
        }


        #endregion

	}
}
