
package de.ivu.elect.presentation.dokumente;

import static org.apache.commons.lang3.StringUtils.isBlank;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.convert.EnumConverter;
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.boundary.BaseDokumentVorlageBoundaryCreate;
import de.ivu.elect.business.dokumente.entity.DokumentVorlage;
import de.ivu.elect.business.dokumente.entity.DokumentVorlageArt;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCaseSecure;
import de.ivu.elect.jee.faces.FacesContextUtil;
import de.ivu.elect.jee.faces.upload.UploadedFileWithCanonicalFilename;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.ViewHavingFileUpload;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@OnlineHilfeId("171")
@Named(DokumentVorlageCreateView.COMPONENT_NAME)
public class DokumentVorlageCreateView extends AbstractDokumentVorlageCreateView implements ViewHavingFileUpload {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient BaseDokumentVorlageBoundaryCreate create;

  @Inject // TODO Wird momentan noch in elect-base-war/src/main/webapp/dokumente/dokument-vorlage-create.xhtml per XHTML gesetzt, cf. DokumentVorlageEditView#addProperties!
  private transient DokumentVorlageUniqueUKFacesValidator uniqueUKFacesValidator;

  @Inject
  private transient DokumentVorlageAuswahl dokumentVorlageAuswahl;

  @Inject
  private transient DokumentVorlageCreateAllowedFileTypes dokumentVorlageCreateAllowedFileTypes;

  @Inject
  private transient DokumentVorlageUploadedFileStore dokumentVorlageUploadedFileStore;

  public DokumentVorlageCreateView() {
    super();
  }

  @Override
  @PostConstruct
  public void init() {
    super.init();

    if (isWahlterminSelected()) {
      getEntity().setWahltermin(getSelectedWahltermin());
    } else {
      getEntity().setWahltermin(getSelectedWahl().getWahltermin());
    }
  }

  @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);

    if (uploadedFile != null) {
      String fileName = uploadedFile.getFileName();

      DokumentVorlage dokumentVorlage = getEntity();
      DokumentVorlageArt dokumentVorlageArt = DokumentVorlageArt.getDokumentVorlageArt(fileName);

      if (dokumentVorlageArt != null) {
        dokumentVorlage.setDokumentVorlageArt(dokumentVorlageArt);
      }

      if (isBlank(dokumentVorlage.getName())) {
        dokumentVorlage.setName(DokumentVorlageArt.getDokumentVorlageName(fileName));
      }
    }
  }

  public List<DokumentVorlageArt> getDokumentVorlageArten() {
    return dokumentVorlageAuswahl.getDokumentVorlageArten();
  }

  public String getDokumentVorlageArtLabel(DokumentVorlageArt value) {
    if (value == null) {
      return "";
    }

    return getMessage(value);
  }

  public EnumConverter getDokumentVorlageArtConverter() {
    return new EnumConverter(DokumentVorlageArt.class);
  }

  @Override
  public void addProperties(FormBuilder formBuilder, DokumentVorlage dokumentVorlage) {
    // TODO cf. DokumentVorlageEditView#addProperties!
  }

  @Override
  @Redirect
  @UseCaseSecure(UseCasePermission.DOKUMENT_VORLAGE_CREATE)
  public String save() {
    if (getUploadedFile() == null) {
      addErrorMessage("dokument.vorlage.create.action.filename.required.message");

      return FacesContextUtil.getCurrentViewId();
    }

    return super.save();
  }

  public boolean isBtnSaveAndCreateRendered() {
    return isBtnSaveRendered();
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.DOKUMENT_VORLAGE_CREATE)
  public String saveAndCreate() {
    String outcome = save();

    if (getViewId().equals(outcome)) {
      return outcome; // Irgendwas lief schief und der aktuelle View soll nochmals angezeigt werden...
    }

    init();

    return getViewId();
  }

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

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

    dokumentVorlageUploadedFileStore.reset();
  }
}
