import objectdraw.*;
import java.awt.*;

/*
 * Example Breakout: the collection of bricks
 *
 * Jim Teresco, Siena College, CSIS 120, Fall 2011
 *
 * $Id: BrickCollection.java 1602 2011-04-14 02:51:26Z terescoj $
 */

public class BrickCollection {

    // dimensions of the array of bricks
    private int bricksPerRow;
    private int numRows;
    
    // the array
    private FilledRect[][] bricks;
    
    // some colors to use to define our rows of bricks
    private static final Color[] colors = { Color.red, Color.orange, Color.yellow,
                                            Color.green, Color.blue, Color.magenta };
    
    /**
     * Construct a BrickCollection
     * 
     * @param bricksPerRow the number of bricks per row
     * @param numRows the number of rows of bricks
     * @param brickHeight the height (in pixels) of a row of bricks
     * @param yPos the y-coordinate of the topmost row of bricks
     * @param xMin the x-coordinate of the leftmost brick
     * @param width the width of the playing area to be filled with bricks
     * @param canvas the DrawingCanvas on which to place the bricks
     */
    public BrickCollection(int bricksPerRow, int numRows,
               double brickHeight, double yPos, 
               double xMin, double width, DrawingCanvas canvas) {

        this.bricksPerRow = bricksPerRow;
        this.numRows = numRows;
        
        // construct the array
        bricks = new FilledRect[numRows][bricksPerRow];
        
        double brickWidth = width/bricksPerRow;
        
        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < bricksPerRow; col++) {
                bricks[row][col] = new FilledRect(xMin + col*brickWidth,
                                        yPos + row*brickHeight, brickWidth,
                                        brickHeight, canvas);
                bricks[row][col].setColor(colors[row%colors.length]);
            }
        }
    }
    
    /**
     * Check if the given ball overlaps any brick, if so, remove it
     * and return true, otherwise return false.
     * 
     * @param ball the FilledOval representing the ball to check for
     * overlap with the existing bricks
     * 
     * @return whether the ball overlapped any brick
     */
    public boolean hitBrick(FilledOval ball) {
        
        boolean hitOne = false;
        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < bricksPerRow; col++) {
                if (bricks[row][col] != null && bricks[row][col].overlaps(ball)) {
                    bricks[row][col].removeFromCanvas();
                    bricks[row][col] = null;
                    hitOne = true;
                }
            }
        }
        
        return hitOne;
    }
}
