package de.ivu.elect.wus.exchange.personenpruefung;

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.boundary.NlExportParameter;
import de.ivu.elect.business.wahl.control.WahlExportContext;
import de.ivu.elect.business.wahl.control.WahlExportObject;
import de.ivu.elect.core.exchange.mapper.WahlInternExternMapper;
import de.ivu.elect.interfaces.core.Wahl;
import de.ivu.elect.wus.exchange.wahl.WahlExportMapper;

import javax.inject.Inject;
import java.io.IOException;
import java.io.OutputStream;

public class NlGbavBatchExportMapper implements WahlExportMapper {

  @Inject
  private transient WahlInternExternMapper wahlInternExternMapper;

  @Inject
  private transient NlGbavBatchExportMapperHelper nlGbavBatchExportMapperHelper;

  private final String GBAV_BATCH_FILENAME = "gbavbatch";

  @Override
  public void serialize(WahlExportObject exportObject, OutputStream os, WahlExportContext context, FileKonfiguration fileKonfiguration) throws MappingException {
    Wahl wahl = wahlInternExternMapper.mappe(exportObject.getWahldaten(), exportObject.getDokumentVorlagen(), exportObject.getWahlParameters());
    context.put(NlExportParameter.CUSTOM_DATEI_NAME.name(), GBAV_BATCH_FILENAME);
    try {
      nlGbavBatchExportMapperHelper.serialize(wahl.getListenkandidaturen(), os);
    } catch (IOException ioException) {
      throw new MappingException(ioException.getMessage(), ioException.getCause());
    }
  }

  @Override
  public FileFormat getFileFormat() {
    return new FileFormat("wahl.export.gbavbatch.name", FileType.TXT);
  }

  @Override
  public boolean isAcceptContext(WahlExportContext context) {
    return context.getWahl() != null && context.containsKey(NlExportParameter.IS_GBAV_VIEW.name());
  }
}
