package simulationObjects;

import controller.Grid;

/**
 * Patch that Simulates GameOfLife
 *
 * @author Will Chang
 *
 */
public class LifePatch extends Patch {

    // TODO Comment everything
    // Need to load this through the XML...
    // Edit from Constructor
    private int comfortableAmount = 3;
    private int baselineAmount = 2;

    public LifePatch() {
	super();
    }

    public LifePatch(int x, int y, Grid m) {
	super(x, y, m);

    }

    @Override
    public void prepareToUpdate() {
	processNeighborsAndUpdateState();
    }

    /**
     * Special rules for Game of Life
     */
    public void processNeighborsAndUpdateState() {
	int occupiedNeighbors = countNeighbors();

	if (occupiedNeighbors == comfortableAmount && isEmpty()) {
	    myState = State.GENERATING;
	} else if ((occupiedNeighbors > comfortableAmount || occupiedNeighbors < baselineAmount)
		&& !isEmpty()) {
	    myState = State.EMPTYING;
	}
    }

    public int countNeighbors() {
	int occupiedNeighbors = 0;
	for (Patch p : myNeighbors) {
	    if (!p.isEmpty()) {
		occupiedNeighbors++;
	    }
	}
	return occupiedNeighbors;
    }

    // TODO Update this
    // talk to Kevin about this switch
    // some duplicated code.
    @Override
    public void update() {
	// Update this
	switch (myState) {
	case OCCUPIED:
	    myCell.setFill(myCell.infoSheet.getColor("CELL"));
	    break;
	case GENERATING:
	    Cell generated = new LifeCell(xCoord, yCoord);
	    addCell(generated);
	    myState = State.OCCUPIED;
	    break;
	case EMPTYING:
	    removeCell();
	    break;
	default:
	    break;
	}
    }
}

@@ -1587,10 +1587,279 @@
 %7D%0A    %7D%0A
+    %0A    @Override%0A    public void toggleCellState() %7B%0A%09switch (myState)%7B%0A%09case OCCUPIED:%0A%09    removeCell();%0A%09    break;%0A%09case EMPTY:%0A%09    Cell generated = new LifeCell(xCoord, yCoord);%0A%09    addCell(generated);%0A%09    myState = State.OCCUPIED;%0A%09    break;%0A%09    %0A%09%7D%0A    %7D%0A
 %7D%0A

enable toggle cell in game of life