using System;
using System.Xml.Serialization;

namespace Icarus.Logic.Physics
{
	/// <summary>
	/// This is specially made for the tux robot hand.  It should only
	/// be put in a MovingPlatform worldobject.
	/// </summary>
	public class CollisionBoxRobotTuxHand : CollisionBox
	{

        /// <summary>
        /// This particular box needs to know things about the world obj holding it
        /// </summary>
        private PhysicsObject poParent;
        
        /// <summary>
        /// Allowing the world obj to set a ref to itself
        /// </summary>
        [XmlIgnore]
        public PhysicsObject PO
        {
            set { this.poParent = value;}
        }

        /// <summary>
        /// Construction
        /// </summary>
		public CollisionBoxRobotTuxHand()
		{
			this.Description = "Tux's Robot Hand";
            this.Damage = 20;
		}


        public override int handleRectCollision(PhysicsRectangle rect, PhysicsObject po)
        {
            if(po is PhysicsPlayer)
            {
                //Stop a person from moving(walking) once they hit the floor(hand)
                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;
                }

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

                if((po.CollisionBox.BoundingBox.Y + po.CollisionBox.BoundingBoxHeight/2) > (this.BoundingBox.Y+this.BoundingBoxHeight/2))//Move PO up
                {
                    //DAMAGE CODE
                    ((WorldObject)po).GameStats.Health -= this.Damage;
                    ((PhysicsPlayer)po).Vulnerable = false;

                    po.Location.x = this.BoundingBox.X - po.CollisionBox.BoundingBoxWidth + po.CBXoffset;
                    po.Location.y -= 10;//Just move him up a bit above the floor
                    po.Velocity.i = -3.0f * PhysicsSettings.Instance.WalkVelocity;
                    po.Velocity.j += 1.5f * PhysicsSettings.Instance.JumpVelocity;
                }
                else //Move PO down
                {
                    po.Location.y = this.BoundingBox.Y - po.CollisionBox.BoundingBoxHeight + po.CBYoffset;
                }
                po.Velocity.j = 0.0f;
            }

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