
package de.ivu.elect.jee.presentation.dynamicform;

import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.annotation.Nullable;

public class LambdaLongProperty extends AbstractLongProperty {

  private final String defaultLabel;

  private final Long defaultValue;

  private final boolean defaultRequired;

  private final Consumer<Long> setter;

  private final Supplier<Long> getter;

  private final Long minValue;

  private final Long maxValue;

  /**
   * @deprecated ELECT-13767
   */
  @Deprecated
  private final Integer maxLength;

  public LambdaLongProperty(String defaultLabel, boolean defaultRequired, Consumer<Long> setter, Supplier<Long> getter) {
    this(defaultLabel, defaultRequired, setter, getter, null, null);
  }

  public LambdaLongProperty(String defaultLabel, boolean defaultRequired, Consumer<Long> setter, Supplier<Long> getter, @Nullable Long minValue, @Nullable Long maxValue) {
    this(defaultLabel, null, defaultRequired, setter, getter, minValue, maxValue);
  }

  public LambdaLongProperty(String defaultLabel, Long defaultValue, boolean defaultRequired, Consumer<Long> setter, Supplier<Long> getter, @Nullable Long minValue, @Nullable Long maxValue) {
    this(defaultLabel, defaultValue, defaultRequired, setter, getter, minValue, maxValue, null);
  }

  public LambdaLongProperty(String defaultLabel, Long defaultValue, boolean defaultRequired, Consumer<Long> setter, Supplier<Long> getter, @Nullable Long minValue, @Nullable Long maxValue,
      @Nullable Integer maxLength) {
    super();

    this.defaultLabel = defaultLabel;
    this.defaultValue = defaultValue;
    this.defaultRequired = defaultRequired;

    this.setter = setter;
    this.getter = getter;

    this.minValue = minValue;
    this.maxValue = maxValue;

    this.maxLength = maxLength;
  }

  @Override
  public Long getDefaultValue() {
    return defaultValue;
  }

  @Override
  public Long getValue() {
    return getter.get();
  }

  @Override
  public void setValue(Long value) {
    setter.accept(value);
  }

  @Override
  public String getDefaultLabel() {
    return defaultLabel;
  }

  @Override
  public boolean getDefaultRequired() {
    return defaultRequired;
  }

  @Override
  public long getMinValue() {
    if (minValue == null) {
      return super.getMinValue();
    }

    return minValue;
  }

  @Override
  public long getMaxValue() {
    if (maxValue == null) {
      return super.getMaxValue();
    }

    return maxValue;
  }

  @Override
  public int getMaxLength() {
    if (maxLength == null) {
      return super.getMaxLength();
    }

    return maxLength;
  }
}
