
package de.ivu.elect.wus.exchange.wahlrechtspruefung.csv;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

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.wahlrechtspruefung.entity.Pruefkriterium;
import de.ivu.elect.business.wahlrechtspruefung.entity.PruefkriteriumImportContext;
import de.ivu.elect.exchange.CsvImportMapper;
import de.ivu.elect.exchange.csv.CsvMarshaller;
import de.ivu.elect.jee.exchange.Violations;
import de.ivu.elect.wus.exchange.wahlrechtspruefung.PruefkriteriumImportMapper;

public class PruefkriteriumCsvImportMapper implements PruefkriteriumImportMapper, CsvImportMapper<List<Pruefkriterium>, PruefkriteriumImportContext> {

  public static final String FILE_FORMAT_NAME = PruefkriteriumCsvExportMapper.FILE_FORMAT_NAME;

  public static final FileFormat FILE_FORMAT = new FileFormat(FILE_FORMAT_NAME, FileType.CSV);

  @Inject
  private transient CsvMarshaller csvMarshaller;

  public PruefkriteriumCsvImportMapper() {
    super();
  }

  @Override
  public boolean isAcceptContext(PruefkriteriumImportContext pruefkriteriumImportContext) {
    return pruefkriteriumImportContext.getWahl() != null;
  }

  @Override
  public FileFormat getFileFormat() {
    return FILE_FORMAT;
  }

  @Override
  public List<Pruefkriterium> deserialize(InputStream is, PruefkriteriumImportContext pruefkriteriumImportContext, FileKonfiguration fileKonfiguration) throws MappingException {
    return mappe(csvMarshaller.deserialize(PruefkriteriumCsv.class, is, fileKonfiguration));
  }

  private List<Pruefkriterium> mappe(List<PruefkriteriumCsv> liste) {
    List<Pruefkriterium> result = new ArrayList<>(liste.size());
    for (PruefkriteriumCsv pruefkriteriumCsv : liste) {
      result.add(pruefkriteriumCsv.createPruefkriterium());
    }
    return result;
  }

  @Override
  public Violations validateInputFormat(InputStream is, PruefkriteriumImportContext pruefkriteriumImportContext, FileKonfiguration fileKonfiguration) {
    return new Violations();
  }

  @Override
  public List<String> getCsvColumnHeaders(PruefkriteriumImportContext context) {
    return csvMarshaller.extractHeader(PruefkriteriumCsv.class);
  }
}
