
package de.ivu.elect.presentation.praesentation;

import static java.util.Arrays.asList;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.core.api.scope.ViewAccessScoped;
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.dokumente.entity.DokumentVorlageArt;
import de.ivu.elect.business.internet.boundary.PraesentationKonfigurieren;
import de.ivu.elect.jee.faces.FacesMessageUtil;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.faces.upload.UploadedFileWithCanonicalFilename;
import de.ivu.elect.presentation.ViewHavingFileUpload;

@ViewAccessScoped
@Named(PraesentationKonfigurierenVorlageView.COMPONENT_NAME)
public class PraesentationKonfigurierenVorlageView implements Serializable, ViewHavingFileUpload {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "praesentationKonfigurierenVorlageView";

  private static final DokumentVorlageArt DOKUMENT_VORLAGE_ART_EXPECTED = PraesentationKonfigurieren.DOKUMENT_VORLAGE_ART_EXPECTED;

  @Inject
  private transient Messages messages;

  @Inject
  private transient FileTypeResolver fileTypeResolver;

  private UploadedFile uploadedFile;

  public PraesentationKonfigurierenVorlageView() {
    super();
  }

  @Override
  public void handleFileUpload(FileUploadEvent event) {
    setUploadedFile(new UploadedFileWithCanonicalFilename(event.getFile()));
  }

  @Override
  public String getFileUploadLabel() {
    return getMessage("praesentation.vorlage.import.edit.action.upload.choose", fileTypeResolver.formatFileTypes(getFileUploadFileTypes()));
  }

  @Override
  public String getFileUploadAllowTypes() {
    return FILE_UPLOAD_ALLOW_TYPES_ALL;
  }

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

  @Override
  public List<FileType> getFileUploadFileTypes() {
    return asList(FileType.ALL);
  }

  public UploadedFile getUploadedFile() {
    return uploadedFile;
  }

  public void setUploadedFile(UploadedFile uploadedFile) {
    // ELECT-3038
    this.uploadedFile = new UploadedFileWithCanonicalFilename(uploadedFile);

    DokumentVorlageArt dokumentVorlageArt = DokumentVorlageArt.getDokumentVorlageArt(this.uploadedFile.getFileName());

    if (!DOKUMENT_VORLAGE_ART_EXPECTED.equals(dokumentVorlageArt)) {
      FacesMessageUtil.addErrorMessage("praesentation.vorlage.import.nicht.erkannt");

      this.uploadedFile = null;
    }
  }

  public String getFilename() {
    if (uploadedFile == null) {
      return null;
    }

    return DokumentVorlageArt.getDokumentVorlageName(this.uploadedFile.getFileName(), DOKUMENT_VORLAGE_ART_EXPECTED);
  }

  public void reset() {
    uploadedFile = null;
  }

  private String getMessage(String msgKey, Object... params) {
    return getMessages().getMessageForMessageKey(msgKey, params);
  }

  private Messages getMessages() {
    return messages;
  }
}
