
package de.ivu.elect.exchange.csv;

import java.math.BigDecimal;
import java.util.Map;

import org.jsefa.common.converter.SimpleTypeConverter;
import org.jsefa.common.converter.provider.SimpleTypeConverterProvider;
import org.jsefa.common.lowlevel.filter.HeaderAndFooterFilter;
import org.jsefa.csv.config.CsvConfiguration;
import org.jsefa.csv.lowlevel.config.CsvLowLevelConfiguration;
import org.jsefa.csv.lowlevel.config.EscapeMode;
import org.jsefa.csv.lowlevel.config.QuoteMode;

import de.ivu.elect.business.exchange.control.CsvFileKonfiguration;
import de.ivu.elect.exchange.csv.converter.ElectCsvBigDecimalConverter;
import de.ivu.elect.exchange.csv.converter.ElectCsvBooleanConverter;
import de.ivu.elect.exchange.csv.converter.ElectCsvIntegerConverter;
import de.ivu.elect.exchange.csv.converter.ElectCsvLongConverter;
import de.ivu.elect.exchange.csv.converter.ElectCsvStringConverter;

public class ElectCsvConfigurationFactory {

  public ElectCsvConfigurationFactory() {
    super();
  }

  public static CsvConfiguration create(CsvDescriptor csvDescriptor) {
    return create(csvDescriptor.getCsvFileKonfiguration(), csvDescriptor.getSimpleTypeConverterMapping());
  }

  private static CsvConfiguration create(CsvFileKonfiguration csvFileKonfiguration, Map<Class< ? >, Class< ? extends SimpleTypeConverter>> simpleTypeConverterMapping) {
    CsvConfiguration csvConfiguration = new CsvConfiguration();
    // NOTE ELECT-6024, Wert NICHT ändern, sonst können vorhandene CSV-Dateien ggf. nicht mehr importiert werden!
    csvConfiguration.setDefaultQuoteMode(QuoteMode.ON_DEMAND);

    CsvLowLevelConfiguration csvLowLevelConfiguration = csvConfiguration.getLowLevelConfiguration();
    csvLowLevelConfiguration.setLineFilter(new HeaderAndFooterFilter(csvFileKonfiguration.getAnzahlKopfzeilen().intValue(), false, false));
    csvLowLevelConfiguration.setFieldDelimiter(csvFileKonfiguration.getTrennzeichen().getCharacter());
    csvLowLevelConfiguration.setQuoteCharacter(csvFileKonfiguration.getAnfuehrungszeichen().getCharacter());
    csvLowLevelConfiguration.setQuoteCharacterEscapeMode(EscapeMode.ESCAPE_CHARACTER);
    csvLowLevelConfiguration.setEscapeCharacter(csvFileKonfiguration.getMaskierungszeichen().getCharacter());
    csvLowLevelConfiguration.setUseDelimiterAfterLastField(csvFileKonfiguration.isNutzeTrennzeichenNachLetztemFeld());

    SimpleTypeConverterProvider simpleTypeConverterProvider = csvConfiguration.getSimpleTypeConverterProvider();
    simpleTypeConverterProvider.registerConverterType(Boolean.class, ElectCsvBooleanConverter.class);

    simpleTypeConverterProvider.registerConverterType(String.class, ElectCsvStringConverter.class);

    simpleTypeConverterProvider.registerConverterType(Long.class, ElectCsvLongConverter.class);
    simpleTypeConverterProvider.registerConverterType(Integer.class, ElectCsvIntegerConverter.class);
    simpleTypeConverterProvider.registerConverterType(BigDecimal.class, ElectCsvBigDecimalConverter.class);

    simpleTypeConverterMapping.forEach(simpleTypeConverterProvider::registerConverterType);

    return csvConfiguration;
  }
}
