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.business.wahl.entity.Wahl;
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.jee.presentation.status.OutputSeverity;
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 NlWahlprozessMenuBuilder getNlWahlprozessMenuBuilder() {
    return nlWahlprozessMenuBuilder;
  }

  public String getBadgeInfo() {
    Wahl selectedWahl = getSelectedWahl();
    if (selectedWahl == null) {
      return "";
    }

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

  private AdvancedDynaFormModel model;

  @Override
  public AdvancedDynaFormModel getModel() {
    if (model == null) {
      FormBuilder 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) {
    formBuilder.withStyleClass("custom-" + OutputSeverity.ERROR.name().toLowerCase());
    formBuilder.withErrorMessage(getMessage(msgKey));
  }

  protected void addWarning(FormBuilder formBuilder, String msgKey) {
    formBuilder.withStyleClass("custom-" + OutputSeverity.WARN.name().toLowerCase());
    formBuilder.withWarningMessage(getMessage(msgKey));
  }
}
