import javax.swing.*;
import java.awt.*;

/** Assignment A4: using a Turtle, Take 4 and 5 */
public class Ball extends YourTurtle {
    private int radius;
    private int vx;
    private int vy;
    /**=radius of the ball*/
    public int getRadius(){
        return radius;
    }
    /**=speed in horizontal direction*/
    public int getVx(){
        return vx;
    }
    /**=speed in vertical direction*/
    public int getVy(){
        return vy;
    }
    /**Constructor:create a ball at position(x,y), radius r, color c, and velocity(vx,vy).*/
    public Ball(int x, int y, int vx, int vy, int r, int c){
        super();
        liftPen();
        moveTo(x,y,0);
        putPenDown();
        this.vx= vx;
        this.vy= vy;
        radius= r;
        setColor(c);
        fillCircle(radius*2);
        liftPen();
    }
    
    /**Constructor:create a ball at position(x,y), radius r, color c, and velocity(vx,vy).*/
    public Ball(int x, int y, int vx, int vy, int r, Color c){
        super();
        liftPen();
        moveTo(x,y,0);
        putPenDown();
        this.vx= vx;
        this.vy= vy;
        radius= r;
        setColor(c);
        fillCircle(radius*2);
        liftPen();
    }
    
    /**Constructor:create a black ball at center of the frame, radius r and velocity(vx,vy).*/
    public Ball(int vx, int vy, int r){
        super();
        putPenDown();
        this.vx= vx;
        this.vy= vy;
        radius= r;
        setColor(Color.black);
        fillCircle(radius*2);
        liftPen();
    }
    /**Move the ball once*/
    public void moveBallOnce(){
        Color save=getColor();
        putPenDown();
        setColor(Color.white);
        fillCircle(radius*2);
        liftPen();
        moveTo(getX()+vx,getY()+vy,0);
        setColor(save);
        fillCircle(radius*2);
        if (((getY())<=radius)||((getY()+radius)>=height))       
            vy= -vy;
        if (((getX())<=radius)||((getX()+radius)>=width))        
            vx= -vx;
    }
    /** Put the ball perpetuallu in motion*/
    public void inMotion(){
        while (true) {
            pause(100);
            moveBallOnce();
        }
    }
    /**Task 5:
     * Draw 3 layers of bricks at the top of the frame.
     * Set the ball to move. When it hits the bricks, the bricks will dissappear.
     Precondition: A ball is created properly using a constructor. */
    public void pinBall(){
        drawBricks();
        while (true) {
            pause(100);
            moveBallOnce();
            if (((getY()-radius)<=90)&&((getY()-radius)>0)){
                  int xx=(int)Math.ceil(getX()/100.0);
                  int yy=(int)Math.ceil((getY()-radius)/30.0);
                  if (brickIsThere[xx-1][yy-1]){
                    if (Ball.in(getX(),getY()-radius,(xx-1)*100,(yy-1)*30)) 
                         vy= -vy;
                    else {if (Ball.in(getX()-radius,getY(),(xx-1)*100,(yy-1)*30)||Ball.in(getX()+radius,getY(),(xx-1)*100,(yy-1)*30))
                        vx= -vx;}
                    eraseBrick(xx,yy);
                    brickIsThere[xx-1][yy-1]=false;
                    fillCircle(radius*2);
                  }
                  else{
                      xx=(int)Math.ceil(getX()/100.0);
                      yy=(int)Math.ceil((getY()+radius)/30.0);
                      if ((xx<=5)&&(yy<=3)&&(brickIsThere[xx-1][yy-1])){
                        if (Ball.in(getX(),getY()+radius,(xx-1)*100,(yy-1)*30)) 
                             vy= -vy;
                        else {if (Ball.in(getX()-radius,getY(),(xx-1)*100,(yy-1)*30)||Ball.in(getX()+radius,getY(),(xx-1)*100,(yy-1)*30))
                            vx= -vx;}
                        eraseBrick(xx,yy);
                        brickIsThere[xx-1][yy-1]=false;
                        fillCircle(radius*2);
                      }
                  }
                }
        }
    }
   
    private boolean [] [] brickIsThere= new boolean [5] [3];
    /**Draw a 3*5 formation of bricks. Assume all are not hit yet.*/
    public void drawBricks(){
        int xxx=getX();
        int yyy=getY();
        Color save=getColor();
        moveTo(250,45,0);
        putPenDown();
        setColor(Color.red);
        fillRectangle(width,90);
        moveTo(0,30,0);
        setColor(Color.white);
        move(width);
        moveTo(0,60,0);
        move(width);
        moveTo(0,90,0);
        move(width);
        for (int i=1; i!=5; i++){
            moveTo(i*100,0,270);
            move(90);
        }
        setColor(save);
        for (int i=0; i!=5; i++){
            for (int j=0; j!=3; j++){
                brickIsThere[i][j]=true;
            }
        }
        moveTo(xxx,yyy,0);
    }
    /**= point(x,y) is in rectangle with width 100, height 30, top-left corner(a,b)*/
    public static boolean in(int x,int y,int a,int b){
        if ((x>=a)&&(x<=a+100)&&(y>=b)&&(y<=b+30))
            return true;
        return false;
    }
    /**Erase the brick at line y, column x */
    public void eraseBrick(int x, int y){
        int xxx=getX();
        int yyy=getY();
        Color save=getColor();
        moveTo((x-1)*100+50,(y-1)*30+15,0);
        putPenDown();
        setColor(Color.white);
        fillRectangle(100,30);
        setColor(save);
        moveTo(xxx,yyy,0);
    }
}