
package de.ivu.elect.presentation.security;

import java.io.Serializable;

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

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.control.ElectCommonPrincipal;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.jee.security.AuthentificationFailedException;
import de.ivu.elect.jee.security.LoginServiceFactory;
import de.ivu.elect.presentation.ElectStartViewResolver;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.KeineOnlineHilfe;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@OnlineHilfeId(KeineOnlineHilfe.KEINE_ONLINE_HILFE)
@Named
@View(viewId = ChangePasswordView.VIEW_ID, useCase = UseCasePermission.OHNE)
@RequestScoped
public class ChangePasswordView implements Serializable {

  public static final String VIEW_ID = "/change-password.xhtml";

  private static final long serialVersionUID = 1L;

  @Inject
  private transient LoginServiceFactory loginServiceFactory;

  @Inject
  protected transient Messages messages;

  @Inject
  private transient ElectCommonPrincipal principal;

  @Inject
  private transient ChangePasswordParameter changePasswordParameter;

  @Inject
  private transient ElectStartViewResolver startViewResolver;

  @Redirect
  public String cancel() {
    changePasswordParameter.cleanup();
    return startViewResolver.getStartViewId();
  }

  public boolean isBtnCancelRendered() {
    return !principal.isMustChangePassword();
  }

  public boolean isBisherigesPasswortRendered(){
    // ELECT-8340
    return !changePasswordParameter.isAfterLogin();
  }

  @Redirect
  public String changePassword() {
    if (changePasswordParameter.getPasswortNeu().equals(changePasswordParameter.getPasswortAlt())) {
      changePasswordParameter.cleanup();
      messages.addErrorMessage("change.password.action.change.error.message.neues.und.passwort.gleich");
      return ChangePasswordView.VIEW_ID;
    }

    if (!changePasswordParameter.getPasswortNeu().equals(changePasswordParameter.getPasswortNeuMatch())) {
      changePasswordParameter.cleanup();
      messages.addErrorMessage("change.password.action.change.error.message");
      return ChangePasswordView.VIEW_ID;
    }

    boolean isMussPasswortWechseln = principal.isMustChangePassword();
    try {
      principal.changePasswordMandant(principal.getMandant().getNummer(), principal.getName(), changePasswordParameter.getPasswortAlt(), changePasswordParameter.getPasswortNeu());
      changePasswordParameter.cleanup();
      messages.addInfoMessage("change.password.action.change.success.message");
    } catch (AuthentificationFailedException e) {
      changePasswordParameter.cleanup();
      messages.addErrorMessage("change.password.action.change.error.message");
      return ChangePasswordView.VIEW_ID;
    }

    if (isMussPasswortWechseln) {
      // Zwangswechsel nach Login
      return startViewResolver.getStartViewIdAfterLogin();
    }
    return startViewResolver.getStartViewId();
  }

  public boolean isBtnChangePasswordRendered() {
    return true;
  }

  public String getPasswortFormatPattern() {
    return loginServiceFactory.getPasswortFormatPattern(principal.getMandant().getNummer());
  }

  public String getPasswortFormatBeschreibung() {
    return loginServiceFactory.getPasswortFormatBeschreibung(principal.getMandant().getNummer());
  }

  /**
   * ELECTVAPP-23 Fügt einen Passworthinweis beim Rendern der Seite hinzu.
   */
  public String getPageTitleAndAddPasswortHinweis() {
    addPasswortHinweis();
    return messages.getMessageForMessageKey("change.password.page.title");
  }

  protected void addPasswortHinweis() {
    // nur für NL
  }
}
