package brickbreaker;

import javafx.scene.*;
import javafx.scene.image.*;

/**
 * @author Pavel Porvatov
 */

// Types of bricks
public def TYPE_BLUE = 0;
public def TYPE_BROKEN1 = 1;
public def TYPE_BROKEN2 = 2;
public def TYPE_BROWN = 3;
public def TYPE_CYAN = 4;
public def TYPE_GREEN = 5;
public def TYPE_GREY = 6;
public def TYPE_MAGENTA = 7;
public def TYPE_ORANGE = 8;
public def TYPE_RED = 9;
public def TYPE_VIOLET = 10;
public def TYPE_WHITE = 11;
public def TYPE_YELLOW = 12;

public class Brick extends CustomNode {
    // Type of the brick
    public-init var type: Integer;

    // Invoked when the ball kicks the brick.
    // Returns true if the brick was broken or false otherwise
    public function kick(): Boolean {
        if (type == TYPE_GREY) {
            return false;
        }

        if (type == TYPE_BROKEN1) {
            type = TYPE_BROKEN2;

            return false;
        }

        return true;
    }

    override public function create(): Node {
        ImageView {
            image: bind Config.bricksImages[type]
        }
    }
}

// Converts string representation of a brick to brick type
public function getBrickType(s: String) {
    if (s == "L") {
        TYPE_BLUE
    } else if (s == "2") {
        TYPE_BROKEN1
    } else if (s == "B") {
        TYPE_BROWN
    } else if (s == "C") {
        TYPE_CYAN
    } else if (s == "G") {
        TYPE_GREEN
    } else if (s == "0") {
        TYPE_GREY
    } else if (s == "M") {
        TYPE_MAGENTA
    } else if (s == "O") {
        TYPE_ORANGE
    } else if (s == "R") {
        TYPE_RED
    } else if (s == "V") {
        TYPE_VIOLET
    } else if (s == "W") {
        TYPE_WHITE
    } else if (s == "Y") {
        TYPE_YELLOW
    } else {
        println("Unknown brick type '{s}'");

        TYPE_WHITE
    }
}