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

import java.util.Date;

public class TimestampTimeProperty extends AbstractTimestampTimeProperty {

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

  private final AbstractTimestampProperty targetProperty;

  public TimestampTimeProperty(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 (value != null) {
      targetProperty.setValue(ueberschreibeDatumsanteil(value, targetProperty.getValue()));
    }
  }

  private Date ueberschreibeDatumsanteil(Date value, Date datumsanteil) {
    Date result = new Date(value.getTime());

    if (datumsanteil != null) {
      result.setYear(datumsanteil.getYear());
      result.setMonth(datumsanteil.getMonth());
      result.setDate(datumsanteil.getDate());
    }

    return result;
  }

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

  protected String getDefaultLabelFormatMsgKey() {
    return DEFAULT_LABEL_FORMAT_MSG_KEY;
  }
}
