
package de.ivu.elect.presentation.system;

import static java.util.Objects.isNull;

import java.lang.management.ThreadInfo;
import java.util.ArrayList;
import java.util.List;

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

import org.apache.deltaspike.core.api.scope.ConversationGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.system.boundary.ThreadDumpCreate;
import de.ivu.elect.business.system.entity.ThreadDumpFilterParameters;
import de.ivu.elect.jee.presentation.dynamicform.AdvancedDynaFormModel;
import de.ivu.elect.jee.presentation.dynamicform.DynamicFormSearchView;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.jee.presentation.search.AbstractSearchView;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@RequestScoped
@OnlineHilfeId(value = "362", mehrfachVergebenOk = true)
@Named(ThreadDumpView.COMPONENT_NAME)
@View(viewId = ThreadDumpView.VIEW_ID, useCase = UseCasePermission.THREAD_DUMP_SEARCH)
public class ThreadDumpView extends AbstractSearchView<ThreadInfo, ThreadDumpFilterParameters> implements DynamicFormSearchView {

  private static final Logger LOG = LoggerFactory.getLogger(ThreadDumpView.class);
  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "threadDumpView";

  public static final String VIEW_ID = "/system/thread-dump.xhtml";

  @Inject
  private transient ThreadDumpCreate threadDumpCreate;

  @Inject
  @ConversationGroup(ThreadDumpFilterParameters.class)
  private transient ThreadDumpFilterParameters threadDumpFilterParameters;

  private AdvancedDynaFormModel searchParametersModel;

  public ThreadDumpView() {
    super();
  }

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

  @Override
  public ThreadDumpFilterParameters getSearchParameters() {
    return threadDumpFilterParameters;
  }

  @Override
  protected List<ThreadInfo> runSearch() {
    ArrayList<ThreadInfo> threadInfos = new ArrayList<>(threadDumpCreate.createThreadDump(getSearchParameters()));

    LOG.info("Anzahl threads: " + threadInfos.size());

    return threadInfos;
  }

  @Override
  public AdvancedDynaFormModel getSearchFormModel() {
    if (isNull(searchParametersModel)) {
      searchParametersModel = createSearchParametersModel();
    }

    return searchParametersModel;
  }

  private AdvancedDynaFormModel createSearchParametersModel() {
    FormBuilder formBuilder = new FormBuilder();

    addProperties(formBuilder);

    return formBuilder.createModel();
  }

  @Override
  public void addProperties(FormBuilder formBuilder) {
    formBuilder.addProperty(getSearchParameters().getThreadNameProperty());
    formBuilder.addProperty(getSearchParameters().getStackTraceContentProperty());
  }

  @Override
  public void resetSearchParametersModel() {
    searchParametersModel = null;
  }

  @Override
  @Redirect
  public String reset() {
    resetSearchParametersModel();

    return super.reset();
  }
}
