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

import java.util.Collections;
import java.util.List;
import java.util.Locale;

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 JvmLocaleSystemStatusOptionResolver extends AbstractSystemStatusOptionResolver {

  private static final StatusOptionType SYSTEM_DEFAULT_LOCALE = new StatusOptionType() {

    private static final long serialVersionUID = 1L;

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

  @Inject
  private transient StartupSystemDefaults startupSystemDefaults;

  public JvmLocaleSystemStatusOptionResolver() {
    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_LOCALE, mandant));
  }

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

  @Override
  protected StatusEntry resolveSystemStatusOption(SystemStatusOption systemStatusOption) {
    Locale locale = getDefaultLocale();

    if (isLocaleRichtig()) {
      return ok("system.info.konfiguriert.locale.ok", locale);
    }

    return warn("system.info.konfiguriert.locale.nok", locale, getExpectedLocale());
  }

  private boolean isLocaleRichtig() {
    return getExpectedLocale() == null || getExpectedLocale().equals(getDefaultLocale());
  }

  private Locale getDefaultLocale() {
    return Locale.getDefault();
  }

  private Locale getExpectedLocale() {
    return startupSystemDefaults.getExpectedServerLocale();
  }
}
