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

public class Puck extends MovablePiece{
	protected static int _radius = 16; //20;
      protected static int _halfradius = 8;//10;
	protected Dimension _playingFieldDimension;
	protected boolean _kaput;

	public Puck(PlayingField fieldPtr){
		//This sets the initial position of a new puck
		super(fieldPtr, new Point(150,90), new Velocity(9,135));
		_fieldPtr = fieldPtr;
		_theView = ((BricklesPlayingField)_fieldPtr).getView();;
		_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 tick(){
				move();
	}

	public void newLeadingPoint(){
		int direction = _currentVelocity.getDirection();
		if(direction >=0 && direction <90){
		//	_leadingPoint.x = _currentPosition.x;
		//	_leadingPoint.y = _currentPosition.y;
			_leadingPoint.x = _currentPosition.x+_radius;
			_leadingPoint.y = _currentPosition.y+_radius;
		}
		else
		if(direction >=90 && direction <180){
			_leadingPoint.x = _currentPosition.x;
			_leadingPoint.y = _currentPosition.y;
		//	_leadingPoint.y = _currentPosition.y+_radius;
		}
		else
		if(direction >=180 && direction <270){
		//	_leadingPoint.x = _currentPosition.x + _radius;
		//	_leadingPoint.y = _currentPosition.y + _radius;
			_leadingPoint.x = _currentPosition.x;
			_leadingPoint.y = _currentPosition.y;
		}
		else
		if(direction >=270 && direction <360){
			_leadingPoint.x = _currentPosition.x+_radius;
			_leadingPoint.y = _currentPosition.y+_radius;
		//	_leadingPoint.y = _currentPosition.y;
		}
		System.out.println("leadingPoint "+_leadingPoint.x+" "+_leadingPoint.y+" "+direction+"\n");
		System.out.println("_currentPosition "+_currentPosition.x+" "+_currentPosition.y+" "+direction+"\n");
		System.out.println("\n");
	}


	public void move(){

        int hitbrickcount = 0;
        GameOverDialog wonDialog;

        if (_kaput == true)
           _fieldPtr.deleted(this);  
        	System.out.println("Original "+ _currentPosition.x + " " + _currentPosition.y+"\n");
        	_playingFieldDimension = _theView.getSize();
		_currentPosition.x = _currentPosition.x+ _currentVelocity.getSpeedX();
		_currentPosition.y = _currentPosition.y+ _currentVelocity.getSpeedY();
		if(_leadingPoint.x < 0){
			_currentVelocity.reverseX();
			_currentPosition.x = 0;
			System.out.println("first "+ _currentPosition.x + " " + _currentPosition.y+"\n");
		}
		else if(_leadingPoint.x + _radius >= _playingFieldDimension.width){
			_currentVelocity.reverseX();
			_leadingPoint.x = _playingFieldDimension.width - _halfradius;
			System.out.println("second "+ _currentPosition.x + " " + _currentPosition.y+"\n");
		}
		else
                if(_leadingPoint.y < 0){
			_currentPosition.y = 0;
			_currentVelocity.reverseY();
			System.out.println("third "+ _currentPosition.x + " " + _currentPosition.y+"\n");
		}
		else if(_leadingPoint.y >= _playingFieldDimension.height){
                        deleted();
		}
		newLeadingPoint();
		_boundingBox = new Rectangle(_currentPosition,_extent);
   		Paddle tmpPaddle = _theView.getPaddle();
            if(tmpPaddle._boundingBox.contains(_leadingPoint.x,_leadingPoint.y)){
            	tmpPaddle.collideWithPuck(this,_leadingPoint);
		}
      	BrickPile tmpBrickPile = _theView.getBrickPile();
      	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);
                	}
                            
        	}
   
      for(int i=0;i<tmpBrickPile.getSize();i++){
        Brick tmpBrick = tmpBrickPile.getBrickAt(i);
        if (tmpBrick.isHit()) 
           hitbrickcount++;
  
      }
      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;
	}

}
