
package de.ivu.elect.business.system.control.status;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.system.entity.SystemStatusOptionGroup;
import de.ivu.elect.jee.presentation.status.StatusEntry;
import de.ivu.elect.jee.presentation.status.StatusOptionType;
import de.ivu.elect.jee.startup.StartupSystemDefaults;

public class JvmCharsetSystemStatusOptionResolver extends AbstractSystemStatusOptionResolver {

  private static final StatusOptionType SYSTEM_DEFAULT_CHARSET = new StatusOptionType() {

    private static final long serialVersionUID = 1L;

    @Override
    public String toString() {
      return "SYSTEM_DEFAULT_CHARSET";
    }
  };

  @Inject
  private transient StartupSystemDefaults startupSystemDefaults;

  public JvmCharsetSystemStatusOptionResolver() {
    super();
  }

  @Override
  protected boolean isAccept(Mandant mandant) {
    return mandant.getArt().isSystemMandant();
  }

  @Override
  protected List<SystemStatusOption> getStatusOptionsInternal(Mandant mandant) {
    return Collections.singletonList(new SystemStatusOption(SYSTEM_DEFAULT_CHARSET, mandant));
  }

  @Override
  public SystemStatusOptionGroup getSystemStatusOptionGroup() {
    return SystemStatusOptionGroup.JVM_START_OPTIONEN;
  }

  @Override
  protected StatusEntry resolveSystemStatusOption(SystemStatusOption systemStatusOption) {
    Charset charset = getDefaultCharset();

    if (isCharsetRichtig()) {
      return ok("system.info.konfiguriert.charset.ok", charset);
    }

    return warn("system.info.konfiguriert.charset.nok", charset, getExpectedCharset());
  }

  protected boolean isCharsetRichtig() {
    return getExpectedCharset() == null || getExpectedCharset().equals(getDefaultCharset());
  }

  private java.nio.charset.Charset getDefaultCharset() {
    return Charset.defaultCharset();
  }

  private Charset getExpectedCharset() {
    return startupSystemDefaults.getExpectedServerCharset();
  }
}
