
package de.ivu.elect.exchange.rolle.csv;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;

import de.ivu.elect.business.benutzerverwaltung.entity.Rolle;
import de.ivu.elect.business.benutzerverwaltung.entity.RolleExchangeContext;
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.exchange.CsvImportMapper;
import de.ivu.elect.exchange.csv.CsvMarshaller;
import de.ivu.elect.exchange.rolle.AbstractDefaultRolleMapper;
import de.ivu.elect.jee.exchange.Violations;

/**
 * Urspüngliches Standard-CSV-Format. Bitte stattdessen überarbeitetes Format <code>DefaultRolleCsvMapper</code> verwenden!
 */
@Deprecated
public class DefaultExchangeRolleCsvMapper extends AbstractDefaultRolleMapper<DefaultExchangeRolleCsvDTO> implements CsvImportMapper<List<Rolle>, RolleExchangeContext> {

  private static final String FORMAT_NAME = "file.format.rolle.default.deprecated";

  @Inject
  private transient CsvMarshaller csvMarshaller;

  public DefaultExchangeRolleCsvMapper() {
    super();
  }

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

  @Override
  public List<Rolle> deserialize(InputStream is, RolleExchangeContext context, FileKonfiguration fileKonfiguration) {
    return mappeEntityExternToIntern(csvMarshaller.deserialize(DefaultExchangeRolleCsvDTO.class, is, fileKonfiguration));
  }

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

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

  @Override
  public FileFormat getFileFormat() {
    return new FileFormat(FORMAT_NAME, getFileType());
  }

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

  protected List<DefaultExchangeRolleCsvDTO> mappeEntityInternToExtern(List<Rolle> entityListe) {
    return entityListe.stream()
        .map(this::mappeEntity)
        .collect(Collectors.toCollection(() -> new ArrayList<>(entityListe.size())));
  }

  protected List<Rolle> mappeEntityExternToIntern(List<DefaultExchangeRolleCsvDTO> entityListe) {
    return entityListe.stream().map(this::mappeEntity).collect(Collectors.toCollection(() -> new ArrayList<>(entityListe.size())));
  }

  @Override
  public boolean isAcceptContext(RolleExchangeContext context) {
    return !context.isExport();
  }

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