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

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;

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

import de.ivu.document.generator.template.monitor.DocumentProgress;
import de.ivu.document.generator.template.monitor.DocumentProgressMonitor;
import de.ivu.document.generator.template.openoffice.OOConnection;
import de.ivu.elect.business.system.entity.PreShutdownEvent;

@ApplicationScoped
public class OpenOfficeApplication implements Serializable {

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

  private static final long serialVersionUID = 1L;

  @PostConstruct
  public void startUp() {
    LOG.info("StartUp");
  }

  public void foo() {
    // nichts zu tun
  }

  @PreDestroy
  public void shutdown() {
    LOG.info("Shutdown");
    OOConnection.INSTANCE.shutdown(new ElectDocumentProgressMonitor());
  }

  public void onPreShutdownEvent(@Observes(notifyObserver = Reception.IF_EXISTS) PreShutdownEvent event) {
    LOG.info("PreShutdownEvent");
    shutdown();
  }

  private static class ElectDocumentProgressMonitor implements DocumentProgressMonitor {
    @Override
    public void progress(DocumentProgress documentProgress) {
      LOG.info("Progress: " + documentProgress);
    }

    @Override
    public void progress(DocumentProgress documentProgress, int i, int i1) {
      LOG.info("Progress: " + documentProgress);
    }

    @Override
    public boolean isCanceled() {
      return false;
    }
  }
}
