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

import java.util.Arrays;
import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.file.UploadedFile;

import de.ivu.elect.business.datei.entity.FileType;
import de.ivu.elect.business.datei.entity.FileTypeResolver;
import de.ivu.elect.business.ergebnis.boundary.NlWasModelNa142BulkExporter;
import de.ivu.elect.business.exchange.control.FileFormat;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.wahl.control.AbstractNlExportArtefaktNachSystemExportstatusExportContext;
import de.ivu.elect.jee.exception.ElectCommonException;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.MessageProperty;
import de.ivu.elect.jee.presentation.dynamicform.ReadOnlyStringProperty;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.ViewHavingFileUpload;
import de.ivu.elect.presentation.exchange.ExportViewResult;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@OnlineHilfeId("TODO")
@Named(NlWasModelNa142BulkExportView.COMPONENT_NAME)
@View(viewId = NlWasModelNa142BulkExportView.VIEW_ID, useCase = UseCasePermission.DOKUMENT_ERZEUGEN_GESAMT_ERGEBNIS)
public class NlWasModelNa142BulkExportView extends AbstractNlWasErgebnisArtefaktNachGebietErgebniseingabestatusBulkExportView implements ViewHavingFileUpload {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "nlWasModelNa142BulkExportView";

  public static final String VIEW_ID = "/ergebnis/nl-was-model-na14-2-bulk-export.xhtml";

  @Inject
  private transient NlWasModelNa142BulkExporter nlWasModelNa142BulkExporter;

  @Inject
  private transient NlWasModelNa142BulkExportDescription nlWasModelNa142BulkExportDescription;

  @Inject
  private transient FileTypeResolver fileTypeResolver;

  private UploadedFile file;

  public NlWasModelNa142BulkExportView() {
    super();
  }

  @Override
  protected void addProperties(FormBuilder formBuilder, @Nullable FileFormat fileFormat, AbstractNlExportArtefaktNachSystemExportstatusExportContext context) {
    formBuilder.addSeparator("file.format.ergebnisdokument.export.eml.upload.separator");
    formBuilder.addProperty(new MessageProperty(getMessage("file.format.ergebnisdokument.export.eml.upload.message")));

    var fileName = "";

    if (file != null) {
      fileName = file.getFileName();
    }

    formBuilder.addProperty(new ReadOnlyStringProperty(getMessage("file.format.ergebnisdokument.export.eml.upload.filename"), fileName));

    super.addProperties(formBuilder, fileFormat, context);
  }

  @Override
  public NlWasModelNa142BulkExportDescription getExportDescription() {
    return nlWasModelNa142BulkExportDescription;
  }

  @Override
  protected NlWasModelNa142BulkExporter getExporter() {
    return nlWasModelNa142BulkExporter;
  }

  @Override
  public void handleFileUpload(FileUploadEvent event) {
    file = event.getFile();

    resetExportParameterModel();
  }

  @Override
  public String getFileUploadAllowTypes() {
    return fileTypeResolver.getFileTypesHtmlRegEx(getFileUploadFileTypes());
  }

  @Override
  public String getFileUploadAccept() {
    return fileTypeResolver.getFileTypesHtmlAccept(getFileUploadFileTypes());
  }

  @Override
  public List<FileType> getFileUploadFileTypes() {
    return Arrays.asList(FileType.XML, FileType.ZIP);
  }

  @Override
  public String getFileUploadLabel() {
    return getMessage("file.format.ergebnisdokument.export.eml.upload.button", fileTypeResolver.formatFileTypes(getFileUploadFileTypes()));
  }

  @Override
  public ExportViewResult export(FileFormat fileFormat, AbstractNlExportArtefaktNachSystemExportstatusExportContext context, @Nullable FileType finaleKonvertierung) {
    if (file == null) {
      throw new ElectCommonException(ElectCommonException.RedirectDestination.NONE, ElectCommonException.LogLevel.MESSAGE_ONLY,
          "file.format.ergebnisdokument.export.eml.upload.missing");
    }

    context.put(NlWasErgebnisArtefaktNachGebietKorrekturBulkExportView.EML_ERGEBNIS_DIFFERENZ_UPLOADED_FILE, file);

    return super.export(fileFormat, context, finaleKonvertierung);
  }
}
