/**
   * Find additional episode files.<br>
   * adds everything which starts with "videoFile name"<br>
   * scans subs/subtitle directories aswell
   *
   * @param episode the episode
   * @param videoFile the video file
   * @param directoryContents the directory contents
   * @return indicator whether something new has been found or not
   */
  private boolean findAdditionalEpisodeFiles(
      TvShowEpisode episode, File videoFile, File[] directoryContents) {
    boolean newFileFound = false;
    List<MediaFile> existingMediaFiles = episode.getMediaFiles();

    for (File file : directoryContents) {
      if (file.isFile()) {
        MediaFile mf = new MediaFile(file);
        if (existingMediaFiles.contains(mf)) {
          continue;
        }
        if (mf.getType().equals(MediaFileType.VIDEO)
            || !mf.getBasename().startsWith(FilenameUtils.getBaseName(videoFile.getName()))
            || file.getName().startsWith(skipFilesStartingWith)) { // MacOS ignore)
          continue;
        }
        if (mf.getType() == MediaFileType.SUBTITLE) {
          episode.setSubtitles(true);
        }
        // check if it is a poster
        if (mf.getType() == MediaFileType.GRAPHIC) {
          LOGGER.debug("parsing unknown graphic " + mf.getFilename());
          String vfilename = FilenameUtils.getBaseName(videoFile.getName());
          if (vfilename.equals(FilenameUtils.getBaseName(mf.getFilename())) // basename match
              || Utils.cleanStackingMarkers(vfilename)
                  .trim()
                  .equals(FilenameUtils.getBaseName(mf.getFilename())) // basename w/o stacking
              || episode
                  .getTitle()
                  .equals(FilenameUtils.getBaseName(mf.getFilename()))) { // title match
            mf.setType(MediaFileType.THUMB);
          }
        }

        episode.addToMediaFiles(mf);
        newFileFound = true;
      } else {
        if (file.getName().equalsIgnoreCase("subs")
            || file.getName().equalsIgnoreCase("subtitle")) {
          File[] subDirContent = file.listFiles();
          if (subDirContent == null) {
            LOGGER.error("Whops. Cannot access directory: " + file.getName());
          } else {
            for (File subDirFile : subDirContent) {
              if (FilenameUtils.getBaseName(subDirFile.getName())
                  .startsWith(FilenameUtils.getBaseName(videoFile.getName()))) {
                MediaFile mf = new MediaFile(subDirFile);
                if (existingMediaFiles.contains(mf)) {
                  continue;
                }
                if (mf.getType() == MediaFileType.SUBTITLE) {
                  episode.setSubtitles(true);
                }
                episode.addToMediaFiles(mf);
                newFileFound = true;
              }
            }
          }
        }
      }
    }

    return newFileFound;
  }
  private static String generateName(
      String template, TvShow tvShow, MediaFile mf, boolean forFile) {
    String filename = "";
    List<TvShowEpisode> eps = TvShowList.getInstance().getTvEpisodesByFile(tvShow, mf.getFile());
    if (eps == null || eps.size() == 0) {
      // this should not happen, but unluckily ODB does it sometimes; try a second time to get the
      // episode
      try {
        Thread.sleep(250);
      } catch (Exception ex) {
      }
      eps = TvShowList.getInstance().getTvEpisodesByFile(tvShow, mf.getFile());
    }
    if (eps == null || eps.size() == 0) {
      return "";
    }

    if (StringUtils.isBlank(template)) {
      filename = createDestination(SETTINGS.getRenamerFilename(), tvShow, eps);
    } else {
      filename = createDestination(template, tvShow, eps);
    }

    // since we can use this method for folders too, use the next options solely for files
    if (forFile) {
      if (mf.getType().equals(MediaFileType.THUMB)) {
        if (SETTINGS.isUseRenamerThumbPostfix()) {
          filename = filename + "-thumb";
        }
        // else let the filename as is
      }
      if (mf.getType().equals(MediaFileType.FANART)) {
        filename = filename + "-fanart";
      }
      if (mf.getType().equals(MediaFileType.TRAILER)) {
        filename = filename + "-trailer";
      }
      if (mf.getType().equals(MediaFileType.VIDEO_EXTRA)) {
        String name = mf.getBasename();
        Pattern p = Pattern.compile("(?i).*([ _.-]extras[ _.-]).*");
        Matcher m = p.matcher(name);
        if (m.matches()) {
          name = name.substring(m.end(1)); // everything behind
        }
        // if not, MF must be within /extras/ folder - use name 1:1
        filename = filename + "-extras-" + name;
      }
      if (mf.getType().equals(MediaFileType.SUBTITLE)) {
        List<MediaFileSubtitle> subtitles = mf.getSubtitles();
        if (subtitles != null && subtitles.size() > 0) {
          MediaFileSubtitle mfs = mf.getSubtitles().get(0);
          if (mfs != null) {
            if (!mfs.getLanguage().isEmpty()) {
              filename = filename + "." + mfs.getLanguage();
            }
            if (mfs.isForced()) {
              filename = filename + ".forced";
            }
          } else {
            // TODO: meh, we didn't have an actual MF yet - need to parse filename ourselves (like
            // movie). But with a recent scan of files/DB this
            // should not occur.
          }
        }
      }
    } // end forFile

    // ASCII replacement
    if (SETTINGS.isAsciiReplacement()) {
      filename = StrgUtils.convertToAscii(filename, false);
    }

    filename = filename + "." + mf.getExtension(); // readd original extension

    return filename;
  }