package com.celements.navigation.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.context.Execution;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.script.service.ScriptService;

import com.celements.emptycheck.service.IEmptyCheckRole;
import com.celements.navigation.NavigationApi;
import com.celements.navigation.TreeNode;
import com.celements.navigation.cmd.ReorderSaveCommand;
import com.celements.web.service.IWebUtilsService;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.web.Utils;

@Component("treeNode")
public class TreeNodeScriptService implements ScriptService {

  private static Logger _LOGGER = LoggerFactory.getLogger(TreeNodeScriptService.class);

  @Requirement
  ITreeNodeService treeNodeService;

  @Requirement
  ITreeNodeCache treeNodeCache;

  @Requirement
  Execution execution;

  private XWikiContext getContext() {
    return (XWikiContext) execution.getContext().getProperty("xwikicontext");
  }

  public NavigationApi createNavigation() {
    return NavigationApi.createNavigation(getContext());
  }

  public void enableMappedMenuItems() {
    treeNodeService.enableMappedMenuItems();
  }

  public int getMaxConfiguredNavigationLevel() {
    return treeNodeService.getMaxConfiguredNavigationLevel();
  }

  public boolean isTreeNode(DocumentReference docRef) {
    return treeNodeService.isTreeNode(docRef);
  }

  public boolean isNavigationEnabled(String configName) {
    NavigationApi nav = NavigationApi.createNavigation(getContext());
    nav.loadConfigByName(configName);
    return nav.isNavigationEnabled();
  }

  public NavigationApi getNavigation(String configName) {
    NavigationApi nav = NavigationApi.createNavigation(getContext());
    nav.loadConfigByName(configName);
    return nav;
  }

  public String includeNavigation(String configName) {
    return getNavigation(configName).includeNavigation();
  }

  public List<TreeNode> getSubNodesForParentRef(EntityReference parentRef) {
    return treeNodeService.getSubNodesForParent(parentRef, "");
  }

  public List<TreeNode> getSubNodesForParent(EntityReference parentRef, String menuPart) {
    return treeNodeService.getSubNodesForParent(parentRef, menuPart);
  }

  public int queryCount() {
    return treeNodeCache.queryCount();
  }

  public String navReorderSave(DocumentReference docRef, String structureJSON) {
    return new ReorderSaveCommand().reorderSave(
        getWebUtilsService().getRefDefaultSerializer().serialize(docRef), structureJSON,
        getContext());
  }

  public DocumentReference getNextNonEmptyChildren(DocumentReference documentRef) {
    return getEmptyCheckService().getNextNonEmptyChildren(documentRef);
  }

  private IEmptyCheckRole getEmptyCheckService() {
    return Utils.getComponent(IEmptyCheckRole.class);
  }

  private IWebUtilsService getWebUtilsService() {
    return Utils.getComponent(IWebUtilsService.class);
  }

  public void moveTreeDocAfter(DocumentReference moveDocRef, DocumentReference insertAfterDocRef) {
    try {
      treeNodeService.moveTreeDocAfter(moveDocRef, insertAfterDocRef);
    } catch (XWikiException exp) {
      _LOGGER.error("Failed to get moveDoc [" + moveDocRef + "]", exp);
    }
  }

}

@@ -605,16 +605,77 @@
 ommand;%0A
+import com.celements.navigation.factories.NavigationFactory;%0A
 import c
@@ -1093,24 +1093,91 @@
 NodeCache;%0A%0A
+  @Requirement%0A  NavigationFactory%3CDocumentReference%3E navFactory;%0A%0A
   @Requireme
@@ -1330,92 +1330,450 @@
 %0A%0A  
-public NavigationApi createNavigation() %7B%0A    return NavigationApi.createNavigation(
+/**%0A   * @deprecated since 2.80 instead use getDefaultNavigation() or getEmptyNavigation()%0A   */%0A  @Deprecated%0A  public NavigationApi createNavigation() %7B%0A    return NavigationApi.createNavigation(getContext());%0A  %7D%0A%0A  public NavigationApi getEmptyNavigation() %7B%0A    return NavigationApi.createNavigation(getContext());%0A  %7D%0A%0A  public NavigationApi getDefaultNavigation() %7B%0A    return NavigationApi.getAPIObject(navFactory.createNavigation(), 
 getC

add getEmptyNavigation and getDefaultNavigation to TreeNodeScriptService