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

import java.util.Collections;
import java.util.List;
import java.util.TimeZone;

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

  private static final StatusOptionType SYSTEM_DEFAULT_TIME_ZONE = new StatusOptionType() {

    private static final long serialVersionUID = 1L;

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

  @Inject
  private transient StartupSystemDefaults startupSystemDefaults;

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

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

  @Override
  protected StatusEntry resolveSystemStatusOption(SystemStatusOption systemStatusOption) {
    String timeZoneDisplayName = getDefaultTimeZone() == null ? "" : getDefaultTimeZone().getDisplayName();

    if (isTimeZoneRichtig()) {
      return ok("system.info.konfiguriert.timezone.ok", timeZoneDisplayName);
    }

    return warn("system.info.konfiguriert.timezone.nok", timeZoneDisplayName, getExpectedTimeZone() == null ? "" : getExpectedTimeZone().getDisplayName());
  }

  protected boolean isTimeZoneRichtig() {
    return getExpectedTimeZone() == null || getExpectedTimeZone().equals(getDefaultTimeZone());
  }

  private TimeZone getExpectedTimeZone() {
    return startupSystemDefaults.getExpectedServerTimeZone();
  }

  private TimeZone getDefaultTimeZone() {
    return TimeZone.getDefault();
  }
}
