package com.celements.web.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.script.service.ScriptService;

import com.celements.navigation.cmd.DeleteMenuItemCommand;
import com.celements.web.plugin.api.CelementsWebPluginApi;
import com.celements.web.plugin.cmd.PlainTextCommand;
import com.celements.web.sajson.Builder;
import com.xpn.xwiki.XWikiContext;

@Component("celementsweb")
public class CelementsWebScriptService implements ScriptService {

  private static final String APP_SCRIPT_XPAGE = "app";

  private static Log LOGGER = LogFactory.getFactory().getInstance(
      CelementsWebPluginApi.class);

  @Requirement
  Execution execution;

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

  public String getAppScriptURL(String scriptName) {
    return getAppScriptURL(scriptName, "");
  }

  public String getAppScriptURL(String scriptName, String queryString) {
    if (queryString == null) {
      queryString = "";
    }
    if (!"".equals(queryString)) {
      queryString = "&" + queryString;
    }
    return getContext().getDoc().getURL("view", "xpage=" + APP_SCRIPT_XPAGE + "&s="
        + scriptName + queryString, getContext());
  }

  public boolean isAppScriptCurrentPage(String scriptName) {
    String scriptStr = getScriptNameFromURL();
    return (!"".equals(scriptStr) && (scriptStr.equals(scriptName)));
  }

  public String getScriptNameFromURL() {
    String scriptStr = "";
    if (isAppScriptRequest()) {
      scriptStr = getAppScriptNameFromRequestURL();
    }
    return scriptStr;
  }

  public boolean isAppScriptRequest() {
    String xpageStr = getContext().getRequest().getParameter("xpage");
    return APP_SCRIPT_XPAGE.equals(xpageStr)
        && (getAppScriptNameFromRequestURL() != null);
  }

  private String getAppScriptNameFromRequestURL() {
    return getContext().getRequest().getParameter("s");
  }

  public String getCurrentPageURL(String queryString) {
    if(isAppScriptRequest()) {
      return getAppScriptURL(getScriptNameFromURL(), queryString);
    } else {
      return "?" + queryString;
    }
  }

  public String convertToPlainText(String htmlContent) {
    LOGGER.trace("convertToPlainText called on celementsweb script service for ["
        + htmlContent + "].");
    return new PlainTextCommand().convertToPlainText(htmlContent);
  }

  public Builder getNewJSONBuilder() {
    return new Builder();
  }

  public boolean deleteMenuItem(DocumentReference docRef) {
    return new DeleteMenuItemCommand().deleteMenuItem(docRef);
  }

}

@@ -250,16 +250,78 @@
 cution;%0A
+import org.xwiki.model.internal.scripting.ModelScriptService;%0A
 import o
@@ -360,24 +360,24 @@
 tReference;%0A
-
 import org.x
@@ -659,16 +659,53 @@
 Context;
+%0Aimport com.xpn.xwiki.XWikiException;
 %0A%0A@Compo
@@ -968,16 +968,16 @@
 irement%0A
-
   Execut
@@ -992,16 +992,67 @@
 ution;%0A%0A
+  @Requirement%0A  ModelScriptService modelService;%0A%0A
   privat
@@ -2906,65 +2906,478 @@
     
-return new DeleteMenuItemCommand().deleteMenuItem(docRef)
+String docFN = modelService.serialize(docRef, %22local%22);%0A    try %7B%0A      if (getContext().getWiki().getRightService().hasAccessLevel(%22edit%22,%0A          getContext().getUser(), docFN, getContext())) %7B%0A        return new DeleteMenuItemCommand().deleteMenuItem(docRef);%0A      %7D%0A    %7D catch (XWikiException exp) %7B%0A      LOGGER.error(%22Failed to check 'edit' access rights for user %5B%22%0A          + getContext().getUser() + %22%5D on document %5B%22 + docFN + %22%5D%22);%0A    %7D%0A    return false
 ;%0A  

add missing access rights check