import java.awt.*;
import java.lang.*;

public class Puck extends MovablePiece{
	static int _radius = 20;
    static int _halfradius = 10;
	Dimension _playingFieldDimension;
	boolean _kaput;
	Paddle tmpPaddle;
	BrickPile tmpBrickPile;


	public Puck(){
		super(null, new Point(150,90), new Velocity(8,225));
		_extent = new Dimension(_radius,_radius);
		_boundingBox = new Rectangle(_currentPosition,_extent);
		_leadingPoint = new Point(_currentPosition.x,_currentPosition.y);
		newLeadingPoint();
	}

	public Puck(PlayingField fieldPtr){
		//This sets the initial position of a new puck
		super(fieldPtr, new Point(150,90), new Velocity(8,225));
		_fieldPtr = fieldPtr;
		_theView = ((BricklesPlayingField)_fieldPtr).getView();
		tmpPaddle = _theView.getPaddle();
		tmpBrickPile = _theView.getBrickPile();
		_extent = new Dimension(_radius,_radius);
		_boundingBox = new Rectangle(_currentPosition,_extent);
		_leadingPoint = new Point(_currentPosition.x,_currentPosition.y);
		newLeadingPoint();
		_bitmap = _theView.getImage(_theView.getCodeBase(),"images/puck.gif");
		if(_bitmap == null)
			_theView.showStatus("BitMap not loaded");
		_kaput = false;
	}


	public void newLeadingPoint(){
		int direction = _currentVelocity.getDirection();
		if(direction >=0 && direction <90){
			_leadingPoint.y = _currentPosition.y;
			_leadingPoint.x = _currentPosition.x+_radius;
		}
		else
		if(direction >=90 && direction <180){
			_leadingPoint.x = _currentPosition.x;
			_leadingPoint.y = _currentPosition.y;
		}
		else
		if(direction >=180 && direction <270){
			_leadingPoint.y = _currentPosition.y + _radius;
			_leadingPoint.x = _currentPosition.x;
		}
		else
		if(direction >=270 && direction <360){
			_leadingPoint.x = _currentPosition.x+_radius;
			_leadingPoint.y = _currentPosition.y+_radius;
		}
	}


	public void move(){

        int hitbrickcount = 0;
        GameOverDialog wonDialog;

        	if (_kaput == true)
           		_fieldPtr.deleted(this);  
        	_playingFieldDimension = _theView.getSize();
		_currentPosition.x = _currentPosition.x+ _currentVelocity.getSpeedX();
		_currentPosition.y = _currentPosition.y - _currentVelocity.getSpeedY();
		newLeadingPoint();
		if(_leadingPoint.x < 0){
			_currentVelocity.reverseX();
			_currentPosition.x = 0;
		}
		else if(_leadingPoint.x /*+ _radius*/ >= _playingFieldDimension.width){
			_currentVelocity.reverseX();
			_leadingPoint.x = _playingFieldDimension.width - _halfradius;
		}
		else
                if(_leadingPoint.y < 0){
			_currentPosition.y = 0;
			_currentVelocity.reverseY();
		}
		else if(_leadingPoint.y >= _playingFieldDimension.height){
                        deleted();
		}
		else if(tmpPaddle._boundingBox.contains(_leadingPoint.x,_leadingPoint.y)){
            	tmpPaddle.collideWithPuck(this,_leadingPoint);
		}
		else{
			if(tmpBrickPile.isContained(_leadingPoint)){
			for(int i = 0;i< tmpBrickPile.getSize();i++){
      			Brick tmpBrick = tmpBrickPile.getBrickAt(i);
                  	if(tmpBrick._boundingBox.contains(_leadingPoint.x,_leadingPoint.y) && !tmpBrick.isHit()){
      				tmpBrick.collideWithPuck(this,_leadingPoint);
					System.out.println("I got a hit at "+i+"\n");
					_theView.repaint();
                		}
			}
			for(int i=0;i<tmpBrickPile.getSize();i++){
        			Brick tmpBrick = tmpBrickPile.getBrickAt(i);
			      if (tmpBrick.isHit()){ 
          				hitbrickcount++;

				}
      		}    
			}           
        	}
		newLeadingPoint();
		_boundingBox = new Rectangle(_currentPosition,_extent);
   	
      	if (hitbrickcount == tmpBrickPile.getSize())
           		wonDialog = new GameOverDialog(new Frame(), "You Won!", false);
        
	}

	public void collideWith(ArcadeGamePiece aPiece, Point aPoint){
		aPiece.collideWithPuck(this,_leadingPoint);
		if(_kaput == true){
			_fieldPtr.deleted(this);
		}
	}


	public void collideWithPaddle(Paddle aPaddle,Point aPoint){
		aPaddle.reverseY();
	}

	public void collideWithPuck(Puck aPuck,Point aPoint){}

	public void deleted(){
		_kaput = true;
	}

}
