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

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

import java.util.Date;

public class TimestampDateProperty extends AbstractDateProperty {

  public static final String DEFAULT_LABEL_FORMAT_MSG_KEY = "TimestampDateProperty.default.label.format";

  private final AbstractTimestampProperty targetProperty;

  public TimestampDateProperty(AbstractTimestampProperty targetProperty) {
    super();

    this.targetProperty = targetProperty;
  }

  @Override
  public boolean getDefaultRequired() {
    return targetProperty.getDefaultRequired();
  }

  @Override
  public boolean isReadonly() {
    return targetProperty.isReadonly();
  }

  @Override
  public void setReadonly(boolean readonly) {
    targetProperty.setReadonly(readonly);
  }

  @Override
  public Date getDefaultValue() {
    return targetProperty.getDefaultValue();
  }

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

  @Override
  public void setValue(Date value) {
    if (nonNull(value)) {
      targetProperty.setValue(ueberschreibeZeitanteil(value, targetProperty.getValue()));
    } else {
      targetProperty.setValue(null);
    }
  }

  private Date ueberschreibeZeitanteil(Date value, Date zeitanteil) {
    Date result = new Date(value.getTime());

    if (isNull(zeitanteil)) {
      result.setHours(0);
      result.setMinutes(0);
      result.setSeconds(0);
    } else {
      result.setHours(zeitanteil.getHours());
      result.setMinutes(zeitanteil.getMinutes());
      result.setSeconds(zeitanteil.getSeconds());
    }

    return result;
  }

  @Override
  public String getDefaultLabel() {
    return getMessage(getDefaultLabelFormatMsgKey(), targetProperty.getDefaultLabel());
  }

  protected String getDefaultLabelFormatMsgKey() {
    return DEFAULT_LABEL_FORMAT_MSG_KEY;
  }
}
