package gof.gui;

import gof.core.Board;
import gof.core.Cell;
import gof.core.DisplayDriver;
import javafx.scene.Group;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class JavaFXDisplayDriver implements DisplayDriver {
    private int sz;
    private TilePane tilePane = new TilePane(5,5);

    public JavaFXDisplayDriver(int boardSize, int cellSizePx, Board board) {
        sz = boardSize;
        tilePane.setPrefRows(boardSize);
        tilePane.setPrefColumns(boardSize);

        Cell[][] g = board.getGrid();
        for (int i = 0; i < boardSize; i++) {
            for (int j = 0; j < boardSize; j++) {
                Color c = g[i][j].getState() ? Color.STEELBLUE : Color.WHITE;
                Rectangle r = new Rectangle(cellSizePx, cellSizePx, c);
                tilePane.getChildren().add(r);
                int ii = i;
                int jj = j;
                boolean newState = g[i][j].getState() ? false : true;
                r.setOnMousePressed(event -> {
                    r.setFill(Color.GRAY);

                });

                r.setOnMouseClicked(event -> {
                    r.setFill(g[ii][jj].getState() ? Color.WHITE : Color.STEELBLUE);
                    g[ii][jj].setNewState(newState);
                    g[ii][jj].updateState();

                });


            }
        }
    }

    @Override
    public void displayBoard(Board board) {
        Cell[][] g = board.getGrid();
        for (int i = 0; i < g.length; i++) {
            for (int j = 0; j < g[0].length; j++) {
                Rectangle r = (Rectangle) tilePane.getChildren().get(boardToPaneCoords(i, j));
                r.setFill(g[i][j].getState() ? Color.STEELBLUE : Color.WHITE);
            }
        }
    }

    public TilePane getPane() {
        return tilePane;
    }

    private int boardToPaneCoords(int i, int j) {
        return i * sz + j;
    }
}

@@ -892,19 +892,8 @@
     
-int ii = i;
 %0A   
@@ -909,454 +909,36 @@
     
-int jj = j;%0A                boolean newState = g%5Bi%5D%5Bj%5D.getState() ? false : true;%0A                r.setOnMousePressed(event -%3E %7B%0A                    r.setFill(Color.GRAY);%0A%0A                %7D);%0A%0A                r.setOnMouseClicked(event -%3E %7B%0A                    r.setFill(
+attachListeners(r, 
 g%5Bi
-i
 %5D%5Bj
-j%5D.getState() ? Color.WHITE : Color.STEELBLUE);%0A                    g%5Bii%5D%5Bjj%5D.setNewState(newState);%0A                    g%5Bii%5D%5Bjj%5D.updateState();%0A%0A                %7D);%0A%0A
+%5D);
 %0A   
@@ -1510,10 +1510,327 @@
 ;%0A    %7D%0A
+    %0A    private void attachListeners(Rectangle r, Cell c) %7B%0A        r.setOnMousePressed(e -%3E %7B r.setFill(Color.GRAY); %7D);%0A%0A        r.setOnMouseClicked(e -%3E %7B%0A            r.setFill(c.getState() ? Color.WHITE : Color.STEELBLUE);%0A            c.setNewState(!c.getState());%0A            c.updateState();%0A        %7D);%0A    %7D%0A
 %7D%0A

refactor FX display driver board init phase