package de.ivu.elect.was.exchange.wahl;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

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

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

import de.ivu.elect.EmlImportMapperHelper;
import de.ivu.elect.business.datei.entity.Datei;
import de.ivu.elect.business.datei.entity.FileType;
import de.ivu.elect.business.exchange.control.FileFormat;
import de.ivu.elect.business.exchange.control.FileKonfiguration;
import de.ivu.elect.business.exchange.control.MappingException;
import de.ivu.elect.business.wahl.control.WahlImportObject;
import de.ivu.elect.business.wahl.entity.WahlImportContext;
import de.ivu.elect.jee.exchange.Violations;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;

public class WasEml110WahlZipImportMapper implements WasWahlImportMapper {

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

  @Inject
  private transient WasEml110WahlImportMapper wasEml110WahlImportMapper;

  @Inject
  private transient EmlImportMapperHelper emlImportMapperHelper;

  @Override
  public FileFormat getFileFormat() {
    return new FileFormat(wasEml110WahlImportMapper.getFileFormat().getName(), FileType.ZIP);
  }

  @Override
  public void addProperties(FormBuilder formBuilder, WahlImportContext context, @Nullable Datei datei) {
    if (datei != null) {
      try {
        try (InputStream is = emlImportMapperHelper.getInputStream(datei)) {
          try (ZipInputStream zis = new ZipInputStream(is)) {
            if (springeZuEml(zis)) {
              wasEml110WahlImportMapper.addProperties(formBuilder, context, zis);
            }
          }
        }
      } catch (Exception e) {
        LOG.warn("Fehler beim Lesen der Datei: " + datei + ", " + e.getMessage());
      }
    }

  }

  private boolean springeZuEml(ZipInputStream zis) throws IOException {
    ZipEntry zipEntry;
    while ((zipEntry = zis.getNextEntry()) != null) {
      if (zipEntry.getName().toLowerCase().endsWith("eml.xml")) {
        return true;
      }
    }

    return false;
  }

  @Override
  public WahlImportObject deserialize(InputStream is, WahlImportContext context, FileKonfiguration fileKonfiguration) throws MappingException {
    try (ZipInputStream zis = new ZipInputStream(is)) {
      springeZuEml(zis);
      return wasEml110WahlImportMapper.deserialize(zis, context, fileKonfiguration);
    } catch (IOException e) {
      throw new MappingException(e.getMessage(), e);
    }
  }

  @Override
  public Violations validateInputFormat(InputStream is, WahlImportContext context, FileKonfiguration fileKonfiguration) {
    try (ZipInputStream zis = new ZipInputStream(is)) {
      springeZuEml(zis);
      return wasEml110WahlImportMapper.validateInputFormat(zis, context, fileKonfiguration);
    } catch (IOException e) {
      LOG.warn("Error reading eml: " + e.getMessage(), e);
      Violations violations = new Violations();
      violations.addError("exception.unknown.message");
      return violations;
    }
  }

  @Override
  public boolean isAcceptContext(WahlImportContext context) {
    return wasEml110WahlImportMapper.isAcceptContext(context);
  }
}
