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

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.AbstractImportBestaetigenView;
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(EmailVorlageImportBestaetigenView.COMPONENT_NAME)
@View(viewId = EmailVorlageImportBestaetigenView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORLAGE_IMPORTIEREN)
@OnlineHilfeId(value = "738", mehrfachVergebenOk = true)
public class EmailVorlageImportBestaetigenView extends AbstractImportBestaetigenView<List<EmailVorlage>, EmailVorlageExchangeContext> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "emailVorlageImportBestaetigenView";

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

  @Inject
  private transient EmailVorlageImporter emailVorlageImporter;

  @Inject
  private transient EmailVorlageImportDescription emailVorlageImportDescription;

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

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

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