
package de.ivu.elect.presentation.system;

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

import de.ivu.elect.business.CurrentCustomer;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.system.boundary.SystemLogExtendedDAO;
import de.ivu.elect.business.system.boundary.SystemLogSearchParameters;
import de.ivu.elect.business.system.entity.SystemLog;
import de.ivu.elect.business.system.entity.SystemLogProperties;
import de.ivu.elect.business.system.entity.SystemLogQuelle;
import de.ivu.elect.business.system.entity.SystemLogStatus;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.LambdaEnumProperty;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;

@OnlineHilfeId(value = "352", mehrfachVergebenOk = true)
@Named(AbstractSystemLogSearchView.COMPONENT_NAME)
public class SystemLogSearchView extends AbstractSystemLogSearchView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient CurrentCustomer currentCustomer;

  @Inject
  private transient SystemLogExtendedDAO systemLogExtendedDAO;

  public SystemLogSearchView() {
    super();
  }

  @Override
  public void presetSearchParameters() {
    // Do nothing.
    if (isZupruefendeMeldungenVorhanden()) {
      getSearchParameters().getSystemLog().setStatus(SystemLogStatus.ZU_PRUEFEN);
    }
  }

  @Override
  public SystemLogSearchParameters getSearchParameters() {
    SystemLogSearchParameters searchParameters = super.getSearchParameters();
    searchParameters.setMandant(getMandant());
    return searchParameters;
  }

  public void preRenderView() {
    if (isZupruefendeMeldungenVorhanden()) {
      addWarningMessage("system.log.meldungen.vorhanden.bearbeiten");
    }
  }

  protected boolean isZupruefendeMeldungenVorhanden() {
    return systemLogExtendedDAO.findByCountStatus(getMandant(), SystemLogStatus.ZU_PRUEFEN) > 0;
  }

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

  @Override
  public void addProperties(FormBuilder formBuilder) {
    SystemLog systemLog = getSearchParameters().getSystemLog();
    SystemLogProperties properties = systemLog.getProperties();

    formBuilder.addProperty(properties.getStatusMitLeererAuswahlProperty()).withRequired(false);
    formBuilder.addProperty(new LambdaEnumProperty<>(entityMeta.getQuelleEinzahlMessage(), false, systemLog::setQuelle,
        systemLog::getQuelle, SystemLogQuelle.class, SystemLogQuelle.getValuesForCustomer(currentCustomer.getCustomer()), true));
    formBuilder.addProperty(properties.getLogNachrichtArtMitLeererAuswahlProperty()).withRequired(false);
    formBuilder.addProperty(properties.getLogNachrichtProperty()).withRequired(false);

    addLimitProperty(formBuilder);
  }

  public boolean isBtnCheckSearchedRendered() {
    return hasPermission(UseCasePermission.SYSTEM_LOG_EDIT);
  }

  public boolean isBtnCheckSearchedDisabled() {
    return isEmptySearchResultDisplayed();
  }

  public boolean isBtnDeleteSearchedRendered() {
    return hasPermission(UseCasePermission.SYSTEM_LOG_DELETE);
  }

  public boolean isBtnDeleteSearchedDisabled() {
    return isEmptySearchResultDisplayed();
  }

}
