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

import javax.ejb.Stateless;
import javax.enterprise.inject.Specializes;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.security.boundary.EmlSignKeyService;
import de.ivu.elect.business.wahl.entity.Wahl;

@Stateless
@Specializes
public class NlWasWahlEdit extends WasWahlEdit {
  private static final Logger LOG = LoggerFactory.getLogger(NlWasWahlEdit.class);

  private static final int MAX_COUNT = 1000;

  @Inject
  private transient EmlSignKeyService emlSignKeyService;

  @Inject
  private transient NlWasDeleteHelper nlWasDeleteHelper;

  @Override
  public void delete(Wahl wahlNotAttched) {

    LOG.info("delete Ergebniseingaenge by gebiet start");
    var count = 0;
    while (nlWasDeleteHelper.deleteErgebniseingaengeNachGebiet(wahlNotAttched) && count < MAX_COUNT) {
      count++;
      LOG.info("delete Ergebniseingaenge by gebiet " + count);
    }
    LOG.info("delete Ergebniseingaenge");
    nlWasDeleteHelper.deleteErgebniseingaenge(wahlNotAttched); // ELECTVAPP-1210
    LOG.info("delete Ergebnisse");
    nlWasDeleteHelper.deleteErgebnisse(wahlNotAttched); // ELECTVAPP-1210
    LOG.info("delete Keystore");
    emlSignKeyService.deleteKeystore(wahlNotAttched); // ELECTVAPP-1089
    LOG.info("delete wahl");
    super.delete(wahlNotAttched);
    LOG.info("delete wahl - done");
  }

//  public boolean toggleHsbc(Wahl wahlNotAttached) {
//    var wahl = dao.attach(wahlNotAttached);
//    var customData = wahl.getCustomData(NlWahlCustomData.class);
//    customData.setHsbcSystem(!customData.isHsbcSystem());
//    return customData.isHsbcSystem();
//  }
}
