package com.celements.structEditor;

import java.util.HashMap;
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.structEditor.classes.StructEditorClass;
import com.celements.structEditor.classes.StructuredDataEditorClasses;
import com.celements.structEditor.classes.TextAreaFieldEditorClass;
import com.xpn.xwiki.objects.BaseObject;

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

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

  @Requirement(StructuredDataEditorClasses.CLASS_DEF_HINT)
  private StructEditorClass structuredDataEditorClass;

  @Requirement(TextAreaFieldEditorClass.CLASS_DEF_HINT)
  private StructEditorClass textAreaFieldEditorClass;

  @Requirement
  protected IModelAccessFacade modelAccess;

  public String getDictionaryKey(DocumentReference cellDocRef) {
    String retVal = new String();
    DocumentReference structuredDataEditorClassRef = structuredDataEditorClass.getClassRef(
        cellDocRef.getWikiReference());
    BaseObject structuredDataEditorConfig;
    try {
      structuredDataEditorConfig = modelAccess.getXObject(cellDocRef, structuredDataEditorClassRef);
      retVal = structuredDataEditorConfig.getStringValue("edit_field_class_fullname") + "_"
          + structuredDataEditorConfig.getStringValue("edit_field_name");
    } catch (DocumentNotExistsException exc) {
      LOGGER.error("Document {} or Document {} does not exist {}", structuredDataEditorClassRef,
          exc);
    }
    return retVal;
  }

  public Map<String, Integer> getRowsAndColsFromTextarea(DocumentReference cellDocRef) {
    Map<String, Integer> retMap = new HashMap<String, Integer>();
    BaseObject textAreaFieldConfig;
    DocumentReference textAreaFieldClassRef = textAreaFieldEditorClass.getClassRef(
        cellDocRef.getWikiReference());
    try {
      textAreaFieldConfig = modelAccess.getXObject(cellDocRef, textAreaFieldClassRef);
      retMap.put("rows", textAreaFieldConfig.getIntValue("textarea_field_rows"));
      retMap.put("cols", textAreaFieldConfig.getIntValue("textarea_field_cols"));
    } catch (DocumentNotExistsException exc) {
      LOGGER.error("Document {} or Document {} does not exist {}", textAreaFieldClassRef, exc);
    }
    return retMap;
  }
}

@@ -2688,11 +2688,604 @@
 Map;%0A  %7D
+%0A%0A  public String getValueFromTextArea(DocumentReference cellDocRef) %7B%0A    String retVal = new String();%0A    BaseObject textAreaFieldConfig;%0A    DocumentReference textAreaFieldClassRef = textAreaFieldEditorClass.getClassRef(%0A        cellDocRef.getWikiReference());%0A    try %7B%0A      textAreaFieldConfig = modelAccess.getXObject(cellDocRef, textAreaFieldClassRef);%0A      retVal = textAreaFieldConfig.getStringValue(%22textarea_field_value%22);%0A    %7D catch (DocumentNotExistsException exc) %7B%0A      LOGGER.error(%22Document %7B%7D does not exist %7B%7D%22, textAreaFieldClassRef, exc);%0A    %7D%0A    return retVal;%0A  %7D
 %0A%7D%0A

add method getValueFromTextArea and use it in the view