
package de.ivu.elect.exchange;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

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

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

import de.ivu.document.generator.template.DocumentMerger;
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) {
    konvertiereDokument(is, toDocumentGeneratorFileType(sourceFileType), ott, os, toDocumentGeneratorFileType(targetFileType), openOfficeKonfiguration,
        new ExchangeDocumentProgressMonitor());
  }

  public void konvertiereDokument(InputStream sourceInputStream, de.ivu.document.generator.template.FileType sourceFileType, @Nullable InputStream ottInputStream, OutputStream os, de.ivu.document.generator.template.FileType targetFileType, OpenOfficeKonfiguration konfiguration, DocumentProgressMonitor monitor) {
    // if (true) throw new RuntimeException("B1");
    new OdtKonvertierer().konvertiereDokument(sourceInputStream, sourceFileType, ottInputStream, os, targetFileType, konfiguration, monitor);
  }

  public void merge(File templateFile, List<Object> contentDataList, InputStream ottInputStream, OutputStream os, DocumentProgressMonitor monitorDocument, de.ivu.document.generator.template.FileType targetFileType, OpenOfficeKonfiguration konfiguration) {
    // if (de.ivu.document.generator.template.FileType.PDF.equals(targetFileType)) throw new RuntimeException("B2");
    new DocumentMerger().merge(templateFile, contentDataList, ottInputStream, os, monitorDocument, targetFileType, konfiguration);
  }

  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);
    }
  }

  private static class ExchangeDocumentProgressMonitor implements 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;
    }
  }
}
