
package de.ivu.elect.presentation.gruppe;

import static java.util.Arrays.asList;

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.PreDestroy;
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.gruppe.boundary.GruppeConversationSelect;
import de.ivu.elect.business.gruppe.boundary.GruppeLogo;
import de.ivu.elect.business.gruppe.entity.Gruppe;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.faces.FacesContextUtil;
import de.ivu.elect.jee.faces.PreConditionCheck;
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.AbstractElectEditView;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.ViewHavingFileUpload;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@OnlineHilfeId("529")
@ViewAccessScoped
@Named(GruppeLogoView.COMPONENT_NAME)
@View(viewId = GruppeLogoView.VIEW_ID, useCase = UseCasePermission.GRUPPE_LOGO_EDIT)
public class GruppeLogoView extends AbstractElectEditView<Gruppe> implements ViewHavingFileUpload {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "gruppeLogoView";

  public static final String VIEW_ID = "/gruppe/gruppe-logo.xhtml";

  @Inject
  private transient FileTypeResolver fileTypeResolver;

  @Inject
  private transient GruppeConversationSelect gruppeSelect;

  @Inject
  private transient GruppeLogoSession gruppeLogoSession;

  @Inject
  private transient GruppeLogo gruppeLogo;

  public GruppeLogoView() {
    super();
  }

  @Nullable
  @PreConditionCheck
  public String checkPreCondition() {
    if (getEntity() == null) {
      return getParentViewId();
    }

    gruppeLogoSession.setLogo(getEntity().getLogo());

    return null;
  }

  @PreDestroy
  public void destroy() {
    gruppeLogoSession.reset();
  }

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

  @Override
  protected Gruppe getEntity() {
    return gruppeSelect.getEntity();
  }

  @Override
  public String delete() {
    return null;
  }

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.GRUPPE_EDIT);
  }

  @Override
  @Redirect
  public String save() {
    if (gruppeLogoSession.getUploadedFile() == null) {
      addInfoMessage("gruppe.logo.unveraendert");
    } else {
      gruppeLogo.saveLogo(getGruppe(), gruppeLogoSession.getUploadedFile().getContent(), gruppeLogoSession.getUploadedFile().getContentType());

      addInfoMessage("gruppe.logo.aktualisiert");
    }

    return goBack(getParentViewId());
  }

  @Override
  public boolean isBtnCancelRendered() {
    return true;
  }

  @Override
  public String cancel() {
    return goBack(getParentViewId());
  }

  @Nonnull
  protected String getParentViewId() {
    return GruppeEditView.PARENT_VIEW_ID;
  }

  @Override
  public void addProperties(FormBuilder formBuilder, Gruppe gruppe) {
    // Do nothing.
  }

  public String getGruppenName() {
    return getGruppe().getName();
  }

  private Gruppe getGruppe() {
    return gruppeSelect.getEntity();
  }

  @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", fileTypeResolver.formatFileTypes(getFileUploadFileTypes()));
  }

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

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

  @Override
  public List<FileType> getFileUploadFileTypes() {
    return asList(FileType.JPEG, FileType.PNG, FileType.SVG);
  }

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

  public void setUploadedFile(UploadedFile uploadedFile) {
    gruppeLogoSession.setUploadedFile(uploadedFile);
  }

  public void finish() {
    FacesContextUtil.redirectToView(VIEW_ID);
  }

  @Nullable
  @Override
  @Redirect
  public String reset() {
    gruppeLogoSession.setUploadedFile(null);
    gruppeLogoSession.setLogo(getEntity().getLogo());

    return super.reset();
  }

  public boolean isLogoVorhanden() {
    return gruppeLogoSession.isLogoVorhanden();
  }

  public boolean isBtnDeleteDisabled() {
    return !isLogoVorhanden();
  }

  @Redirect
  public String deleteLogo() {
    gruppeLogo.deleteLogo(getGruppe());

    gruppeLogoSession.setUploadedFile(null);
    gruppeLogoSession.setLogo(null);

    addInfoMessage("gruppe.logo.geloescht");

    return VIEW_ID;
  }
}
