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

import de.ivu.elect.business.datei.entity.DateiImportBerichtCustomData;
import de.ivu.elect.business.email.boundary.EmailVorlageImporter;
import de.ivu.elect.business.email.control.EmailVorlageExchangeContext;
import de.ivu.elect.business.email.entity.EmailVorlage;
import de.ivu.elect.business.exchange.control.AbstractImporter;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.business.wahl.entity.Wahltermin;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.exchange.AbstractImportSearchView;
import de.ivu.elect.presentation.exchange.ImportDescription;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

@Named(EmailVorlageImportSearchView.COMPONENT_NAME)
@View(viewId = EmailVorlageImportSearchView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORLAGE_IMPORTIEREN)
@OnlineHilfeId(value = "738", mehrfachVergebenOk = true)
public class EmailVorlageImportSearchView extends AbstractImportSearchView<List<EmailVorlage>, EmailVorlageExchangeContext> {

  public static final String COMPONENT_NAME = "emailVorlageImportSearchView";

  public static final String VIEW_ID = "/email/email-vorlage-import-search.xhtml";

  @Inject
  private transient EmailVorlageImporter emailVorlageImporter;

  @Inject
  private transient EmailVorlageImportDescription emailVorlageImportDescription;

  public EmailVorlageImportSearchView() {
    super();
  }

  @Override
  protected ImportDescription getImportDescription() {
    return emailVorlageImportDescription;
  }

  @Override
  protected AbstractImporter<List<EmailVorlage>, EmailVorlageExchangeContext, ?> getImporter() {
    return emailVorlageImporter;
  }

  @Override
  protected DateiImportBerichtCustomData createNewDateiImportBerichtCustomData() {
    return new DateiImportBerichtCustomData();
  }

  @Override
  protected EmailVorlageExchangeContext getContext() {
    Wahltermin selectedWahltermin = getSelectedWahltermin();
    Wahl selectedWahl = getSelectedWahl();
    if (selectedWahltermin == null && selectedWahl != null) {
      selectedWahltermin = selectedWahl.getWahltermin();
    }
    return new EmailVorlageExchangeContext(selectedWahltermin, selectedWahl);
  }
}
