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

import java.io.Serializable;

import javax.inject.Inject;
import javax.persistence.EntityNotFoundException;

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

import de.ivu.elect.business.system.control.files.cleaner.FileCleanerFinder;

// @Stateless ELECTKOM-2576 keine Transaktion
public class FileCleanup implements Serializable {

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

  private static final long serialVersionUID = 1L;

  @Inject
  private transient FileCleanupTransaction fileCleanupTransaction;

  @Inject
  private transient FileCleanerFinder fileCleanerFinder;

  public boolean isEtwasZuBereinigen() {
    return fileCleanupTransaction.isDateibereinugungAktiviert() && fileCleanupTransaction.isServerVerzeichnisVerfuegbar() && isInCleanerEtwasZuBereinigen();
  }

  private boolean isInCleanerEtwasZuBereinigen() {
    var serverVerzeichnis = fileCleanupTransaction.getServerVerzeichnis();
    for (var cleaner : fileCleanerFinder.getFileCleaner()) {
      try {
        var dirtyFilePresent = fileCleanupTransaction.isDirtyFilePresent(cleaner, serverVerzeichnis);
        LOG.info("cleaner = " + cleaner.getName() + ": " + dirtyFilePresent);
        if (dirtyFilePresent) {
          return true;
        }
      } catch (EntityNotFoundException enf) {
        LOG.warn("EntityNotFoundException: " + enf.getMessage());
        return false;
      } catch (Exception e) {
        throw new RuntimeException(cleaner.getName() + "-" + e.getMessage(), e);
      }
    }
    return false;
  }

  public void bereinige() {
    if (fileCleanupTransaction.isDateibereinugungAktiviert()) {
      LOG.info("Starte Dateibereinigung");
      for (var cleaner : fileCleanerFinder.getFileCleaner()) {
        try {
          var buckets = fileCleanupTransaction.getBuckets(cleaner);
          LOG.info("Cleaner: " + cleaner.getName() + "; buckets: " + buckets.size());
          if (!buckets.isEmpty()) {
            for (var bucket : buckets) {
              try {
                fileCleanupTransaction.bereinige(cleaner, bucket);
              } catch (EntityNotFoundException nfe) {
                LOG.warn("EntityNotFoundException: " + nfe.getMessage() + ", Cleaner: " + cleaner.getName() + ", bucket: " + bucket);
              } catch (Exception e) {
                throw new RuntimeException(cleaner.getName() + " - bucket:" + bucket + ", " + e.getMessage(), e);
              }
            }
            fileCleanupTransaction.cacheLoeschen();
          }
        } catch (Exception e) {
          throw new RuntimeException(cleaner.getName() + ", " + e.getMessage(), e);
        }
      }
      LOG.info("Dateibereinigung Beendet");
    }
  }

}
