class BookmarkableUrlHelper {

    public static String registerOpenedScreen(String bookmarkableUrl,
                                              final PlaceRequest placeRequest) {
        final String screenName = placeRequest.getFullIdentifier();
        final String closedScreen = CLOSED_PREFIX.concat(screenName);
        final String currentBookmarkableUrl = bookmarkableUrl;

        if (screenWasClosed(bookmarkableUrl,
                            closedScreen)) {
            bookmarkableUrl = bookmarkableUrl.replace(closedScreen,
                                                      screenName);
        } else if (!isPerspectiveInUrl(bookmarkableUrl)) {
            // must add the screen in the group of the current perspective (which is not yet loaded)
            if (isNotBlank(bookmarkableUrl)) {
                bookmarkableUrl = bookmarkableUrl.concat(SEPARATOR).concat(screenName);
            } else {
                bookmarkableUrl = screenName;
            }
        } else {
            // this is a screen outside the current perspective
            if (!urlContainsExtraPerspectiveScreen(bookmarkableUrl)) {
                bookmarkableUrl = bookmarkableUrl.concat(OTHER_SCREEN_SEP).concat(screenName);
            } else {
                bookmarkableUrl = bookmarkableUrl.concat(SEPARATOR).concat(screenName);
            }
        }
        if (isBiggerThenMaxURLSize(bookmarkableUrl)) {
            return currentBookmarkableUrl;
        }
        return bookmarkableUrl;
    }

}