using System;
using System.Xml.Serialization;

namespace Icarus.Logic.Physics
{
	/// <summary>
	/// This moving box needs access to the world object that is holding it
	/// </summary>
	public class CollisionBoxMobile : CollisionBox
	{
		/// <summary>
		/// This particular box needs to know things about the world obj holding it
		/// </summary>
        private WorldObject wo;
        
        /// <summary>
        /// Allowing the world obj to set a ref to itself
        /// </summary>
        [XmlIgnore]
        public WorldObject WO
        {
            set { this.wo = value;}
        }

        /// <summary>
        /// Constructionering
        /// </summary>
        public CollisionBoxMobile()
		{
			this.Description = "CB-Mobile";
		}

        public override int handleRectCollision(PhysicsRectangle rect, PhysicsObject po)
        {
            if(!(po is PhysicsPlayer)) return (int)PhysicsSettings.enumActions.DoNothing;

            //Stop a person from moving(walking) once they hit the floor
            if(po.ActionState == (int)PhysicsSettings.enumPlayerActionStates.FallingLeft)
            {
                po.LastActionState = po.ActionState;
                po.ActionState = (int)PhysicsSettings.enumPlayerActionStates.StandingLeft;
                po.Velocity.i = 0.0f;
            }
            else if(po.ActionState == (int)PhysicsSettings.enumPlayerActionStates.FallingRight)
            {
                po.LastActionState = po.ActionState;
                po.ActionState = (int)PhysicsSettings.enumPlayerActionStates.StandingRight;
                po.Velocity.i = 0.0f;
            }

            //Prevent the player from colliding when jumping up hill
            if(po.ActionState == (int)PhysicsSettings.enumPlayerActionStates.JumpingRight||
                po.ActionState == (int)PhysicsSettings.enumPlayerActionStates.JumpingLeft)
            {
                return (int)PhysicsSettings.enumActions.DoNothing;
            }

            //Move the PO with the velocity of itself plus this WO
            po.Velocity.i = wo.Velocity.i;

            if((po.CollisionBox.BoundingBox.Y + po.CollisionBox.BoundingBoxHeight/2) > (this.BoundingBox.Y+this.BoundingBoxHeight/2))//Move PO up
            {
                //po.Location.y = this.BoundingBox.Y + this.BoundingBoxHeight + po.CBYoffset;
            }
            else //Move PO down
            {
                po.Location.y = this.BoundingBox.Y - po.CollisionBox.BoundingBoxHeight + po.CBYoffset;
                po.Velocity.j = 0.0f;
            }
            //po.Velocity.j = 0.0f; Moved inside if else statement

            return (int)Icarus.Logic.Physics.PhysicsSettings.enumActions.DoNothing;
        }

	}
}
