package com.celements.structEditor.fields;

import static com.celements.structEditor.classes.FormFieldEditorClass.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.reference.DocumentReference;

import com.celements.cells.attribute.AttributeBuilder;
import com.celements.model.access.exception.DocumentNotExistsException;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.xpn.xwiki.doc.XWikiDocument;

@Component(FormFieldPageType.PAGETYPE_NAME)
public class FormFieldPageType extends AbstractStructFieldPageType {

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

  public static final String PAGETYPE_NAME = "FormField";

  static final String VIEW_TEMPLATE_NAME = "FormFieldView";

  @Override
  public String getName() {
    return PAGETYPE_NAME;
  }

  @Override
  protected String getViewTemplateName() {
    return VIEW_TEMPLATE_NAME;
  }

  @Override
  public Optional<String> defaultTagName() {
    return Optional.of("form");
  }

  @Override
  public void collectAttributes(AttributeBuilder attrBuilder, DocumentReference cellDocRef) {
    attrBuilder.addCssClasses("celAddValidationToForm inactive");
    try {
      XWikiDocument cellDoc = modelAccess.getDocument(cellDocRef);
      if (modelAccess.getProperty(cellDoc, FIELD_SEND_DATA_ENCODED)) {
        attrBuilder.addNonEmptyAttribute("enctype", "multipart/form-data");
      }
      attrBuilder.addNonEmptyAttribute("action", modelAccess.getProperty(cellDoc, FIELD_ACTION));
      String method = modelAccess.getProperty(cellDoc, FIELD_METHOD);
      if (Strings.isNullOrEmpty(method)) {
        method = "post";
      }
      attrBuilder.addNonEmptyAttribute("method", method);
    } catch (DocumentNotExistsException exc) {
      LOGGER.error("cell doesn't exist '{}'", cellDocRef, exc);
    }
  }

}

@@ -397,16 +397,70 @@
 eption;%0A
+import com.celements.model.classes.fields.ClassField;%0A
 import c
@@ -1400,19 +1400,73 @@
 ;%0A      
-if 
+Optional%3CBoolean%3E sendEncodedData = Optional.fromNullable
 (modelAc
@@ -1486,24 +1486,34 @@
 rty(cellDoc,
+%0A         
  FIELD_SEND_
@@ -1526,16 +1526,53 @@
 NCODED))
+;%0A      if (sendEncodedData.or(true))
  %7B%0A     
@@ -1699,39 +1699,33 @@
 ction%22, 
-modelAccess.getProperty
+getNotEmptyString
 (cellDoc
@@ -1739,16 +1739,24 @@
 _ACTION)
+.or(%22?%22)
 );%0A     
@@ -1760,322 +1760,431 @@
     
-String method = modelAccess.getProperty(cellDoc, FIELD_METHOD);%0A      if (Strings.isNullOrEmpty(method)) %7B%0A        method = %22post%22;%0A      %7D%0A      attrBuilder.addNonEmptyAttribute(%22method%22, method);%0A    %7D catch (DocumentNotExistsException exc) %7B%0A      LOGGER.error(%22cell doesn't exist '%7B%7D'%22, cellDocRef, exc);%0A    %7D
+attrBuilder.addNonEmptyAttribute(%22method%22, getNotEmptyString(cellDoc, FIELD_METHOD).or(%0A          %22post%22));%0A    %7D catch (DocumentNotExistsException exc) %7B%0A      LOGGER.error(%22cell doesn't exist '%7B%7D'%22, cellDocRef, exc);%0A    %7D%0A  %7D%0A%0A  Optional%3CString%3E getNotEmptyString(XWikiDocument cellDoc, ClassField%3CString%3E classField) %7B%0A    return Optional.fromNullable(Strings.emptyToNull(modelAccess.getProperty(cellDoc, classField)));
 %0A  %7D

fix NPE if sendEncodedData Boolean is null