package com.commercetools.sunrise.common.pages;

import com.commercetools.sunrise.common.contexts.UserContext;
import com.commercetools.sunrise.common.models.CategoryBean;
import com.commercetools.sunrise.common.reverserouter.ProductReverseRouter;
import io.sphere.sdk.categories.Category;
import io.sphere.sdk.categories.CategoryTree;
import io.sphere.sdk.models.Base;
import play.Configuration;

import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

public class PageNavMenuFactoryImpl extends Base implements PageNavMenuFactory {

    private String saleCategoryExtId;
    @Inject
    private CategoryTree categoryTree;
    @Inject
    private UserContext userContext;
    @Inject
    private ProductReverseRouter productReverseRouter;

    @Inject
    public void initializeFields(Configuration configuration) {
        this.saleCategoryExtId = configuration.getString("common.saleCategoryExternalId");
    }

    @Override
    public PageNavMenu create() {
        return fillBean(new PageNavMenu());
    }

    protected <T extends PageNavMenu> T fillBean(final T bean) {
        final List<CategoryBean> categories = new LinkedList<>();
        categoryTree.getRoots().forEach(root -> {
            final CategoryBean categoryData = createCategoryData(root, categoryTree, userContext, productReverseRouter, saleCategoryExtId);
            categories.add(categoryData);
        });
        bean.setCategories(categories);
        return bean;
    }

    private static CategoryBean createCategoryData(final Category category, final CategoryTree categoryTree,
                                                   final UserContext userContext, final ProductReverseRouter productReverseRouter,
                                                   @Nullable final String saleCategoryExtId) {
        final CategoryBean categoryData = new CategoryBean();
        categoryData.setText(category.getName().find(userContext.locales()).orElse(""));
        categoryData.setUrl(productReverseRouter.productOverviewPageUrlOrEmpty(userContext.locale(), category));
        categoryData.setSale(Optional.ofNullable(category.getExternalId())
                .map(id -> id.equals(saleCategoryExtId))
                .orElse(false));
        categoryData.setChildren(categoryTree.findChildren(category).stream()
                .map(child -> createCategoryData(child, categoryTree, userContext, productReverseRouter, saleCategoryExtId))
                .collect(toList()));
        return categoryData;
    }
}

@@ -1106,107 +1106,252 @@
     
-return fillBean(new PageNavMenu());%0A    %7D%0A%0A    protected %3CT extends PageNavMenu%3E T fillBean(final T
+final PageNavMenu bean = new PageNavMenu();%0A        fillBean(bean);%0A        return bean;%0A    %7D%0A%0A    protected final void fillBean(final PageNavMenu bean) %7B%0A        fillCategories(bean);%0A    %7D%0A%0A    protected void fillCategories(final PageNavMenu
  bea
@@ -1705,37 +1705,16 @@
 ories);%0A
-        return bean;%0A
     %7D%0A%0A 

Fix Page nav old style