package breakout;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;

/**
 *
 * @author Joel Duggan
 */
public class Brick implements BreakoutConstants {

    private int   type;                             // what type of brick this is (normal, unbreakable, etc.)
    private int   x, y;                             // location
    private Color color;                            // color of the brick

    private int     numHits = 0;                    // number of times this brick has been hit
    private boolean isAlive;                        // is brick active or been destroyed
    private boolean isDying;                        // if brick is in process of dying or dead
    private int     dyingCount = 1;                 // used for dying animation

    public MyVector[] vertex = new MyVector[5];     // vertices used for hit detection
    private BasicStroke edgeStroke;                 // stroke to draw brick edge

    public Brick(int type, int x, int y, Color color) {

        this.type = type;
        this.x    = x;
        this.y    = y;
        this.color = color;
        this.isAlive = true;
        this.isDying = false;

        vertex[0] = new MyVector(x, y);
        vertex[1] = new MyVector(x + BRICK_SIZE, y);
        vertex[2] = new MyVector(x + BRICK_SIZE, y + BRICK_SIZE);
        vertex[3] = new MyVector(x, y + BRICK_SIZE);
        vertex[4] = vertex[0];

        edgeStroke = new BasicStroke(1.0f);
    }

    public void draw(Graphics2D buffer) {

        if (isAlive) {
            if ( !isDying) {

                drawBrick(buffer, x, y, BRICK_SIZE);
                // draw a border
                buffer.setColor(Color.BLACK);
                buffer.setStroke(edgeStroke);
                buffer.drawRect(x, y, BRICK_SIZE, BRICK_SIZE);

            }
            else {
                // draw dying animation
                drawBrick(buffer, x + dyingCount / 2, y + dyingCount / 2, BRICK_SIZE - 1 - dyingCount);
                if ( ++dyingCount >= (BRICK_SIZE - 1) )
                    isAlive = false;
            }
        }
    }

    private void drawBrick(Graphics2D buffer, int x, int y, int size) {

        int r = color.getRed()   + BRICK_GRAD_AMOUNT;
        int g = color.getGreen() + BRICK_GRAD_AMOUNT;
        int b = color.getBlue()  + BRICK_GRAD_AMOUNT;

        if ( r > 255 ) r = 255;
        if ( g > 255 ) g = 255;
        if ( b > 255 ) b = 255;

        buffer.setPaint( new GradientPaint(x, y, color, x + Math.round(size / 2.0), y + Math.round(size / 2.0), new Color(r, g, b, color.getAlpha()), true ) );
        buffer.fillRect(x, y, size, size);
    }

    public void hit() {

        numHits++;

        // note : if brick is a multiple-hit(2-5), then type = number of hits needed
        if (type == BRICK_NORMAL || Player.hasSteelBall)
            kill();
        else if (type >= BRICK_2HIT && type <= BRICK_5HIT) {
            if (type == numHits)
                kill();
            else {
                int alpha = ((type - numHits) * 255) / type;
                color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
            }
        }
        else if (type == BRICK_METAL) { }

    }

    public void kill() {
        isDying = true;
    }

    public int getType() { return type; }
    public boolean isMetal() { return (type == BRICK_METAL); }
    public boolean isDyingOrDead() { return isDying; }
    public boolean isAlive() { return isAlive; }

}