
package de.ivu.elect.presentation.wahlvorschlag.form;

import javax.inject.Inject;

import de.ivu.elect.business.gruppe.entity.Gruppe;
import de.ivu.elect.business.gruppe.entity.GruppeProperties;
import de.ivu.elect.business.gruppe.entity.ReferendumVorlageMeta;
import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOption;
import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOptionProperties;
import de.ivu.elect.jee.presentation.dynamicform.AbstractTimestampProperty;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.TimestampDateProperty;
import de.ivu.elect.jee.presentation.dynamicform.TimestampTimeProperty;
import de.ivu.elect.presentation.ViewFormBuilder;

public class ReferendumOptionViewFormBuilder extends ViewFormBuilder {

  private static final long serialVersionUID = 1L;

  @Inject
  private ReferendumVorlageMeta referendumVorlageMeta;

  @Inject
  private ReferendumOptionUniquePositionFacesValidator referendumOptionUniquePositionFacesValidator;

  public ReferendumOptionViewFormBuilder() {
    super();
  }

  public void addProperties(FormBuilder formBuilder, ReferendumOption referendumOption) {
    addReferendumVorlageProperties(formBuilder, referendumOption.getGruppe());
    addReferendumOptionProperties(formBuilder, referendumOption);
  }

  private void addReferendumVorlageProperties(FormBuilder formBuilder, Gruppe referendumVorlage) {
    GruppeProperties referendumVorlageProperties = referendumVorlage.getProperties();

    formBuilder.addProperty(referendumVorlageProperties.getNameProperty()).withLabel(referendumVorlageMeta.getNameEinzahlMessage())
        .readonly();
  }

  private void addReferendumOptionProperties(FormBuilder formBuilder, ReferendumOption referendumOption) {
    ReferendumOptionProperties referendumOptionProperties = referendumOption.getProperties();

    formBuilder.addProperty(referendumOptionProperties.getLabelProperty());
    formBuilder.addProperty(referendumOptionProperties.getKurzbezeichnungProperty());
    formBuilder.addProperty(referendumOptionProperties.getPositionProperty())
        .withValidatorAndPk(referendumOptionUniquePositionFacesValidator, referendumOption.getPk());

    if (referendumOption.getWahl().isBegehren()) {
      AbstractTimestampProperty zeitpunktDerEinreichungProperty = referendumOptionProperties.getZeitpunktDerEinreichungProperty();

      formBuilder.addProperty(new TimestampDateProperty(zeitpunktDerEinreichungProperty));
      formBuilder.addProperty(new TimestampTimeProperty(zeitpunktDerEinreichungProperty));
    }
  }
}
