/**
   * Opens an index entry in the booklet.
   *
   * @param resetTextField will clear the text in the searchField and reset the search entry's data
   */
  @SuppressWarnings("unchecked")
  public static void openIndexEntry(
      GuiBooklet booklet, BookletEntry entry, int page, boolean resetTextField) {
    booklet.searchField.setVisible(entry instanceof BookletEntryAllSearch);
    booklet.searchField.setFocused(entry instanceof BookletEntryAllSearch);
    if (resetTextField) {
      booklet.searchField.setText("");
      if (entry instanceof BookletEntryAllSearch) {
        entry.chapters =
            (ArrayList<BookletChapter>) ((BookletEntryAllSearch) entry).allChapters.clone();
      }
    }

    booklet.currentPage = null;
    booklet.currentChapter = null;

    booklet.currentIndexEntry = entry;
    booklet.indexPageAmount =
        entry == null ? 1 : entry.chapters.size() / booklet.chapterButtons.length + 1;
    booklet.pageOpenInIndex =
        entry == null
            ? 1
            : (booklet.indexPageAmount <= page || page <= 0 ? booklet.indexPageAmount : page);

    booklet.buttonPreviousScreen.visible = entry != null;
    booklet.buttonForward.visible = booklet.pageOpenInIndex < booklet.indexPageAmount;
    booklet.buttonBackward.visible = booklet.pageOpenInIndex > 1;

    for (int i = 0; i < booklet.chapterButtons.length; i++) {
      IndexButton button = (IndexButton) booklet.chapterButtons[i];
      if (entry == null) {
        boolean entryExists = InitBooklet.entries.size() > i;
        button.visible = entryExists;
        if (entryExists) {
          button.displayString = InitBooklet.entries.get(i).getNameWithColor();
          button.chap = null;
        }
      } else {
        boolean entryExists =
            entry.chapters.size()
                > i
                    + (booklet.chapterButtons.length * booklet.pageOpenInIndex
                        - booklet.chapterButtons.length);
        button.visible = entryExists;
        if (entryExists) {
          BookletChapter chap =
              entry.chapters.get(
                  i
                      + (booklet.chapterButtons.length * booklet.pageOpenInIndex
                          - booklet.chapterButtons.length));
          button.displayString = chap.getNameWithColor();
          button.chap = chap;
        }
      }
    }
  }
  /**
   * Opens a chapter in the booklet. Can only be done when the chapter is not null and an index
   * entry is opened in the booklet
   */
  public static void openChapter(GuiBooklet booklet, BookletChapter chapter, BookletPage page) {
    if (chapter == null || booklet.currentIndexEntry == null) {
      return;
    }

    booklet.searchField.setVisible(false);
    booklet.searchField.setFocused(false);
    booklet.searchField.setText("");

    booklet.currentChapter = chapter;
    booklet.currentPage =
        page != null && doesChapterHavePage(chapter, page) ? page : chapter.pages[0];

    booklet.buttonForward.visible = getNextPage(chapter, booklet.currentPage) != null;
    booklet.buttonBackward.visible = getPrevPage(chapter, booklet.currentPage) != null;
    booklet.buttonPreviousScreen.visible = true;

    for (GuiButton chapterButton : booklet.chapterButtons) {
      chapterButton.visible = false;
    }
  }
  /** Called when the "previous page"-button is pressed */
  public static void handlePreviousPage(GuiBooklet booklet) {
    if (booklet.currentIndexEntry != null) {
      if (booklet.currentPage != null) {
        BookletPage page = getPrevPage(booklet.currentChapter, booklet.currentPage);
        if (page != null) {
          booklet.currentPage = page;
        }

        booklet.buttonForward.visible =
            getNextPage(booklet.currentChapter, booklet.currentPage) != null;
        booklet.buttonBackward.visible =
            getPrevPage(booklet.currentChapter, booklet.currentPage) != null;
      } else {
        openIndexEntry(
            booklet,
            booklet.currentIndexEntry,
            booklet.pageOpenInIndex - 1,
            !(booklet.currentIndexEntry instanceof BookletEntryAllSearch));
      }
    }
  }