using System;

namespace Icarus.Logic.Physics
{
	/// <summary>
	/// Summary description for TriangleBR.
	/// </summary>
	public class TriangleBR : Triangle
	{
		public TriangleBR()
		{
			this.Description = "Tri-Floor-PosSlope";
		}

        /// <summary>
        /// Performs the necessary operations on a PO
        /// that has collided with this triangle
        /// </summary>
        /// <param name="po">Physics Object that collided with triangle</param>
        /// <returns>Action to be taken after collision occurs(ie: remove, doNothing)</returns>
        public override int intersectionAction(PhysicsObject po)
        {

            float fTmp, fTmp2;//, fMag;
            bool bTmp;

            if (!(po is PhysicsPlayer)) return (int)PhysicsSettings.enumActions.DoNothing;
 
            bTmp = ((PhysicsPlayer)po).StopWalking; 
            fTmp = po.Velocity.i;

            //this whole stage system has to go
            
            if(po.ActionState == (int)PhysicsSettings.enumPlayerActionStates.FallingLeft)
            {
                if(((PhysicsPlayer)po).StopWalking)
                {
                    //Reset stopWalking boolean
                    ((PhysicsPlayer)po).StopWalking = false;
                    //Update ActionState
                    po.LastActionState = po.ActionState;
                    po.ActionState = (int)PhysicsSettings.enumPlayerActionStates.StandingLeft;
                    po.Velocity.i = 0.0f;
                }
                else
                {
                    //Update ActionState
                    po.LastActionState = po.ActionState;
                    po.ActionState = (int)PhysicsSettings.enumPlayerActionStates.WalkingLeft;
                }         
            }
            else if(po.ActionState == (int)PhysicsSettings.enumPlayerActionStates.FallingRight)
            {
                if(((PhysicsPlayer)po).StopWalking)
                {
                    //Reset stopWalking boolean
                    ((PhysicsPlayer)po).StopWalking = false;
                    //Update ActionState
                    po.LastActionState = po.ActionState;
                    po.ActionState = (int)PhysicsSettings.enumPlayerActionStates.StandingRight;
                    po.Velocity.i = 0.0f;
                }
                else
                {
                    //Update ActionState
                    po.LastActionState = po.ActionState;
                    po.ActionState = (int)PhysicsSettings.enumPlayerActionStates.WalkingRight;
                }
            }

            if(((PhysicsPlayer)po).StopWalking)
            {
                po.Velocity.i = 0.0f;
            }
            
            //Calculate and move the player to the spot of intersection
            fTmp2 = (this.TriangleBounds.Y + this.TriangleBounds.Height) - (this.Slope * this.TriangleBounds.X);
            fTmp2 = (this.Slope * (po.CollisionBox.BoundingBox.X + po.CollisionBox.BoundingBoxWidth)) + fTmp2;
            po.Location.y = fTmp2 - (po.CollisionBox.BoundingBoxHeight - po.CBYoffset);
            
            //Prevent the player from colliding when jumping up hill
            if(po.ActionState == (int)PhysicsSettings.enumPlayerActionStates.JumpingRight||
                po.ActionState == (int)PhysicsSettings.enumPlayerActionStates.JumpingLeft)
            {
                //if he is in the slope, He has been moved up... now do nothing
                return (int)PhysicsSettings.enumActions.DoNothing;
            }

            

            //TODO: Check to make sure he doesnt climb above top of triangle
            if(po.Location.y < this.TriangleBounds.Y) 
            {
                po.Location.y = this.TriangleBounds.Y;
            }
                        
            //Check to see if he is not moving
            if(fTmp == 0.0f)
            {
                //Stopped walking
                po.Velocity.i = 0.0f;
                po.Velocity.j = 0.0f;
                return (int)PhysicsSettings.enumActions.DoNothing;
            }
                        
            //Check to see if he is trying to move up slope
            if(!bTmp)
            if(fTmp > 0.0f)
            {            
                po.Velocity.j = 0.0f;
                //fMag = po.Velocity.magnitude;
                if(po.ActionState != (int)PhysicsSettings.enumPlayerActionStates.WalkingRight)
                {
                    po.LastActionState = po.ActionState;
                    po.ActionState = (int)PhysicsSettings.enumPlayerActionStates.WalkingRight;//WalkingUpSlopeRight;
                }
            }
            else//Down Slope
            {
                //fMag = po.Velocity.magnitude;
                //fAngle = (float)Math.Atan2(this.rectTriangleBounds.Height, this.rectTriangleBounds.Width);
                po.Velocity.j = 0.0f;//((float)((po.Velocity.i/Math.Cos(fAngle))*Math.Sin(fAngle)));

                if(po.ActionState != (int)PhysicsSettings.enumPlayerActionStates.WalkingLeft)
                {
                    po.LastActionState = po.ActionState;
                    po.ActionState = (int)PhysicsSettings.enumPlayerActionStates.WalkingLeft;//WalkingDownSlopeLeft;
                }
            }

            //Moving up slope slower 
//            if(fTmp > 0.0f)//Moving Up Slope
//            {
//                if(fTmp < 0.0f) fTmp *= -1;
//                po.Velocity.i /= fMag;
//                po.Velocity.i *= fTmp;
//            }

            return (int)PhysicsSettings.enumActions.DoNothing;
        }
        
	}
}
