/**
   * When saving a map, we are looping through the map that was built and adding the fixed roads to
   * a new map. To prevent adding the same bits of roads again, delete all the bits from the already
   * added road
   *
   * @param oldMap the map from which to delete some number of components
   * @param start the start coordinate from where to begin deleting components
   * @param end the end coordinate to which we must delete all components
   */
  private void deleteFromOldMap(Map oldMap, Coordinate start, Coordinate end) {
    int startX = start.getX();
    int startY = start.getY();
    int endX = end.getX();
    int endY = end.getY();

    if (startY == endY) { // horizontal
      for (int i = startX; i <= endX; i++) {
        oldMap.clearCell(new Coordinate(i, startY));
      }
    } else { // vertical
      for (int i = startY; i <= endY; i++) {
        oldMap.clearCell(new Coordinate(startX, i));
      }
    }
  }
  /**
   * Adds a "grass" component to the map, aka a null component Useful if the user wants to delete a
   * map component they placed in the map
   *
   * @param x the x coordinate where to add the grass
   * @param y the y coordinate where to add the grass
   * @param map the map to add the grass to
   * @param mapGridGUIDecorator the GUI decorator associated with this map
   * @param mapGridPane the gridPane that would need to be updated with the new view
   * @param imgView the associated image to place in the x,y cell
   */
  private void addGrass(
      int x,
      int y,
      Map map,
      MapGridGUIDecorator mapGridGUIDecorator,
      GridPane mapGridPane,
      ImageView imgView) {
    Coordinate coord = new Coordinate(x, y);
    map.clearCell(coord);

    StackPane sp = mapGridGUIDecorator.redrawCell(x, y, mapGridPane);

    sp.setOnMouseClicked(
        click -> {
          ComponentType currentFocused = MapMakerController.getCurrentFocused();
          if (currentFocused == ComponentType.INTERSECTION) {
            addIntersection(x, y, map, mapGridGUIDecorator, mapGridPane, intersectionImgView);
          } else if (currentFocused == ComponentType.ROADNS) {
            addRoadNS(x, y, map, mapGridGUIDecorator, mapGridPane, roadNSImgView);
          } else if (currentFocused == ComponentType.ROADEW) {
            addRoadEW(x, y, map, mapGridGUIDecorator, mapGridPane, roadEWImgView);
          }
        });

    // put focus back on Grass
    MapMakerController.setPreviousFocused(MapMakerController.getCurrentFocused());
    MapMakerController.setCurrentFocused(ComponentType.GRASS);
    imgView.requestFocus();
  }