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

import java.util.List;

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

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

import de.ivu.elect.business.system.boundary.SystemLogWriter;
import de.ivu.elect.business.system.boundary.VirusScanAvailable;
import de.ivu.elect.business.system.entity.SystemLogQuelle;
import de.ivu.elect.jee.startup.PostStartupAsync;

@Stateless
public class VirusScanPostStarup implements PostStartupAsync {

  private static final Logger LOG = LoggerFactory.getLogger(VirusScanPostStarup.class);

  @Inject
  private transient VirusScanInit virusScanInit;

  @Inject
  private transient VirusScanAvailable virusScanAvailable;

  @Inject
  private transient SystemLogWriter systemLogWriter;

  @Override
  public void postStartup() {
    virusScanInit.initScanner();
    checkScanner();
  }

  protected void checkScanner() {
    if (virusScanAvailable.isVirusScanAvailable()) {
      LOG.info("virus scan configured - check access");
      List<String> virusScannerWorking = virusScanAvailable.isVirusScannerWorking();
      if (virusScannerWorking.isEmpty()) {
        LOG.info("virus scanner working");
      } else {
        systemLogWriter
            .writeErrorLogMessageFuerSystemMandant("Virus scanner not working: " + String.join("\n", virusScannerWorking), SystemLogQuelle.VIRUS_SCAN);
      }

    } else {
      LOG.info("virus scan not configured");
    }

  }


}
