package school_project;


import school_project.Utils.Array;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Base class for everything that is a shape kind, like map and pieces
 * it contain a matrix of boolean where the shape is defined by the true's
 */
public class Shape implements Serializable, Cloneable{
    
    protected boolean[][] matrix;
    protected int height, width;

    public Shape() {
        matrix = new boolean[0][0];
    }

    public Shape(boolean[][] matrix){
        this.setShape(matrix);
    }

    public void setShape(boolean[][] matrix) throws IllegalArgumentException{

        for (boolean[] row: matrix){
            if(row.length != matrix[0].length){
                throw new IllegalArgumentException("The argument should be a square matrix");
            }
        }

        for (int i = 0; i < matrix.length; i++) {
            if(!Array.isRowOnlyFalse(matrix, i)) {
                for (int j = 0; j < i; j++) {
                    matrix = Array.MatrixRemoveRow(matrix, 0);
                }
                break;
            }
        }

        for (int i = matrix.length-1; i >= 0; i--) {
            if(!Array.isRowOnlyFalse(matrix, i)) {
                for (int j = matrix.length-1; j > i; j--) {
                    matrix = Array.MatrixRemoveRow(matrix, j);
                }
                break;
            }
        }

        for (int i = 0; i < matrix[0].length; i++) {
            if(!Array.isColumnOnlyFalse(matrix, i)) {
                for (int j = 0; j < i; j++) {
                    matrix = Array.MatrixRemoveColumn(matrix, 0);
                }
                break;
            }
        }

        for (int i = matrix[0].length-1; i >= 0; i--){
            if(!Array.isColumnOnlyFalse(matrix, i)) {
                for (int j = matrix[0].length-1; j > i; j--) {
                    matrix = Array.MatrixRemoveColumn(matrix, j);
                }
                break;
            }
        }

        height = matrix.length;
        width = matrix[0].length;

        this.matrix = matrix;
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public boolean[][] getShape() {
        return matrix;
    }

    /**
     * Get the list of all the open possition of the map
     * @return ArrayList of vec2 of all the positions
     */
    public ArrayList<Vec2> getPosList(){
        ArrayList<Vec2> ret = new ArrayList<>();
        for (int x = 0; x < height; x++) {
            for (int y = 0; y < width; y++) {
                if(matrix[x][y]){
                    ret.add(new Vec2(x, y));
                }
            }
        }
        return ret;
    }

    @Override
    public String toString() {
        String ret = "";
        for (boolean[] row : matrix) {
            for (boolean el : row) {
                if(el) ret = ret.concat("⬛");
                else ret = ret.concat("⬜");
            }
            ret = ret.concat("\n");
        }
        return ret;
    }
}
