package de.ivu.elect.presentation.stimmeingabe.exchange;

import javax.enterprise.inject.Specializes;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.faces.PreConditionCheck;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.MessageProperty;
import de.ivu.elect.presentation.wahl.NlBackupStatus;

@Specializes
public class NlStimmeingabeImportSearchView extends StimmeingabeImportSearchView {
  private static final long serialVersionUID = 1L;

  @Inject
  private transient NlBackupStatus nlBackupStatus;

  @Override
  @PreConditionCheck
  public String checkPreCondition() {
    String result = super.checkPreCondition();
    if (StringUtils.isBlank(result)) {
      if(nlBackupStatus.isBackupRestoreFreigeschaltet()) {
        if (!getPrincipal().hasUseCasePermission(UseCasePermission.STIMMEINGABE_IMPORTIEREN_CUSTOM)) {
          return getStartViewId();
        }
      } else {
        if (getPrincipal().hasUseCasePermission(UseCasePermission.STIMMEINGABE_IMPORTIEREN_CUSTOM)) {
          return getStartViewId();
        }
      }
    }
    return null;
  }

  @Override
  protected void addProperties(FormBuilder formBuilder) {
    super.addProperties(formBuilder);

    if (nlBackupStatus.isBackupRestoreFreigeschaltet()) {
      formBuilder.addProperty(new MessageProperty(getMessage("stimmeingabe.backup.import.hinweis")));
    }
  }

  @Override
  public String getPageTitle() {
    if (nlBackupStatus.isBackupRestoreFreigeschaltet()) {
      return getMessage("stimmeingabe.backup.import.page.title");
    }
    return super.getPageTitle();
  }

  @Override
  public String getPageHeader() {
    if (nlBackupStatus.isBackupRestoreFreigeschaltet()) {
      return getMessage("stimmeingabe.backup.import.page.header");
    }
    return super.getPageHeader();
  }
}
