
package de.ivu.elect.presentation.system;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import org.apache.commons.lang3.StringUtils;

import de.ivu.elect.business.konfiguration.entity.KonfigurationExtendedLoader;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.control.ElectCommonPrincipal;
import de.ivu.elect.business.system.control.TempDir;
import de.ivu.elect.jee.memory.GcController;
import de.ivu.elect.jee.memory.MemoryController;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.jee.presentation.view.AbstractView;
import de.ivu.elect.jee.session.SessionController;
import de.ivu.elect.jee.session.SessionCounterListener;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;


import static de.ivu.elect.business.konfiguration.entity.ElectKonfigurationWerte.ELECT_COMMON_DATEI_ABLAGE_VERZEICHNIS_PFAD;

@RequestScoped
@OnlineHilfeId("314")
@Named(MemoryMonitorView.COMPONENT_NAME)
@View(viewId = MemoryMonitorView.VIEW_ID, useCase = UseCasePermission.MEMORY_MONITOR_SEARCH)
public class MemoryMonitorView extends AbstractView {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "memoryMonitorView";

  public static final String VIEW_ID = "/system/memory-monitor.xhtml";

  @Inject
  private transient KonfigurationExtendedLoader konfigurationExtendedLoader;

  @Inject
  private transient MemoryController memoryController;

  @Inject
  private transient SessionController sessionController;

  @Inject
  private transient GcController gcController;

  @Inject
  private transient ElectCommonPrincipal principal;

  @Inject
  private transient TempDir tempDir;

  public MemoryMonitorView() {
    super();
  }

  public MemoryController getMemoryController() {
    return memoryController;
  }

  public GcController getGcController() {
    return gcController;
  }

  /**
   * ELECTMUC-205
   */
  public SessionController getSessionController() {
    return sessionController;
  }

  @Redirect
  public String performGc() {
    getMemoryController().performGc();
    addInfoMessage("memory.gc.erfolgreich");
    return VIEW_ID;
  }

  @Redirect
  public String speichereZeitpunkt() {
    getMemoryController().saveData();
    addInfoMessage("memory.save.data.erfolgreich");
    return VIEW_ID;
  }

  @Redirect
  public String reset() {
    getMemoryController().clearData();
    addInfoMessage("memory.clear.data.erfolgreich");
    return VIEW_ID;
  }

  @Redirect
  public String dump() {

    String ablagageverzeichnis = konfigurationExtendedLoader.resolveStringValue(ELECT_COMMON_DATEI_ABLAGE_VERZEICHNIS_PFAD);
    if (StringUtils.isBlank(ablagageverzeichnis)) {
      ablagageverzeichnis = tempDir.getTempDir().getAbsolutePath();
    }

    String fileName = ablagageverzeichnis + File.separator + "ELECT_ELECT_HEAP_DUMP_" + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".hprof";

    getMemoryController().dumpHeap(fileName, true);
    addInfoMessage("memory.heap.dump.created", fileName);
    return VIEW_ID;
  }

  /**
   * ELECTMUC-205 TODO mal an eine vernünftige Stelle verschieben
   */
  public long getAnzahlAktiverHttpSessions() {
    return SessionCounterListener.getAnzahlSessions();
  }

  public boolean isSpeichereZeitpunktRendered() {
    return principal.hasUseCasePermission(UseCasePermission.MEMORY_MONITOR_EDIT);
  }

  public boolean isResetRendered() {
    return principal.hasUseCasePermission(UseCasePermission.MEMORY_MONITOR_EDIT);
  }

  public boolean isPerformGcRendered() {
    return principal.hasUseCasePermission(UseCasePermission.MEMORY_GC);
  }

  public boolean isDumpRendered() {
    return principal.hasUseCasePermission(UseCasePermission.MEMORY_HEAP_DUMP);
  }

}
