/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.celements.cells;

import java.util.List;

import org.xwiki.model.reference.SpaceReference;

import com.celements.navigation.TreeNode;
import com.celements.navigation.service.ITreeNodeService;
import com.celements.web.service.IWebUtilsService;
import com.xpn.xwiki.web.Utils;

public class RenderingEngine implements IRenderingEngine {

  private IRenderStrategy renderStrategy;

  ITreeNodeService treeNodeService;
  IWebUtilsService webUtilsService;

  public RenderingEngine() {
  }

  /* (non-Javadoc)
   * @see com.celements.web.cells.IRenderingEngine#renderCell(
   *   com.xpn.xwiki.objects.BaseObject)
   */
  public void renderCell(TreeNode node) {
    renderStrategy.startRendering();
    //isFirst AND isLast because only this item and its children is
    //rendered (NO SIBLINGS!)
    internal_renderCell(node, true, true);
    renderStrategy.endRendering();
  }

  /* (non-Javadoc)
   * @see com.celements.web.cells.IRenderingEngine#renderSubCells(java.lang.String)
   */
  @Deprecated
  public void renderPageLayout(String spaceName) {
    SpaceReference spaceRef = getWebUtilsService().resolveSpaceReference(spaceName);
    renderPageLayout(spaceRef);
  }

  /* (non-Javadoc)
   * @see com.celements.web.cells.IRenderingEngine#renderSubCells(
   *      org.xwiki.model.reference.SpaceReference)
   */
  public void renderPageLayout(SpaceReference spaceRef) {
    renderStrategy.startRendering();
    renderStrategy.setSpaceReference(spaceRef);
    internal_renderSubCells("");
    renderStrategy.endRendering();
  }

  void internal_renderCell(TreeNode node, boolean isFirstItem, boolean isLastItem) {
    if(renderStrategy.isRenderCell(node)) {
      renderStrategy.startRenderCell(node, isFirstItem, isLastItem);
      internal_renderSubCells(node.getFullName());
      renderStrategy.endRenderCell(node, isFirstItem, isLastItem);
    }
  }

  void internal_renderSubCells(String parent) {
    if(renderStrategy.isRenderSubCells(parent)) {
      List<TreeNode> children = getTreeNodeService().getSubNodesForParent(parent,
          renderStrategy.getMenuSpace(parent), renderStrategy.getMenuPart(parent));
      if (children.size() > 0) {
        renderStrategy.startRenderChildren(parent);
        boolean isFirstItem = true;
        for (TreeNode node : children) {
          boolean isLastItem = (children.lastIndexOf(node)
              == (children.size() - 1));
            internal_renderCell(node, isFirstItem, isLastItem);
          isFirstItem = false;
        }
        renderStrategy.endRenderChildren(parent);
      } else {
        renderStrategy.renderEmptyChildren(parent);
      }
    }
  }

  public RenderingEngine setRenderStrategy(IRenderStrategy newStrategy) {
    this.renderStrategy = newStrategy;
    return this;
  }

  ITreeNodeService getTreeNodeService() {
    if (treeNodeService != null) {
      return treeNodeService;
    }
    return Utils.getComponent(ITreeNodeService.class);
  }

  IWebUtilsService getWebUtilsService() {
    if (webUtilsService != null) {
      return webUtilsService;
    }
    return Utils.getComponent(IWebUtilsService.class);
  }

}

@@ -941,16 +941,101 @@
 .List;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -1075,16 +1075,16 @@
 erence;%0A
-
 %0Aimport 
@@ -1320,16 +1320,107 @@
 gine %7B%0A%0A
+  private static Log LOGGER = LogFactory.getFactory().getInstance(RenderingEngine.class);%0A%0A
   privat
@@ -2444,24 +2444,99 @@
 spaceRef) %7B%0A
+    LOGGER.debug(%22renderPageLayout: start rendering %5B%22 + spaceRef + %22%5D.%22);%0A
     renderSt
@@ -3188,16 +3188,16 @@
 parent,%0A
-
         
@@ -3268,24 +3268,172 @@
 t(parent));%0A
+      LOGGER.debug(%22internal_renderSubCells: for parent %5B%22 + parent%0A          + %22%5D render %5B%22 + children.size() + %22%5D children %5B%22 + children + %22%5D.%22);%0A
       if (ch

add logging to RenderingEngine