
package de.ivu.elect.presentation.system;

import static de.ivu.elect.business.security.UseCasePermission.HINTERGRUNDDIENSTE_AKTIVIEREN;
import static de.ivu.elect.business.security.UseCasePermission.HINTERGRUNDDIENSTE_AUSLOESEN;
import static de.ivu.elect.business.security.UseCasePermission.HINTERGRUNDDIENSTE_DEAKTIVIEREN;
import static java.util.stream.Collectors.toList;

import java.util.List;

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

import de.ivu.elect.business.search.control.AbstractElectSearchParameters;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.control.ElectCommonPrincipal;
import de.ivu.elect.business.system.boundary.BackgroundServiceStatusErmittler;
import de.ivu.elect.business.system.control.BackgroundServiceDisableToggler;
import de.ivu.elect.business.system.control.BackgroundServiceEventStarter;
import de.ivu.elect.business.system.entity.BackgroundServiceStatus;
import de.ivu.elect.business.system.entity.SystemLogQuelle;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.security.AbstractGebietSecurityFilter;

@RequestScoped
@OnlineHilfeId("626")
@Named(BackgroundServiceStatusView.COMPONENT_NAME)
@View(viewId = BackgroundServiceStatusView.VIEW_ID, useCase = UseCasePermission.HINTERGRUNDDIENSTE_STATUSUEBERSICHT_ANZEIGEN)
public class BackgroundServiceStatusView extends AbstractElectSearchView<BackgroundServiceStatus> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "backgroundServiceStatusView";

  public static final String VIEW_ID = "/system/background-service-status.xhtml";

  @Inject
  private transient ElectCommonPrincipal principal;

  @Inject
  private transient BackgroundServiceEventStarter backgroundServiceEventStarter;

  @Inject
  private transient BackgroundServiceStatusErmittler backgroundServiceStatusErmittler;

  @Inject
  private transient BackgroundServiceDisableToggler backgroundServiceDisableToggler;

  public BackgroundServiceStatusView() {
    super();
  }

  @Override
  protected String getViewId() {
    return VIEW_ID;
  }

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

  @Override
  public boolean isSupportsLazyLoading() {
    return false; // ELECT-8409
  }

  @Override
  public int getDefaultPageSize() {
    return getPageSizeShowAll();
  }

  @Override
  public AbstractElectSearchParameters getSearchParameters() {
    return AbstractElectSearchParameters.EMPTY;
  }

  /**
   * @deprecated To be removed...
   * @see AbstractElectSearchView#presetSearchParameters
   */
  @Deprecated
  @Override
  public void presetSearchParameters() {
    // To be removed...
  }

  @Override
  public void addProperties(FormBuilder formBuilder) {
    // nichts zu tun
  }

  @Override
  protected List<BackgroundServiceStatus> performSearch() {
    return backgroundServiceStatusErmittler.getBackgroundServiceStatus().values().stream().sorted().collect(toList());
  }

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

  public boolean isBtnEventRendered() {
    return principal.hasUseCasePermission(HINTERGRUNDDIENSTE_AUSLOESEN);
  }

  @Redirect
  public String fireEvents() {
    List<SystemLogQuelle> events = backgroundServiceEventStarter.sendeBackgroundServiceEvents();
    addInfoMessage("background.service.events.fired", String.join(", ", events.stream().map(this::getMessage).collect(toList())));
    return VIEW_ID;
  }

  public boolean isBtnDisableAllRendered() {
    return principal.hasUseCasePermission(HINTERGRUNDDIENSTE_DEAKTIVIEREN);
  }

  public boolean isBtnEnableAllRendered() {
    return principal.hasUseCasePermission(HINTERGRUNDDIENSTE_AKTIVIEREN);
  }

  @Redirect
  public String disableAll() {
    backgroundServiceDisableToggler.disable(performSearch());
    addInfoMessage("background.service.disabled.all");
    return VIEW_ID;
  }

  @Redirect
  public String enableAll() {
    backgroundServiceDisableToggler.enable(performSearch());
    addInfoMessage("background.service.enabled.all");
    return VIEW_ID;
  }
}
