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

import de.ivu.elect.business.email.entity.EmailVorlage;
import de.ivu.elect.business.email.entity.EmailVorlageProperties;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.presentation.ViewFormBuilder;

import javax.inject.Inject;

public class EmailVorlageViewFormBuilder extends ViewFormBuilder {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient EmailVorlageEmailLayoutZuordnenProperties emailVorlageEmailLayoutZuordnenProperties;

  @Inject
  private EmailVorlageUniqueUKFacesValidator emailVorlageUniqueUKFacesValidator;

  public EmailVorlageViewFormBuilder() {
    super();
  }

  public void addEmailVorlageProperties(FormBuilder formBuilder, EmailVorlage emailVorlage) {
    addEmailVorlageProperties(formBuilder, emailVorlage, false);
  }

  public void addEmailVorlagePropertiesWithDefaultValues(FormBuilder formBuilder, EmailVorlage emailVorlage) {
    addEmailVorlageProperties(formBuilder, emailVorlage, true);
  }

  private void addEmailVorlageProperties(FormBuilder formBuilder, EmailVorlage emailVorlage, boolean withDefaultValues) {
    EmailVorlageProperties emailLayoutProperties = emailVorlage.getProperties();

    formBuilder.addProperty(emailLayoutProperties.getBezeichnungProperty())
        .withValidatorAndPk(emailVorlageUniqueUKFacesValidator, emailVorlage.getPk());
    if (withDefaultValues) {
      formBuilder.addProperty(emailVorlageEmailLayoutZuordnenProperties.getEmailVorlageEmailLayoutWithDefaultValueProperty(emailVorlage))
          .withRequired(false);
    } else {
      formBuilder.addProperty(emailVorlageEmailLayoutZuordnenProperties.getEmailVorlageEmailLayoutProperty(emailVorlage))
          .withRequired(false);
    }
    formBuilder.addProperty(new EmailVorlageInhaltCKEditorProperty(emailVorlage));
    formBuilder.addProperty(new EmailVorgangPlatzhalterErlaeuterungProperty());
  }
}
