package de.ivu.elect.presentation.mandant;

import javax.inject.Named;

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;

@Named(AbstractMandantSearchView.COMPONENT_NAME)
@OnlineHilfeId("814")
public class MandantSearchView extends AbstractMandantSearchView {

  private static final long serialVersionUID = 1L;

  public MandantSearchView() {
    super();
  }

  @Override
  protected AbstractGebietSecurityFilter<Mandant> getGebietSecurityFilter() {
    return KEIN_GEBIET_SECURITY_FILTER;
  }

  @Override
  public void presetSearchParameters() {
    // Do nothing.
  }

  @Override
  public void addProperties(FormBuilder formBuilder) {
    formBuilder.addProperty(searchParameters.getMandant().getProperties().getNummerProperty()).withRequired(false);
    formBuilder.addProperty(searchParameters.getMandant().getProperties().getNameProperty()).withRequired(false);
    formBuilder.addProperty(searchParameters.getMandant().getProperties().getArtMitLeererAuswahlProperty()).withRequired(false);
    formBuilder.addProperty(searchParameters.getMandant().getProperties().getStatusMitLeererAuswahlProperty()).withRequired(false);
  }

  public boolean isBtnResetSearchedRendered() {
    return hasPermission(UseCasePermission.MANDANT_RESET);
  }

  public boolean isBtnResetSearchedDisabled() {
    return isEmptySearchResultDisplayed() || isKeinMandantFuerResetImSuchergebnis();
  }

  private boolean isKeinMandantFuerResetImSuchergebnis() {
    return isSearchNotReady() || getSearchResult().stream().noneMatch(m -> m.getArt().isResetMoeglich());

  }

}
