
package de.ivu.elect.exchange.konfiguration.xml;

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

import javax.inject.Inject;
import javax.xml.bind.JAXBException;

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

public class DefaultXmlKonfigurationMapper extends AbstractDefaultKonfigurationMapper<DefaultExchangeKonfigurationXml> {

  @Inject
  protected XmlMarshaller<DefaultExchangeXmlKonfigurationListe> xmlMarshaller;

  @Override
  public void serialize(List<Konfiguration> object, OutputStream os, KonfigurationExchangeContext context,
      FileKonfiguration fileKonfiguration) throws MappingException {
    try {
      xmlMarshaller.serialize(mappeEntityInternToExtern(object), os);
    } catch (JAXBException e) {
      throw new MappingException(e.getMessage(), e);
    }
  }

  @Override
  public List<Konfiguration> deserialize(InputStream is, KonfigurationExchangeContext context, FileKonfiguration fileKonfiguration)
      throws MappingException {
    try {
      return mappeEntityExternToIntern(xmlMarshaller.deserialize(DefaultExchangeXmlKonfigurationListe.class, is));
    } catch (JAXBException e) {
      throw new MappingException(e.getMessage(), e);
    }
  }

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

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

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

  protected DefaultExchangeXmlKonfigurationListe mappeEntityInternToExtern(List<Konfiguration> entityListe) {
    DefaultExchangeXmlKonfigurationListe result = new DefaultExchangeXmlKonfigurationListe();

    for (Konfiguration entity : entityListe) {
      result.getKonfigurationen().add(mappeEntity(entity));
    }

    return result;
  }

  protected List<Konfiguration> mappeEntityExternToIntern(DefaultExchangeXmlKonfigurationListe entityListe) {
    List<Konfiguration> mappedEntities = new ArrayList<>(entityListe.getKonfigurationen().size());

    for (DefaultExchangeKonfigurationXml entity : entityListe.getKonfigurationen()) {
      mappedEntities.add(mappeEntity(entity));
    }

    return mappedEntities;
  }

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