import java.awt.*;
import java.lang.*;

/**
  *The Paddle tracks the left/right mouse movement to provide
  *a surface off of which the Puck bounces
  */
public class Paddle extends MovablePiece{

	/**
	  *The constructor initializes the attributes of the Paddle
	  *@param fieldPtr the PlayingField that contains the MovablePiece
	  *@param initialPosition the starting position for the MovabalePiece
	  *@param initalVelocity the Velocity of the piece when it is created
	  */
	public Paddle(PlayingField fieldPtr,Point initialPosition,Velocity initialVelocity){
		super(fieldPtr,initialPosition,initialVelocity);
	  	_theView = ((BricklesPlayingField)_fieldPtr).getView();
	  	_bitmap = _theView.getImage(_theView.getCodeBase(),"images/paddle.gif");
		if(_bitmap == null)
		 	_theView.showStatus("BitMap not loaded");
		_extent = new Dimension(60,20);
            _leadingPoint = new Point(_currentPosition.x,
                                          _currentPosition.y);
            newLeadingPoint();
		_boundingBox = newBoundingBox(_currentPosition,_extent);
	}
 
	/**
	  *This method calculates the corner of the bounding box in the
	  *direction of travel
	  */
      public void newLeadingPoint(){
                int direction = _currentVelocity.getDirection();
			_leadingPoint.y = _currentPosition.y;
                if(direction >=0 && direction <90){
				 _leadingPoint.x = _currentPosition.x;
                }
                else
                if(direction >=90 && direction <180){
				_leadingPoint.x = _currentPosition.x + _extent.width;
                }
                else
                if(direction >=180 && direction <270){
                        _leadingPoint.x = _currentPosition.x;
                }
                else
                if(direction >=270 && direction <360){
                        _leadingPoint.x = _currentPosition.x + _extent.width;
                }
        }

	/**
	  *The behavior on receiving a tick
	  *post: The Paddle is moved to the new location
	  */
	public void tick(){
		move();
	}

	/**
	  *Since the Paddle can not collide with itself, this is a no-op
	  */
	public void collideWithPaddle(Paddle aPaddle,Point aPoint){
	}

	/**
	  *The general collision method called by an ArcadeGamePiece
	  *when it does not know which MovablePiece hit it 
	  *@param aPiece the ArcadeGamePiece that has collided with the Paddle
	  *@param aPoint where the collision occurred
	  */
	public void collideWith(ArcadeGamePiece aPiece,Point aPoint){
		aPiece.collideWithPaddle(this,aPoint);
	}

	/**
	  *Called when the Puck collides with the Paddle
	  *post: The Y component of the Puck's velocity is reversed
	  *@param aPuck the Puck that caused the collision
	  *@param aPoint the location of the collision
	  */
	public void collideWithPuck(Puck aPuck,Point aPoint){
	  	aPuck.reverseY();
	}

	/**
	  *Recalculates the attributes after a tick was received
	  *post: the position of the Paddle is updated
	  */
	public void move(){
      	if (_currentPosition.x + _extent.width >= _theView.getFieldWidth())  
            	_currentPosition.x = _theView.getFieldWidth() - _extent.width;               
		_boundingBox = new Rectangle(_currentPosition,_extent);
		newLeadingPoint();
	}
}
