
package de.ivu.elect.presentation.dokumente;

import java.util.List;

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.dokumente.entity.DokumentVorlage;
import de.ivu.elect.business.dokumente.entity.DokumentVorlageProperties;
import de.ivu.elect.jee.faces.upload.UploadedFileWithCanonicalFilename;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.presentation.ViewHavingFileUpload;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@OnlineHilfeId("176")
@Named(AbstractDokumentVorlageEditView.COMPONENT_NAME)
public class DokumentVorlageEditView extends AbstractDokumentVorlageEditView implements ViewHavingFileUpload {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient DokumentVorlageUniqueUKFacesValidator uniqueUKFacesValidator;

  @Inject
  private transient DokumentVorlageAuswahl dokumentVorlageAuswahl;

  @Inject
  private transient DokumentVorlageCreateAllowedFileTypes dokumentVorlageCreateAllowedFileTypes;

  @Inject
  private transient DokumentVorlageUploadedFileStore dokumentVorlageUploadedFileStore;

  public DokumentVorlageEditView() {
    super();
  }

  @Override
  protected boolean isGebietBearbeitenRechtPruefen() {
    return false;
  }

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

  @Override
  public String getFileUploadLabel() {
    return getMessage("datei.import.edit.action.upload.choose", dokumentVorlageCreateAllowedFileTypes.getAllowedFileTypeNames());
  }

  @Override
  public String getFileUploadAllowTypes() {
    return dokumentVorlageCreateAllowedFileTypes.getAllowedFileTypeRegEx();
  }

  @Override
  public String getFileUploadAccept() {
    return dokumentVorlageCreateAllowedFileTypes.getAllowedFileTypeHtmlAccept();
  }

  @Override
  public List<FileType> getFileUploadFileTypes() {
    return dokumentVorlageCreateAllowedFileTypes.getAllowedFileTypes();
  }

  public UploadedFile getUploadedFile() {
    return dokumentVorlageUploadedFileStore.getUploadedFile();
  }

  public void setUploadedFile(UploadedFile uploadedFile) {
    this.dokumentVorlageUploadedFileStore.setUploadedFile(uploadedFile);
  }

  @Override
  public void addProperties(FormBuilder formBuilder, DokumentVorlage dokumentVorlage) {
    DokumentVorlageProperties dokumentVorlageProperties = dokumentVorlage.getProperties();

    formBuilder.addProperty(new DokumentVorlageArtNachAnwendungGefiltertProperty(dokumentVorlage, dokumentVorlageAuswahl));
    formBuilder.addProperty(dokumentVorlageProperties.getNameProperty()).withValidatorAndPk(uniqueUKFacesValidator, dokumentVorlage.getPk());
    formBuilder.addProperty(dokumentVorlageProperties.getBeschreibungProperty());
  }

  @Override
  protected void callSave(DokumentVorlage dokumentVorlage) {
    edit.save(dokumentVorlage, getUploadedFile());
  }

  @Override
  protected void resetStateAfterAction() {
    super.resetStateAfterAction();

    dokumentVorlageUploadedFileStore.reset();
  }
}
