package com.celements.structEditor.fields;

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

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.celements.structEditor.classes.StructuredDataEditorClass;
import com.google.common.base.Optional;

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

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

  public static final String PAGETYPE_NAME = "OptionTag";

  static final String VIEW_TEMPLATE_NAME = "OptionTagView";

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

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

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

  @Override
  public void collectAttributes(AttributeBuilder attrBuilder, DocumentReference cellDocRef) {
    try {
      // String cellValueStr = getStructDataEditorService().getCellValueAsString(cellDocRef).or("");
      // if (!Strings.isNullOrEmpty(cellValueStr) &&
      // (cellValueStr.equals(getNotEmptyString(cellDocRef,
      // FIELD_VALUE).or("")))) {
      // attrBuilder.addEmptyAttribute("selected");
      // } else if (Strings.isNullOrEmpty(cellValueStr) && getFieldValue(cellDocRef,
      // FIELD_SELECTED).or(false)) {
      // attrBuilder.addEmptyAttribute("selected");
      // }
      Optional<DocumentReference> selectTagDocRef = getStructDataEditorService().getSelectTagDocumentReference(
          cellDocRef);
      System.out.println("\nOptionTagPageType getSelectTagName selectTagDocRef: "
          + selectTagDocRef);
      if (selectTagDocRef.isPresent()) {
        Optional<String> selectValue = getNotEmptyString(selectTagDocRef.get(),
            StructuredDataEditorClass.FIELD_EDIT_FIELD_NAME);
        System.out.println("OptionTagPageType getSelectTagName selectValue: " + selectValue);
        System.out.println("OptionTagPageType getSelectTagName cellValue: "
            + getStructDataEditorService().getCellValueAsString(cellDocRef,
                modelContext.getDoc()).get());
        if (selectValue.equals(getStructDataEditorService().getCellValueAsString(cellDocRef,
            modelContext.getDoc()).get())) {
          attrBuilder.addEmptyAttribute("selected");
        }
      }
      if (getFieldValue(cellDocRef, FIELD_DISABLED).or(false)) {
        attrBuilder.addEmptyAttribute("disabled");
      }
      attrBuilder.addNonEmptyAttribute("value", getNotEmptyString(cellDocRef, FIELD_VALUE).or(""));
      attrBuilder.addNonEmptyAttribute("label", getNotEmptyString(cellDocRef, FIELD_LABEL).or(""));
    } catch (DocumentNotExistsException exc) {
      LOGGER.error("cell doesn't exist '{}'", cellDocRef, exc);
    }
  }

}

@@ -401,77 +401,8 @@
 on;%0A
-import com.celements.structEditor.classes.StructuredDataEditorClass;%0A
 impo
@@ -1140,30 +1140,51 @@
     
-// String cellValueStr
+Optional%3CDocumentReference%3E selectTagDocRef
  = g
@@ -1218,480 +1218,217 @@
 .get
-CellValueAsString(cellDocRef).or(%22%22);%0A      // if (!Strings.isNullOrEmpty(cellValueStr) &&
+SelectTagDocumentReference(%0A          cellDocRef);
 %0A      
-// (cellValueStr.equals(getNotEmptyString(cellDocRef,%0A      // FIELD_VALUE).or(%22%22)))) %7B%0A      // attrBuilder.addEmptyAttribute(%22selected%22);%0A      // %7D else if (Strings.isNullOrEmpty(cellValueStr) && getFieldValue(cellDocRef,%0A      // FIELD_SELECTED).or(false)) %7B%0A      // attrBuilder.addEmptyAttribute(%22selected%22);%0A      // %7D%0A      Optional%3CDocumentReference%3E selectTagDocRef
+if (selectTagDocRef.isPresent()) %7B%0A        Optional%3CString%3E optionValue = getNotEmptyString(cellDocRef, FIELD_VALUE);%0A        Optional%3CString%3E cellValue
  = g
@@ -1462,351 +1462,83 @@
 .get
-SelectTagDocumentReference(%0A          cellDocRef);%0A      System.out.println(%22%5CnOptionTagPageType getSelectTagName selectTagDocRef: %22%0A          + selectTagDocRef);%0A      if (selectTagDocRef.isPresent()) %7B%0A        Optional%3CString%3E selectValue = getNotEmptyString(selectTagDocRef.get(),%0A            StructuredDataEditorClass.FIELD_EDIT_FIELD_NAME
+CellValueAsString(%0A            selectTagDocRef.get(), modelContext.getDoc()
 );%0A 
@@ -1556,32 +1556,34 @@
 em.out.println(%22
+%5Cn
 OptionTagPageTyp
@@ -1601,22 +1601,20 @@
 TagName 
-select
+cell
 Value: %22
@@ -1616,22 +1616,20 @@
 ue: %22 + 
-select
+cell
 Value);%0A
@@ -1691,28 +1691,30 @@
 TagName 
-cell
+option
 Value: %22
 %0A       
@@ -1709,212 +1709,258 @@
 e: %22
-%0A            + getStructDataEditorService().getCellValueAsString(cellDocRef,%0A                modelContext.getDoc()).get());%0A        if (selectValue.equals(getStructDataEditorService().getCell
+ + optionValue);%0A        if (cellValue.isPresent() && optionValue.isPresent() && cellValue.get().equals(%0A            optionValue.get())) %7B%0A          attrBuilder.addEmptyAttribute(%22selected%22);%0A        %7D else if (!cellValue.isPresent() && getField
 Value
-AsString
 (cel
@@ -1971,49 +1971,33 @@
 Ref,
-%0A            modelContext.getDoc()).get()
+ FIELD_SELECTED).or(false
 )) %7B

set correct select attribute