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

import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.faces.validator.Validator;

/**
 * ELECTMUC-2753 von-bis-Datumsfelder nebeneinander
 */
public class AbstractTemporalBetweenProperty extends AbstractTemporalProperty {

  private final PropertyType propertyType;

  private final AbstractTemporalProperty template;

  private final String defaultLabel;

  private final boolean valueFromRequired;

  private final Consumer<Date> setterFrom;

  private final Supplier<Date> getterFrom;

  private final boolean valueToRequired;

  private final Consumer<Date> setterTo;

  private final Supplier<Date> getterTo;

  public AbstractTemporalBetweenProperty(PropertyType propertyType, AbstractTemporalProperty template, String defaultLabel, boolean valueFromRequired, Consumer<Date> setterFrom,
      Supplier<Date> getterFrom, boolean valueToRequired, Consumer<Date> setterTo, Supplier<Date> getterTo) {
    super();

    this.propertyType = propertyType;

    this.template = template;

    this.defaultLabel = defaultLabel;

    this.valueFromRequired = valueFromRequired;
    this.setterFrom = setterFrom;
    this.getterFrom = getterFrom;

    this.valueToRequired = valueToRequired;
    this.setterTo = setterTo;
    this.getterTo = getterTo;
  }

  @Override
  public PropertyType getDefaultType() {
    return propertyType;
  }

  @Override
  public boolean getDefaultRequired() {
    return valueFromRequired || valueToRequired;
  }

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

  public boolean isValueRequired() {
    return valueFromRequired;
  }

  @Override
  public Date getValue() {
    return getterFrom.get();
  }

  @Override
  public void setValue(Date value) {
    setterFrom.accept(value);
  }

  public boolean isValue2Required() {
    return valueToRequired;
  }

  public Date getValue2() {
    return getterTo.get();
  }

  public void setValue2(Date value) {
    setterTo.accept(value);
  }

  @Override
  protected DateFormat createDateFormat(Locale locale) {
    return template.createDateFormat(locale);
  }

  @Override
  public Collection<Validator> getValidators() {
    return template.getValidators();
  }
}
