package bricklesPackage;
import java.awt.*;
import java.lang.*;

public class Puck extends MovablePiece{
	protected static int _radius = 20;
      protected static int _halfradius = 10;
	protected Dimension _playingFieldDimension;
	protected boolean _kaput;
	protected Paddle tmpPaddle;
	protected 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 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();
	//	_currentPosition.y = _currentPosition.y+ _currentVelocity.getSpeedY();
		newLeadingPoint();
		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();
		}
		else if(tmpPaddle._boundingBox.contains(_leadingPoint.x,_leadingPoint.y)){
            	tmpPaddle.collideWithPuck(this,_leadingPoint);
		//	System.out.println("Paddle "+_leadingPoint.x+" "+_leadingPoint.y+"\n");
		}
		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++;
					//System.out.println("Brick "+tmpBrick.getPosition()+"\n");

				}
      		}    
			}           
        	}
		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;
	}

}
