
package de.ivu.elect.presentation.konfiguration;

import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.konfiguration.entity.Konfiguration;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.konfiguration.form.KonfigurationViewFormBuilder;

@OnlineHilfeId(value = "353", mehrfachVergebenOk = true)
@Named(AbstractKonfigurationEditView.COMPONENT_NAME)
public class KonfigurationEditView extends AbstractKonfigurationEditView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient KonfigurationViewFormBuilder konfigurationViewFormBuilder;

  public KonfigurationEditView() {
    super();
  }

  @Override
  protected boolean isGebietBearbeitenRechtPruefen() {
    return false;
  }

  @Override
  public void addProperties(FormBuilder formBuilder, Konfiguration konfiguration) {
    konfigurationViewFormBuilder.addProperties(formBuilder, konfiguration);
  }

  @Override
  public boolean isBtnSaveRendered() {
    return super.isBtnSaveRendered() && !isReadonlyParameter();
  }

  private boolean isReadonlyParameter() {
    return isEntitySet() && getEntity().getWertTyp().expectsWertIsReadonly();
  }

  public String getPageHeader() {
    if (isAndererMandant()) {
      return getMessage("konfiguration.edit.page.header.mandant", getEntity().getMandant().getNummer(), getEntity().getMandant().getName());
    }

    return getMessage("konfiguration.edit.page.header");
  }

  private boolean isAndererMandant() {
    var mandant = getMandant();
    if (mandant == null || getEntity() == null || getEntity().getMandant() == null) {
      return false;
    }
    return !mandant.equals(getEntity().getMandant());
  }
}
