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

import java.math.BigDecimal;
import java.util.Currency;

import javax.faces.convert.Converter;
import javax.faces.convert.NumberConverter;

public class MoneyProperty extends AbstractBigDecimalProperty {

  private static final int DEFAULT_MIN_VALUE = 0;

  private static final int DEFAULT_MIN_FRACTION_DIGITS = 2;

  private final AbstractBigDecimalProperty property;

  private final Currency currency;

  public MoneyProperty(AbstractBigDecimalProperty property, String currencyCode) {
    super();

    this.property = property;
    this.currency = Currency.getInstance(currencyCode);
  }

  @Override
  public Converter getDefaultConverter() {
    NumberConverter numberConverter = (NumberConverter) super.getDefaultConverter();
    numberConverter.setMinFractionDigits(DEFAULT_MIN_FRACTION_DIGITS);

    return numberConverter;
  }

  public Currency getCurrency() {
    return currency;
  }

  public String getCurrencySymbol() {
    // NOTE, dass java.util.Currency.getSymbol() das eigentliche 'Symbol', bspw. '€', liefert.
    return getCurrency().getSymbol(getUserLocale());
  }

  @Override
  public PropertyType getDefaultType() {
    return PropertyType.INPUT_MONEY;
  }

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

  @Override
  public String getDefaultLabel() {
    return property.getDefaultLabel();
  }

  @Override
  public long getMinValue() {
    return DEFAULT_MIN_VALUE;
  }

  @Override
  public long getMaxValue() {
    return property.getMaxValue();
  }

  @Override
  public String getPattern() {
    return property.getPattern();
  }

  @Override
  public BigDecimal getDefaultValue() {
    return property.getDefaultValue();
  }

  @Override
  public BigDecimal getValue() {
    return property.getValue();
  }

  @Override
  public void setValue(BigDecimal value) {
    property.setValue(value);
  }

  @Override
  public void onObservedPropertyValueChanged(Property observedProperty) {
    property.onObservedPropertyValueChanged(observedProperty);
  }
}
