package de.ivu.elect.presentation.internet;

import javax.annotation.Nullable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.PrimeFaces;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import de.ivu.elect.business.internet.boundary.InternetWidgetConversationSelect;
import de.ivu.elect.business.internet.boundary.InternetWidgetEdit;
import de.ivu.elect.business.internet.boundary.InternetWidgetFileConversationSelect;
import de.ivu.elect.business.internet.entity.InternetWidget;
import de.ivu.elect.business.internet.entity.InternetWidgetCustomData;
import de.ivu.elect.business.internet.entity.InternetWidgetFileEntity;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.faces.PreConditionCheck;
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.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.internet.form.InternetWidgetFileViewFormBuilder;
import de.ivu.elect.util.FileUtil;

@RequestScoped
@Named(InternetWidgetFileEditView.COMPONENT_NAME)
@View(viewId = InternetWidgetFileEditView.VIEW_ID, useCase = UseCasePermission.INTERNET_WIDGET_EDIT)
@OnlineHilfeId(value = "750", mehrfachVergebenOk = true)
public class InternetWidgetFileEditView extends AbstractElectEditView<InternetWidgetFileEntity> {

  public static final String COMPONENT_NAME = "internetWidgetFileEditView";

  public static final String VIEW_ID = "/internet/internet-widget-file-edit.xhtml";

  public static final String PARENT_VIEW_ID = InternetWidgetSearchView.VIEW_ID;

  private static final long serialVersionUID = 1L;

  @Inject
  private transient InternetWidgetFileConversationSelect internetWidgetFileConversationSelect;

  @Inject
  private transient InternetWidgetConversationSelect internetWidgetConversationSelect;

  @Inject
  private transient InternetWidgetFileEditViewStore internetWidgetFileEditViewStore;

  @Inject
  private transient InternetWidgetFileViewFormBuilder viewFormBuilder;

  @Inject
  private transient InternetWidgetEdit internetWidgetEdit;

  public InternetWidgetFileEditView() {
    super();
  }

  @Override
  public InternetWidgetFileEntity getEntity() {
    return internetWidgetFileConversationSelect.getEntity();
  }

  @Nullable
  @PreConditionCheck
  public String checkCondition() {
    if (getEntity() != null) {
      return null;
    }

    return cancel();
  }

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

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

  @Override
  public void addProperties(FormBuilder formBuilder, InternetWidgetFileEntity internetWidgetFileEntity) {
    viewFormBuilder.addProperties(formBuilder, internetWidgetFileEntity);
  }

  @Override
  @Redirect
  public String delete() {

    InternetWidgetFileEntity fileEntity = internetWidgetFileConversationSelect.getEntity();
    InternetWidget entity = internetWidgetConversationSelect.getEntity();

    InternetWidgetCustomData customData = entity.getCustomData(InternetWidgetCustomData.class);
    customData.getFiles().remove(fileEntity.getInternetWidgetFile());
    customData.filesChanged();

    internetWidgetEdit.save(entity);

    internetWidgetFileEditViewStore.reset();
    addInfoMessage("internet.widget.file.deleted");
    return PARENT_VIEW_ID;
  }

  @Override
  @Redirect
  public String save() {
    if (getUploadedFile() != null) {
      InternetWidgetFileEntity fileEntity = getEntity();
      fileEntity.getInternetWidgetFile().setContent(getUploadedFile().getContents());
    }

    InternetWidget entity = internetWidgetConversationSelect.getEntity();
    InternetWidgetCustomData customData = entity.getCustomData(InternetWidgetCustomData.class);
    customData.filesChanged();
    internetWidgetEdit.save(entity);

    internetWidgetFileEditViewStore.reset();
    addInfoMessage("internet.widget.file.saved");
    return PARENT_VIEW_ID;
  }

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

  @Override
  @Redirect
  public String cancel() {
    internetWidgetFileEditViewStore.reset();
    return PARENT_VIEW_ID;
  }

  public void handleFileUpload(FileUploadEvent event) {
    UploadedFile file = event.getFile();
    internetWidgetFileEditViewStore.setFile(file);
    if (file != null && StringUtils.isNotBlank(file.getFileName()) && getEntity() != null && getEntity().getInternetWidgetFile() != null) {
      getEntity().getInternetWidgetFile().setFilename(normalize(file));
      PrimeFaces.current().ajax().update("frmEdit");
    }

  }

  private String normalize(UploadedFile file) {
    String fileName = file.getFileName();
    if (fileName.contains(".")) {
      int lastSeparator = fileName.lastIndexOf('.');
      String prefix = fileName.substring(0, lastSeparator);
      String extension = fileName.substring(lastSeparator);
      return FileUtil.normalizeFileName(prefix) + "." + FileUtil.normalizeFileName(extension);
    }
    return FileUtil.normalizeFileName(fileName);
  }

  public UploadedFile getUploadedFile() {
    return internetWidgetFileEditViewStore.getFile();
  }

}
