
package de.ivu.elect.presentation.wahl;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.wahl.entity.NlWahlCustomData;
import de.ivu.elect.jee.presentation.dynamicform.AdvancedDynaFormModel;
import de.ivu.elect.jee.presentation.dynamicform.DynamicFormEditOrCreateView;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.presentation.AbstractElectView;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.wahl.menu.NlWahlprozessMenuBuilder;

@RequestScoped
@Named(NlWahlprozessView.COMPONENT_NAME)
@View(viewId = NlWahlprozessView.VIEW_ID, useCase = UseCasePermission.CUSTOMER_5)
public class NlWahlprozessView extends AbstractElectView implements DynamicFormEditOrCreateView {

  private static final long serialVersionUID = 1L;

  public static final String VIEW_ID = "/wahl/nl-wahlprozess.xhtml";

  public static final String COMPONENT_NAME = "nlWahlprozessView";

  @Inject
  private transient NlWahlprozessMenuBuilder nlWahlprozessMenuBuilder;

  public NlWahlprozessView() {
    super();
  }

  public NlWahlprozessMenuBuilder getNlWahlprozessMenuBuilder() {
    return nlWahlprozessMenuBuilder;
  }

  public String getBadgeInfo() {
    var selectedWahl = getSelectedWahl();

    if (selectedWahl == null) {
      return "";
    }

    return getMessage(selectedWahl.getCustomData(NlWahlCustomData.class).getNlWahlPhase());
  }

  private AdvancedDynaFormModel model;

  @Override
  public AdvancedDynaFormModel getModel() {
    if (model == null) {
      var formBuilder = new FormBuilder();
      addProperties(formBuilder);

      model = formBuilder.createModel();
    }

    return model;
  }

  protected void addProperties(FormBuilder formBuilder) {
    // nichts zu tun
  }

  @Override
  public String getBtnSaveLabel() {
    return "";
  }

  @Override
  public boolean isBtnSaveRendered() {
    return false;
  }

  @Override
  public String save() {
    return VIEW_ID;
  }

  @Override
  public boolean isBtnResetRendered() {
    return false;
  }

  @Override
  public String reset() {
    model = null;

    return VIEW_ID;
  }

  @Override
  public boolean isBtnCancelRendered() {
    return false;
  }

  @Override
  public String cancel() {
    return VIEW_ID;
  }

  protected void addError(FormBuilder formBuilder, String msgKey, Object... params) {
    formBuilder.withErrorMessage(getMessage(msgKey, params));
  }

  protected void addWarning(FormBuilder formBuilder, String msgKey, Object... params) {
    formBuilder.withWarningMessage(getMessage(msgKey, params));
  }
}
