/**
   * This method starts the process of editing a new slide show. If a pose is already being edited,
   * it will prompt the user to save it first.
   */
  public void handleNewSlideShowRequest() {
    try {
      // WE MAY HAVE TO SAVE CURRENT WORK
      boolean continueToMakeNew = true;
      if (!saved) {
        // THE USER CAN OPT OUT HERE WITH A CANCEL
        continueToMakeNew = promptToSave();
      }

      // IF THE USER REALLY WANTS TO MAKE A NEW COURSE
      if (continueToMakeNew) {
        // RESET THE DATA, WHICH SHOULD TRIGGER A RESET OF THE UI
        SlideShowModel slideShow = ui.getSlideShow();
        slideShow.reset();
        saved = false;

        // REFRESH THE GUI, WHICH WILL ENABLE AND DISABLE
        // THE APPROPRIATE CONTROLS
        ui.updateToolbarControls(saved);

        // MAKE SURE THE TITLE CONTROLS ARE ENABLED
        ui.reloadTitleControls();
      }
    } catch (IOException ioe) {
      ErrorHandler eH = ui.getErrorHandler();
      eH.processError(LanguagePropertyType.ERROR_UNEXPECTED);
    }
  }
  /**
   * This helper method asks the user for a file to open. The user-selected file is then loaded and
   * the GUI updated. Note that if the user cancels the open process, nothing is done. If an error
   * occurs loading the file, a message is displayed, but nothing changes.
   */
  private void promptToOpen() {
    // AND NOW ASK THE USER FOR THE COURSE TO OPEN
    FileChooser slideShowFileChooser = new FileChooser();
    slideShowFileChooser.setInitialDirectory(new File(PATH_SLIDE_SHOWS));
    File selectedFile = slideShowFileChooser.showOpenDialog(ui.getWindow());

    // ONLY OPEN A NEW FILE IF THE USER SAYS OK
    if (selectedFile != null) {
      try {
        SlideShowModel slideShowToLoad = ui.getSlideShow();
        slideShowIO.loadSlideShow(slideShowToLoad, selectedFile.getAbsolutePath());
        ui.reloadSlideShowPane();
        saved = true;
        ui.updateToolbarControls(saved);
      } catch (Exception e) {
        ErrorHandler eH = ui.getErrorHandler();
        eH.processError(LanguagePropertyType.ERROR_UNEXPECTED);
      }
    }
  }
  /**
   * This method will save the current slideshow to a file. Note that we already know the name of
   * the file, so we won't need to prompt the user.
   */
  public boolean handleSaveSlideShowRequest() {
    try {
      // GET THE SLIDE SHOW TO SAVE
      SlideShowModel slideShowToSave = ui.getSlideShow();

      // SAVE IT TO A FILE
      slideShowIO.saveSlideShow(slideShowToSave);

      // MARK IT AS SAVED
      saved = true;

      // AND REFRESH THE GUI, WHICH WILL ENABLE AND DISABLE
      // THE APPROPRIATE CONTROLS
      ui.updateToolbarControls(saved);
      return true;
    } catch (IOException ioe) {
      ErrorHandler eH = ui.getErrorHandler();
      eH.processError(LanguagePropertyType.ERROR_UNEXPECTED);
      return false;
    }
  }
  /** This method will exit the application, making sure the user doesn't lose any data first. */
  public void handleExitRequest() {
    try {
      // WE MAY HAVE TO SAVE CURRENT WORK
      boolean continueToExit = true;
      if (!saved) {
        // THE USER CAN OPT OUT HERE
        continueToExit = promptToSave();
      }

      // IF THE USER REALLY WANTS TO EXIT THE APP
      if (continueToExit) {
        // EXIT THE APPLICATION
        System.exit(0);
      }
    } catch (IOException ioe) {
      ErrorHandler eH = ui.getErrorHandler();
      eH.processError(LanguagePropertyType.ERROR_UNEXPECTED);
    }
  }
  /**
   * This method lets the user open a slideshow saved to a file. It will also make sure data for the
   * current slideshow is not lost.
   */
  public void handleLoadSlideShowRequest() {
    try {
      // WE MAY HAVE TO SAVE CURRENT WORK
      boolean continueToOpen = true;
      if (!saved) {
        // THE USER CAN OPT OUT HERE WITH A CANCEL
        continueToOpen = promptToSave();
      }

      // IF THE USER REALLY WANTS TO OPEN A POSE
      if (continueToOpen) {
        // GO AHEAD AND PROCEED MAKING A NEW POSE
        promptToOpen();
      }
    } catch (IOException ioe) {
      ErrorHandler eH = ui.getErrorHandler();
      eH.processError(LanguagePropertyType.ERROR_DATA_FILE_LOADING);
    }
  }
  /**
   * This helper method verifies that the user really wants to save their unsaved work, which they
   * might not want to do. Note that it could be used in multiple contexts before doing other
   * actions, like creating a new pose, or opening another pose, or exiting. Note that the user will
   * be presented with 3 options: YES, NO, and CANCEL. YES means the user wants to save their work
   * and continue the other action (we return true to denote this), NO means don't save the work but
   * continue with the other action (true is returned), CANCEL means don't save the work and don't
   * continue with the other action (false is retuned).
   *
   * @return true if the user presses the YES option to save, true if the user presses the NO option
   *     to not save, false if the user presses the CANCEL option to not continue.
   */
  private boolean promptToSave() throws IOException {
    // PROMPT THE USER TO SAVE UNSAVED WORK
    boolean saveWork = true;

    // IF THE USER SAID YES, THEN SAVE BEFORE MOVING ON
    if (saveWork) {
      SlideShowModel slideShow = ui.getSlideShow();
      slideShowIO.saveSlideShow(slideShow);
      saved = true;
    } // IF THE USER SAID CANCEL, THEN WE'LL TELL WHOEVER
    // CALLED THIS THAT THE USER IS NOT INTERESTED ANYMORE
    else if (!true) {
      return false;
    }

    // IF THE USER SAID NO, WE JUST GO ON WITHOUT SAVING
    // BUT FOR BOTH YES AND NO WE DO WHATEVER THE USER
    // HAD IN MIND IN THE FIRST PLACE
    return true;
  }
 public void markAsEdited() {
   saved = false;
   ui.updateToolbarControls(saved);
 }
  /** This method shows the current slide show in a separate window. */
  public void handleViewSlideShowRequest(String title) {
    WebViewSlideShow viewer = new WebViewSlideShow(title, ui, slides);
    if (slides.size() == 0) {
      ErrorHandler eH = ui.getErrorHandler();
      eH.processErrorString("THERE ARE NO SLIDES");
    }

    String filePath = PATH_DATA + "sites/" + title;
    File fileDirectory = new File(filePath);
    fileDirectory.mkdir();
    try {
      if (fileDirectory.exists()) {
        File slideHTML = new File(fileDirectory, "index.html");
        File sourceHTML = new File("src/ssm/style/SlideShowHTML.html");
        slideHTML.createNewFile();

        Path source = Paths.get(sourceHTML.toURI());
        Path dest = Paths.get(slideHTML.toURI());
        Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);

        File javaScriptFile = new File(fileDirectory, "JavaScript");
        javaScriptFile.mkdir();
        File slideJS = new File(javaScriptFile, "javascript.js");
        File sourceJS = new File("src/ssm/style/SlideShowWebJavaScript.js");
        slideJS.createNewFile();

        source = Paths.get(sourceJS.toURI());
        dest = Paths.get(slideJS.toURI());
        Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);

        File cssFile = new File(fileDirectory, "CSS");
        cssFile.mkdir();
        File slideCSS = new File(cssFile, "slideCSS.css");
        File sourceCSS = new File("src/ssm/style/SlideShowCSS.css");
        slideCSS.createNewFile();

        source = Paths.get(sourceCSS.toURI());
        dest = Paths.get(slideCSS.toURI());
        Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);

        File imageFile = new File(fileDirectory, "Images");
        imageFile.mkdir();
        String imagePath = "images/slide_show_images/";

        for (int i = 0; i < slides.size(); i++) {
          BufferedImage image =
              ImageIO.read(new File(imagePath + slides.get(i).getImageFileName()));
          File outputImage = new File(imageFile, "image " + i + ".jpg");
          outputImage.createNewFile();
          ImageIO.write(image, "jpg", outputImage);
        }
      }
    } catch (IOException ex) {
      ErrorHandler eH = ui.getErrorHandler();
      eH.processError(LanguagePropertyType.ERROR_UNEXPECTED);
    }
    try {
      viewer.start();
    } catch (FileNotFoundException ex) {
      ErrorHandler eH = ui.getErrorHandler();
      eH.processError(LanguagePropertyType.ERROR_UNEXPECTED);
    } catch (MalformedURLException ex) {
      ErrorHandler eH = ui.getErrorHandler();
      eH.processError(LanguagePropertyType.ERROR_UNEXPECTED);
    }
  }