
package de.ivu.elect.presentation;

import java.io.Serializable;
import java.util.Collection;

import javax.inject.Inject;

import de.ivu.elect.business.CurrentCustomer;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.control.ElectCommonPrincipal;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.faces.UserLocale;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.Property;
import de.ivu.elect.presentation.mandant.MandantSessionSelect;

public class ViewFormBuilder implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient CurrentCustomer currentCustomer;

  @Inject
  private transient ElectCommonPrincipal principal;

  @Inject
  private transient UserLocale userLocale;

  @Inject
  private transient Messages messages;

  @Inject
  private transient MandantSessionSelect mandantSessionSelect;

  public ViewFormBuilder() {
    super();
  }

  protected FormBuilder focusProperty(FormBuilder formBuilder, Property property) {
    if (property != null) {
      Class<? extends Property> propertyClass = property.getClass();

      if (formBuilder.containsPropertyByClass(propertyClass)) {
        formBuilder.selectPropertyByClass(propertyClass).focused();
      }
    }

    return formBuilder;
  }

  protected FormBuilder setPropertiesRequired(FormBuilder formBuilder, Collection<Class<? extends Property>> propertyClasses) {
    return setPropertiesWithRequired(formBuilder, propertyClasses, true);
  }

  protected FormBuilder setPropertiesOptional(FormBuilder formBuilder, Collection<Class<? extends Property>> propertyClasses) {
    return setPropertiesWithRequired(formBuilder, propertyClasses, false);
  }

  private FormBuilder setPropertiesWithRequired(FormBuilder formBuilder, Collection<Class<? extends Property>> propertyClasses, boolean required) {
    Property lastPropertySelectedOrNull = formBuilder.lastProperty();

    for (Class<? extends Property> propertyClass : propertyClasses) {
      if (formBuilder.containsPropertyByClass(propertyClass)) {
        formBuilder.selectPropertyByClass(propertyClass).withRequired(required);
      }
    }

    if (lastPropertySelectedOrNull != null) {
      formBuilder.selectProperty(lastPropertySelectedOrNull);
    }

    return formBuilder;
  }

  protected boolean hasUseCasePermission(UseCasePermission useCasePermission) {
    return getPrincipal().hasUseCasePermission(useCasePermission);
  }

  protected CurrentCustomer getCurrentCustomer() {
    return currentCustomer;
  }

  protected ElectCommonPrincipal getPrincipal() {
    return principal;
  }

  protected UserLocale getUserLocale() {
    return userLocale;
  }

  protected boolean existsMessage(String msgKey) {
    return getMessages().isMessageKeyVorhanden(msgKey);
  }

  protected String getMessage(String msgKey, Object... params) {
    return getMessages().getMessageForMessageKey(msgKey, params);
  }

  protected String getMessage(Enum<?> enumValue, Object... params) {
    return getMessages().lokalisiere(enumValue, params);
  }

  protected Messages getMessages() {
    return messages;
  }

  protected Mandant getMandant() {
    return mandantSessionSelect.getMandant();
  }
}
