
package de.ivu.elect.business.system.boundary;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.enterprise.context.ApplicationScoped;

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.mandant.entity.MandantPK;
import de.ivu.elect.business.system.entity.SystemStatus;

@ApplicationScoped
public class SystemStatusStore implements Serializable {

  private static final long serialVersionUID = 1L;
  public static final SystemStatus DEFAULT_VALUE = SystemStatus.PRODUKTION;

  private final Map<MandantPK, SystemStatus> systemStatus = new ConcurrentHashMap<>();

  public SystemStatusStore() {
    super();
  }

  public boolean isProduktivmodusEnabled(Mandant mandant) {
    return getSystemStatus(mandant).isProduktivmodus();
  }

  public boolean isProduktivmodusEnabled(MandantPK mandantPK) {
    return getSystemStatus(mandantPK).isProduktivmodus();
  }

  public boolean isTestmodusEnabled(Mandant mandant) {
    return getSystemStatus(mandant).isTestmodus();
  }

  public boolean isTestmodusEnabled(MandantPK mandantPK) {
    return getSystemStatus(mandantPK).isTestmodus();
  }


  public SystemStatus getSystemStatus(Mandant mandant) {
    if (mandant==null){
      return DEFAULT_VALUE;
    }
    return getSystemStatus(mandant.getPk());
  }

  public SystemStatus getSystemStatus(MandantPK mandantPK) {
    if (mandantPK==null){
      return DEFAULT_VALUE;
    }
    return systemStatus.getOrDefault(mandantPK, DEFAULT_VALUE);
  }

  public void setSystemStatus(MandantPK mandantPK, SystemStatus systemStatus) {
    if (mandantPK==null){
      return;
    }
    this.systemStatus.put(mandantPK, systemStatus);
  }
}
