package com.celements.structEditor;

import java.util.LinkedHashMap;
import java.util.Map;

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

import com.celements.model.access.IModelAccessFacade;
import com.celements.model.access.exception.DocumentNotExistsException;
import com.celements.model.classes.fields.ClassField;
import com.celements.model.context.ModelContext;
import com.celements.structEditor.classes.TextAreaFieldEditorClass;
import com.google.common.base.Optional;
import com.xpn.xwiki.doc.XWikiDocument;

@Component("structuredDataEditor")
public class StructuredDataEditorScriptService implements ScriptService {

  private static Logger LOGGER = LoggerFactory.getLogger(StructuredDataEditorScriptService.class);

  @Requirement
  StructuredDataEditorService service;

  @Requirement
  IModelAccessFacade modelAccess;

  @Requirement
  ModelContext context;

  public String getPrettyName(DocumentReference cellDocRef) {
    String prettyName = "";
    if (cellDocRef != null) {
      try {
        prettyName = service.getPrettyName(cellDocRef).or("");
      } catch (DocumentNotExistsException exc) {
        LOGGER.error("cell doesn't exist '{}'", cellDocRef, exc);
      }
    }
    return prettyName;
  }

  public Map<String, String> getTextAttributes(DocumentReference cellDocRef) {
    Map<String, String> retMap = new LinkedHashMap<>();
    try {
      XWikiDocument cellDoc = modelAccess.getDocument(cellDocRef);
      retMap.put("type", "text");
      addNameAttributeToMap(retMap, cellDoc);
      retMap.put("value", context.getDoc().getTemplate());
    } catch (DocumentNotExistsException exc) {
      LOGGER.error("Properties for docRef {} does not exist {}", cellDocRef, exc);
    }
    return retMap;
  }

  public Map<String, String> getTextAreaAttributes(DocumentReference cellDocRef) {
    Map<String, String> retMap = new LinkedHashMap<>();
    try {
      XWikiDocument cellDoc = modelAccess.getDocument(cellDocRef);
      addNameAttributeToMap(retMap, cellDoc);
      addAttributeToMap(retMap, "rows", cellDoc, TextAreaFieldEditorClass.FIELD_ROWS);
      addAttributeToMap(retMap, "cols", cellDoc, TextAreaFieldEditorClass.FIELD_COLS);
    } catch (DocumentNotExistsException exc) {
      LOGGER.error("Properties for docRef {} does not exist {}", cellDocRef, exc);
    }
    return retMap;
  }

  public String getTextAreaContent(DocumentReference cellDocRef) {
    String retVal = new String();
    try {
      retVal = modelAccess.getProperty(cellDocRef, TextAreaFieldEditorClass.FIELD_VALUE);
    } catch (DocumentNotExistsException exc) {
      LOGGER.error("Properties for docRef {} does not exist {}", cellDocRef, exc);
    }
    return retVal;
  }

  private void addAttributeToMap(Map<String, String> map, String attrName, XWikiDocument cellDoc,
      ClassField<?> field) {
    Object val = modelAccess.getProperty(cellDoc, field);
    if (val != null) {
      map.put(attrName, val.toString());
    }
  }

  private void addNameAttributeToMap(Map<String, String> map, XWikiDocument cellDoc) {
    Optional<String> val = service.getAttributeName(cellDoc);
    if (val.isPresent()) {
      map.put("name", val.toString());
    }
  }

  public String getCellValueAsString(DocumentReference cellDocRef) {
    String retVal = new String();
    try {
      Optional<String> cellValue = service.getCellValueAsString(cellDocRef);
      if (cellValue.isPresent()) {
        retVal = cellValue.get();
      }
    } catch (DocumentNotExistsException exc) {
      LOGGER.error("cell doesn't exist '{}'", cellDocRef, exc);
    }
    return retVal;
  }
}

@@ -3504,34 +3504,14 @@
     
-Optional%3CString%3E cell
+ret
 Val
-ue
  = s
@@ -3553,86 +3553,16 @@
 Ref)
-;%0A      if (cellValue.isPresent()) %7B%0A        retVal = cellValue.get();%0A      %7D
+.or(%22%22);
 %0A   

add unimplemented method to TestXWikiDocumentCreator