package de.ivu.elect.business.stimmzettel.control;

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

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class WesEncryptionServiceCache implements Serializable {
  private static final long serialVersionUID = 1L;

  private final Map<String, Boolean> checkPruefzifferCache = new ConcurrentHashMap<>();

  private final Map<String, String> encryptPruefzifferCache = new ConcurrentHashMap<>();

  public void storeResultCheckPruefziffer(String gebietId, String salt, String pruefziffer, Boolean result) {
    checkPruefzifferCache.putIfAbsent(createKey(gebietId, salt, pruefziffer), result);

  }

  private String createKey(String gebietId, String salt, String pruefziffer) {
    return gebietId + "_" + salt + "_" + pruefziffer;
  }

  public Boolean getResultCheckPruefziffer(String gebietId, String salt, String pruefziffer) {
    return checkPruefzifferCache.get(createKey(gebietId, salt, pruefziffer));
  }

  public void storeEncryptPruefziffer(String pruefziffer, String salt, String encrypted) {
    encryptPruefzifferCache.putIfAbsent(createKey(salt, pruefziffer), encrypted);
  }

  public String getEncryptPruefziffer(String pruefziffer, String salt) {
    return encryptPruefzifferCache.get(createKey(salt, pruefziffer));
  }

  private String createKey(String salt, String pruefziffer) {
    return salt + "_" + pruefziffer;
  }

}
