
package de.ivu.elect.presentation.system;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.presentation.dynamicform.AdvancedDynaFormModel;
import de.ivu.elect.jee.presentation.dynamicform.DynamicFormEditOrCreateView;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.ReadOnlyPropertyFactory;
import de.ivu.elect.jee.presentation.view.AbstractView;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@OnlineHilfeId("487")
@RequestScoped
@Named(SystemInfoView.COMPONENT_NAME)
@View(viewId = SystemInfoView.VIEW_ID, useCase = UseCasePermission.SYSTEM_INFO_ANZEIGEN)
public class SystemInfoView extends AbstractView implements DynamicFormEditOrCreateView {

  private static final Logger LOG = LoggerFactory.getLogger(SystemInfoView.class);

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "systemInfoView";

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

  public SystemInfoView() {
    super();
  }

  @Override
  public AdvancedDynaFormModel getModel() {
    FormBuilder formBuilder = new FormBuilder();

    try {
      // HINT, ELECTKOM-220, there is no way to "enable" the com.sun.management classes in thorntail
      // cf. https://confluence.ivu.de/pages/viewpage.action?pageId=139854048
      new SystemInfoViewHelper().addOsInformation(formBuilder, getMessages());
      new SystemInfoViewHelper().addVmInformation(formBuilder, getMessages());
    } catch (NoClassDefFoundError e) {
      LOG.info("Sun Management nicht freigegen, ignoriere " + e.getMessage()); // ELECT-12295
    } catch (Exception e) {
      LOG.warn("Fehler beim Ermmitteln der System-Informationen " + e.getMessage(), e);
      addWarningMessage("system.info.exception.unknown", e.getMessage());
    }

    formBuilder.addSeparator("system.properties.info");
    appendVars(formBuilder, new HashMap(System.getProperties()));
    formBuilder.addSeparator("system.environment.info");
    appendVars(formBuilder, System.getenv());

    return formBuilder.createModel();
  }

  protected void appendVars(FormBuilder formBuilder, Map<String, ?> m) {
    List<String> keys = new ArrayList<>(m.keySet());
    Collections.sort(keys);
    for (String k : keys) {
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(k, convertToString(k, m.get(k))));
    }
  }

  private String convertToString(String key, Object o) {
    if (o == null) {
      return null;
    }
    if (key.toLowerCase(Locale.getDefault()).contains("passwor")) {
      return "******";
    }
    return o.toString();
  }

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

  @Override
  public String getBtnSaveLabel() {
    return getMessage("entity.edit.action.save");
  }

  @Override
  public String save() {
    return null;
  }

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

  @Override
  public String reset() {
    return null;
  }

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

  @Override
  public String cancel() {
    return null;
  }
}
