
package de.ivu.elect.exchange.konfiguration.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.konfiguration.entity.Konfiguration;
import de.ivu.elect.business.konfiguration.entity.KonfigurationExchangeContext;
import de.ivu.elect.exchange.CsvImportMapper;
import de.ivu.elect.exchange.csv.CsvMarshaller;
import de.ivu.elect.exchange.konfiguration.AbstractDefaultKonfigurationMapper;
import de.ivu.elect.jee.exchange.Violations;

public class DefaultCsvKonfigurationMapper extends AbstractDefaultKonfigurationMapper<DefaultExchangeKonfigurationCsv> implements CsvImportMapper<List<Konfiguration>, KonfigurationExchangeContext> {

  @Inject
  private transient CsvMarshaller csvMarshaller;

  public DefaultCsvKonfigurationMapper() {
    super();
  }

  @Override
  public void serialize(List<Konfiguration> object, OutputStream os, KonfigurationExchangeContext context, FileKonfiguration fileKonfiguration) {
    csvMarshaller.serialize(DefaultExchangeKonfigurationCsv.class, mappeEntityInternToExtern(object), os, fileKonfiguration);
  }

  @Override
  public List<Konfiguration> deserialize(InputStream is, KonfigurationExchangeContext context, FileKonfiguration fileKonfiguration) {
    return mappeEntityExternToIntern(csvMarshaller.deserialize(DefaultExchangeKonfigurationCsv.class, is, fileKonfiguration));
  }

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

  @Override
  public FileFormat getFileFormat() {
    return new FileFormat(FILE_FORMAT_NAME, FileType.CSV);
  }

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

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

  protected List<Konfiguration> mappeEntityExternToIntern(List<DefaultExchangeKonfigurationCsv> entityListe) {
    List<Konfiguration> mappedBenutzer = new ArrayList<>(entityListe.size());
    for (DefaultExchangeKonfigurationCsv entity : entityListe) {
      mappedBenutzer.add(mappeEntity(entity));
    }
    return mappedBenutzer;
  }

  @Override
  public boolean isAcceptContext(KonfigurationExchangeContext context) {
    return true;
  }

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