package com.celements.model.classes.fields;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.validation.constraints.NotNull;

import com.xpn.xwiki.objects.classes.BooleanClass;
import com.xpn.xwiki.objects.classes.PropertyClass;

@Immutable
public final class BooleanField extends AbstractClassField<Boolean> {

  private final String displayType;
  private final Integer defaultValue;

  public static class Builder extends AbstractClassField.Builder<Builder, Boolean> {

    private String displayType;
    private Integer defaultValue;

    public Builder(@NotNull String classDefName, @NotNull String name) {
      super(classDefName, name);
    }

    @Override
    public Builder getThis() {
      return this;
    }

    public Builder displayType(@Nullable String val) {
      displayType = val;
      return getThis();
    }

    public Builder defaultValue(@Nullable Integer val) {
      defaultValue = val;
      return getThis();
    }

    @Override
    public BooleanField build() {
      return new BooleanField(getThis());
    }
  }

  protected BooleanField(@NotNull Builder builder) {
    super(builder);
    this.displayType = builder.displayType;
    this.defaultValue = builder.defaultValue;
  }

  @Override
  public Class<Boolean> getType() {
    return Boolean.class;
  }

  public String getDisplayType() {
    return displayType;
  }

  public Integer getDefaultValue() {
    return defaultValue;
  }

  @Override
  protected PropertyClass getPropertyClass() {
    BooleanClass element = new BooleanClass();
    if (displayType != null) {
      element.setDisplayType(displayType);
    }
    if (defaultValue != null) {
      element.setDefaultValue(defaultValue);
    }
    return element;
  }

}

@@ -164,16 +164,61 @@
 tNull;%0A%0A
+import com.google.common.base.Preconditions;%0A
 import c
@@ -387,24 +387,65 @@
 eld%3CBoolean%3E
+ implements%0A    CustomClassField%3CBoolean%3E
  %7B%0A%0A  privat
@@ -1846,10 +1846,359 @@
 t;%0A  %7D%0A%0A
+  @Override%0A  public Object serialize(Boolean value) %7B%0A    if (value == null) %7B%0A      return null;%0A    %7D%0A    return value ? 1 : 0;%0A  %7D%0A%0A  @Override%0A  public Boolean resolve(Object obj) %7B%0A    if (obj == null) %7B%0A      return null;%0A    %7D%0A    Preconditions.checkArgument(obj.getClass().equals(Integer.class));%0A    return ((Integer) obj).equals(1);%0A  %7D%0A%0A
 %7D%0A

implement CustomMapping for BooleanField