
package de.ivu.elect.exchange;

import java.io.InputStream;
import java.io.OutputStream;

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

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

import de.ivu.document.generator.template.OpenOfficeKonfiguration;
import de.ivu.document.generator.template.monitor.DocumentProgress;
import de.ivu.document.generator.template.monitor.DocumentProgressMonitor;
import de.ivu.document.generator.template.pdf.OdtKonvertierer;
import de.ivu.elect.business.datei.entity.FileType;
import de.ivu.elect.business.dokumente.control.ElectOdtKonvertierer;
import de.ivu.elect.business.dokumente.control.FileTypeMapper;
import de.ivu.elect.business.dokumente.control.OpenOfficeKonfigurationFactory;

@Specializes
public class ExchangeOdtKonvertierer extends ElectOdtKonvertierer {

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

  private static final long serialVersionUID = 1L;

  @Inject
  private transient OpenOfficeKonfigurationFactory openOfficeDokumentKonfiguration;

  @Override
  public void konvertiere(InputStream is, InputStream ott, OutputStream os, FileType sourceFileType, FileType targetFileType) {
    konvertiere(is, ott, os, sourceFileType, targetFileType, openOfficeDokumentKonfiguration.createConfig());
  }

  protected void konvertiere(InputStream is, InputStream ott, OutputStream os, FileType sourceFileType, FileType targetFileType, OpenOfficeKonfiguration openOfficeKonfiguration) {
    new OdtKonvertierer().konvertiereDokument(is, toDocumentGeneratorFileType(sourceFileType), ott , os, toDocumentGeneratorFileType(targetFileType), openOfficeKonfiguration, new DocumentProgressMonitor() {

      @Override
      public void progress(DocumentProgress documentProgress) {
        LOG.info("documentProgress=" + documentProgress);
      }

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

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

  public static de.ivu.document.generator.template.FileType toDocumentGeneratorFileType(FileType fileType) {
    try {
      return FileTypeMapper.mappe(fileType);
    } catch (IllegalArgumentException ex) {
      throw new RuntimeException("Das Ausgabeformat [" + fileType + "] wird nicht unterstützt.", ex);
    }
  }
}
