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

import java.io.InputStream;
import java.io.OutputStream;
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.gruppe.control.GruppeExchangeContext;
import de.ivu.elect.business.gruppe.control.GruppeExportObject;
import de.ivu.elect.business.gruppe.entity.Gruppe;
import de.ivu.elect.exchange.CsvImportMapper;
import de.ivu.elect.exchange.csv.CsvDescriptor;
import de.ivu.elect.exchange.csv.CsvMarshaller;
import de.ivu.elect.jee.exchange.Violations;
import de.ivu.elect.wus.exchange.gruppe.AbstractDefaultGruppeMapper;

public class DefaultCsvGruppeMapper extends AbstractDefaultGruppeMapper<DefaultExchangeGruppeCsv> implements CsvImportMapper<List<Gruppe>, GruppeExchangeContext> {

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

  @Inject
  private transient CsvMarshaller csvMarshaller;

  public DefaultCsvGruppeMapper() {
    super();
  }

  @Override
  public FileType getFileType() {
    return FileType.CSV;
  }

  @Override
  public void serialize(GruppeExportObject object, OutputStream os, GruppeExchangeContext context, FileKonfiguration fileKonfiguration) {
    CsvDescriptor csvDescriptor = CsvDescriptor.forFileKonfiguration(fileKonfiguration);
    csvDescriptor.setWithHeader(true);

    csvMarshaller.serialize(DefaultExchangeGruppeCsv.class, mappeEntityInternToExtern(object.getGruppen()), os, csvDescriptor);
  }

  @Override
  public List<Gruppe> deserialize(InputStream is, GruppeExchangeContext context, FileKonfiguration fileKonfiguration) {
    return mappeEntityExternToIntern(csvMarshaller.deserialize(DefaultExchangeGruppeCsv.class, is, fileKonfiguration));
  }

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

  @Override
  protected DefaultExchangeGruppeCsv createEntity() {
    return new DefaultExchangeGruppeCsv();
  }

  protected List<DefaultExchangeGruppeCsv> mappeEntityInternToExtern(List<Gruppe> entityListe) {
    List<DefaultExchangeGruppeCsv> mappedEntity = new ArrayList<>(entityListe.size());
    for (Gruppe entity : entityListe) {
      mappedEntity.add(mappeEntity(entity));
    }
    return mappedEntity;
  }



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