import java.awt.*;
import java.util.Random;

public class Mondrian {

    private int canvasWidth;
    private int canvasHeight;
    private static final int MIN_REGION_SIZE = 10;
    private Random rand;

    public void paintBasicMondrian(Color[][] pixels) {
        rand = new Random();
        int width = pixels[0].length;
        int height = pixels.length;
        this.canvasWidth = width;
        this.canvasHeight = height;

        divideCanvas(pixels, 0, width, 0, height);
    }

    public void paintComplexMondrian(Color[][] pixels) {

    }

    private void divideCanvas(Color[][] pixels, int x1, int x2, int y1, int y2) {
    if (x2 - x1 >= MIN_REGION_SIZE * 3 && y2 - y1 >= MIN_REGION_SIZE * 3) {
        //int hSplit = (x2 - x1) / 2 + x1;
        //int vSplit = (y2 - y1) / 2 + y1;

        int hSplit = rand.nextInt(x1+1, x2-10);
        int vSplit = rand.nextInt(y1+1, y2-10);

        //if (x2 - x1 >= canvasWidth / 4) {
        //int rangeX = (x2 - x1 - MIN_REGION_SIZE * 2) / MIN_REGION_SIZE;
        //    hSplit = rand.nextInt(rangeX) * MIN_REGION_SIZE + x1 + MIN_REGION_SIZE;
        //    hSplit = Math.min(hSplit, x2 - MIN_REGION_SIZE); // Ensure hSplit is within canvas bounds
        //    hSplit = Math.max(hSplit, x1 + MIN_REGION_SIZE); // Ensure hSplit is at least MIN_REGION_SIZE away from the canvas edge
        //}

        //if (y2 - y1 >= canvasHeight / 4) {
        //    int rangeY = (y2 - y1 - MIN_REGION_SIZE * 2) / MIN_REGION_SIZE;
        //    vSplit = rand.nextInt(rangeY) * MIN_REGION_SIZE + y1 + MIN_REGION_SIZE;
        //    vSplit = Math.min(vSplit, y2 - MIN_REGION_SIZE); // Ensure vSplit is within canvas bounds
        //    vSplit = Math.max(vSplit, y1 + MIN_REGION_SIZE); // Ensure vSplit is at least MIN_REGION_SIZE away from the canvas edge
        //}

        //// Additional constraints for the horizontal splitting points to prevent rectangles wider than canvasWidth/4
        //if (x2 - x1 >= canvasWidth / 4) {
        //    hSplit = Math.min(hSplit, x1 + (x2 - x1) / 2); // Ensure hSplit is not farther than halfway across the canvas
        //}

        fill(pixels, x1, x2, y1, y2);

        if (x2 - x1 >= canvasWidth / 4 && y2 - y1 >= canvasHeight / 4) {
            divideCanvas(pixels, x1, hSplit, y1, vSplit);
            divideCanvas(pixels, hSplit, x2, y1, vSplit);
            divideCanvas(pixels, x1, hSplit, vSplit, y2);
            divideCanvas(pixels, hSplit, x2, vSplit, y2);
        } else if (x2 - x1 >= canvasWidth / 4) {
            divideCanvas(pixels, x1, hSplit, y1, y2);
            divideCanvas(pixels, hSplit, x2, y1, y2);
        } else if (y2 - y1 >= canvasHeight / 4) {
            divideCanvas(pixels, x1, x2, y1, vSplit);
            divideCanvas(pixels, x1, x2, vSplit, y2);
        }
    } else {
        fill(pixels, x1, x2, y1, y2);
    }
}


    private void fill(Color[][] pixels, int x1, int x2, int y1, int y2) {
        Color region = getRandomColor();
        for (int i = y1; i < y2; i++) {
            pixels[i][x1] = Color.BLACK;
            pixels[i][x2 - 1] = Color.BLACK;
        }

        for (int j = x1; j < x2; j++) {
            pixels[y1][j] = Color.BLACK;
            pixels[y2 - 1][j] = Color.BLACK;
        }

        for (int i = y1 + 1; i < y2 - 1; i++) {
            for (int j = x1 + 1; j < x2 - 1; j++) {
                pixels[i][j] = region;
            }
        }
    }


    private Color getRandomColor() {
        Color[] colors = {Color.RED, Color.YELLOW, Color.CYAN, Color.WHITE};
        return colors[rand.nextInt(4)];
    }
}

