import java.awt.Color;
import java.awt.Image;
import java.util.Random;

public class ShapeFactory {
    public static Shape createShape() {
        int shapeId = random.nextInt(basicShapeData.length);
        int x = Config.getInstance().getGroundColumnCount() / 2 - 2; // 居中
        int y = -3; // can random
        int rotateCount = random.nextInt(10);
        Color color = Config.getInstance().getRandomColor();
        Image image = Config.getInstance().getRandomImage();
        
        Shape shape = new Shape(shapeId, x, y, color, image, basicShapeData[shapeId]);
        for (int i = 0; i < rotateCount; ++i) {
            shape.rotateLeft();
        }
        
        return shape;
    }
    
    // 创建长条，作弊用的，哈哈
    public static Shape createLongShape() {
        int x = Config.getInstance().getGroundColumnCount() / 2 - 2;
        int y = -3; // random
        int shapeId = 6;
        Color color = Config.getInstance().getRandomColor();
        Image image = Config.getInstance().getRandomImage();
        Shape shape = new Shape(shapeId, x, y, color, image, basicShapeData[shapeId]);
        return shape;
    }

    // 所有同一个方块的不同变形都共享同一个数据.
    // 可以创建一个shape pool，然后从里面取方块，当被Ground合并后，返还给shape pool.
    // 这样做的话，可以使用即将到来的方块与当前方块不是同一个，而不会在当前方块旋转时他也旋转.
    private static Random random = new Random(System.currentTimeMillis());
    private static int[][][] basicShapeData = {
            {   {0, 0, 0, 0, 0},
                {0, 0, 1, 1, 0},
                {0, 0, 1, 1, 0},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0} }, 
            
            {   {0, 0, 0, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 1, 1, 1, 0},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0} },
                
            {   {0, 0, 0, 0, 0},
                {0, 1, 1, 0, 0},
                {0, 0, 1, 1, 0},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0} },
                
            {   {0, 0, 0, 0, 0},
                {0, 0, 1, 1, 0},
                {0, 1, 1, 0, 0},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0} },
                
            {   {0, 0, 0, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 0, 1, 1, 0},
                {0, 0, 0, 0, 0} },
                    
            {   {0, 0, 0, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 1, 1, 0, 0},
                {0, 0, 0, 0, 0} },
                
            {   {0, 0, 1, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 0, 0, 0, 0} }
    };
    
}
