
package de.ivu.elect.presentation.system;

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

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCaseSecure;
import de.ivu.elect.business.security.control.ElectCommonPrincipal;
import de.ivu.elect.business.system.boundary.SystemStatusStore;
import de.ivu.elect.business.system.boundary.SystemStatusUpdate;
import de.ivu.elect.business.system.entity.SystemStatus;
import de.ivu.elect.jee.faces.FacesContextUtil;
import de.ivu.elect.jee.presentation.dynamicform.AdvancedDynaFormModel;
import de.ivu.elect.jee.presentation.dynamicform.DynamicFormEditOrCreateView;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.jee.presentation.view.AbstractView;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;


import static de.ivu.elect.jee.presentation.dynamicform.ReadOnlyPropertyFactory.createReadOnlyProperty;

@OnlineHilfeId("490")
@RequestScoped
@Named(SystemStatusView.COMPONENT_NAME)
@View(viewId = SystemStatusView.VIEW_ID, useCase = UseCasePermission.SYSTEM_STATUS_SEARCH)
public class SystemStatusView extends AbstractView implements DynamicFormEditOrCreateView {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "systemStatusView";

  public static final String VIEW_ID = "/system/system-status.xhtml";

  @Inject
  private transient ElectCommonPrincipal principal;

  @Inject
  private transient SystemStatusStore systemStatusStore;

  @Inject
  private transient SystemStatusUpdate systemStatusUpdate;

  public SystemStatusView() {
    super();
  }

  @Override
  public AdvancedDynaFormModel getModel() {
    FormBuilder formBuilder = new FormBuilder();

    formBuilder.addProperty(createReadOnlyProperty(getMessage("system.status.aktuell"), getMessage(systemStatusStore.getSystemStatus(principal.getMandant().getPk()))));

    // ELECT-8516
    if (FacesContextUtil.isDevelopmentStage()) {
      formBuilder.addProperty(createReadOnlyProperty(getMessage("jsf.stage.aktuell"), getJSFStage()));
    }

    return formBuilder.createModel();
  }

  private String getJSFStage() {
    if (FacesContextUtil.isDevelopmentStage()) {
      return "Development";
    }
    return "Production";
  }

  @Override
  public boolean isBtnSaveRendered() {
    return false;
  }

  @Override
  public String getBtnSaveLabel() {
    return getMessage("entity.edit.action.save");
  }

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

  @Override
  public boolean isBtnResetRendered() {
    return false;
  }

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

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

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

  public boolean isBtnStatusTestsystemRendered() {
    return hasUseCasePermissionSystemStatusEdit();
  }

  public boolean isBtnJsfRendered() {
    return hasUseCasePermissionSystemStatusEdit();
  }

  public boolean isBtnJsfDisabled() {
    return false;
  }

  public boolean isBtnStatusProduktivsystemRendered() {
    return hasUseCasePermissionSystemStatusEdit();
  }

  public boolean isBtnStatusTestsystemDisabled() {
    return isTestmodusEnabled();
  }

  public boolean isBtnStatusProduktivsystemDisabled() {
    return isProduktivmodusEnabled();
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.SYSTEM_STATUS_EDIT)
  public String aktiviereProduktivmodus() {
    if (isProduktivmodusEnabled()) {
      addErrorMessage("system.status.produktiv.bereits.aktiv");
    } else {
      systemStatusUpdate.setSystemStatus(getMandant(), SystemStatus.PRODUKTION);

      addInfoMessage("system.status.produktiv.aktiviert");
    }

    return VIEW_ID;
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.SYSTEM_STATUS_EDIT)
  public String aktiviereTestmodus() {
    if (isTestmodusEnabled()) {
      addErrorMessage("system.status.test.bereits.aktiv");
    } else {
      systemStatusUpdate.setSystemStatus(getMandant(), SystemStatus.TEST);

      addInfoMessage("system.status.test.aktiviert");
    }

    return VIEW_ID;
  }

  private boolean hasUseCasePermissionSystemStatusEdit() {
    return principal.hasUseCasePermission(UseCasePermission.SYSTEM_STATUS_EDIT);
  }

  private boolean isProduktivmodusEnabled() {
    return systemStatusStore.isProduktivmodusEnabled(getMandant());
  }

  private boolean isTestmodusEnabled() {
    return systemStatusStore.isTestmodusEnabled(getMandant());
  }

  private Mandant getMandant() {
    return principal.getMandant();
  }


}
