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

import de.ivu.elect.business.datei.entity.FileType;
import de.ivu.elect.business.email.control.EmailVorlageExchangeContext;
import de.ivu.elect.business.email.entity.EmailVorlage;
import de.ivu.elect.business.exchange.control.FileKonfiguration;
import de.ivu.elect.business.exchange.control.MappingException;
import de.ivu.elect.exchange.email.AbstractDefaultEmailVorlageMapper;
import de.ivu.elect.jee.exchange.Violations;
import de.ivu.elect.jee.exchange.XmlMarshaller;

import javax.inject.Inject;
import javax.xml.bind.JAXBException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class DefaultXmlEmailVorlageMapper extends AbstractDefaultEmailVorlageMapper<DefaultEmailVorlageXmlDTO> {

  @Inject
  protected transient XmlMarshaller<DefaultEmailVorlageXmlListe> xmlMarshaller;

  public DefaultXmlEmailVorlageMapper() {
    super();
  }

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

  @Override
  protected FileType getFileType() {
    return FileType.XML;
  }

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

  protected DefaultEmailVorlageXmlListe mappeEntityInternToExtern(List<EmailVorlage> entityListe) {
    DefaultEmailVorlageXmlListe result = new DefaultEmailVorlageXmlListe();
    for (EmailVorlage entity : entityListe) {
      result.getEmailVorlagen().add(mappeEntity(entity));
    }
    return result;
  }

  @Override
  public List<EmailVorlage> deserialize(InputStream is, EmailVorlageExchangeContext emailVorlageExchangeContext,
      FileKonfiguration fileKonfiguration) throws MappingException {
    try {
      return mappeEntityExternToIntern(xmlMarshaller.deserialize(DefaultEmailVorlageXmlListe.class, is), emailVorlageExchangeContext);
    } catch (JAXBException e) {
      throw new MappingException(e.getMessage(), e);
    }
  }

  protected List<EmailVorlage> mappeEntityExternToIntern(DefaultEmailVorlageXmlListe entityListe,
      EmailVorlageExchangeContext emailVorlageExchangeContext) {
    List<EmailVorlage> mappedEmailVorlagen = new ArrayList<>(entityListe.getEmailVorlagen().size());
    for (DefaultEmailVorlageXmlDTO entity : entityListe.getEmailVorlagen()) {
      EmailVorlage mappedEmailVorlage = mappeEntity(entity);
      mappedEmailVorlage.setWahltermin(emailVorlageExchangeContext.getWahltermin());
      mappedEmailVorlage.setWahl(emailVorlageExchangeContext.getWahl());
      mappedEmailVorlagen.add(mappedEmailVorlage);
    }
    return mappedEmailVorlagen;
  }

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

  @Override
  public boolean isAcceptContext(EmailVorlageExchangeContext emailVorlageExchangeContext) {
    return true;
  }
}
