
package de.ivu.elect.business.security.control;

import java.util.ArrayList;
import java.util.Collection;

import javax.inject.Inject;

import de.ivu.elect.business.CurrentCustomer;
import de.ivu.elect.business.konfiguration.control.AbstractKonfigurationInit;
import de.ivu.elect.business.konfiguration.entity.Konfiguration;
import de.ivu.elect.business.konfiguration.entity.KonfigurationInitContext;
import de.ivu.elect.business.konfiguration.entity.MandantKonfigurationWerte;
import de.ivu.elect.business.security.entity.LoginServiceName;
import de.ivu.elect.business.security.entity.PasswordStoreType;
import de.ivu.elect.business.security.entity.TwoFactorAuthenticationStatus;
import de.ivu.elect.business.security.entity.TwoFactorAuthenticationTyp;
import de.ivu.elect.business.system.boundary.SystemName;
import de.ivu.elect.presentation.security.tfa.TfaSupported;


import static java.util.Arrays.asList;

public class LoginServiceKonfigurationInit extends AbstractKonfigurationInit {

  public static final String DEFAULT_LOGIN_SERVICE_PASSWORT_FORMAT_PATTERN = ".{0,}";

  public static final String DEFAULT_LOGIN_SERVICE_PASSWORT_FORMAT_BESCHREIBUNG = "beliebige Zeichen, beliebige Länge";

  public LoginServiceKonfigurationInit() {
    super();
  }

  @Inject
  private transient TfaSupported tfaSupported;

  @Inject
  private transient CurrentCustomer currentCustomer;

  @Inject
  private transient SystemName systemName;

  @Override
  protected Collection<Konfiguration> createDefaultParameters(KonfigurationInitContext konfigurationInitContext) {

    ArrayList<Konfiguration> result = new ArrayList<>(asList( //
        create(MandantKonfigurationWerte.LOGIN_SERVICE_NAME, LoginServiceName.Database.name()), //
        create(MandantKonfigurationWerte.LOGIN_SERVICE_PASSWORT_FORMAT_PATTERN, getDefaultLoginServicePasswortFormatPattern()), //
        create(MandantKonfigurationWerte.LOGIN_SERVICE_PASSWORT_FORMAT_BESCHREIBUNG, getDefaultLoginServicePasswortFormatBeschreibung()), //
        create(MandantKonfigurationWerte.LOGIN_SERVICE_PASSWORD_STORE_TYPE, PasswordStoreType.AKTUELL_BISHER_KLARTEXT) //

    ));

    // WASDE-1586
    if (tfaSupported.isTfaAvailable()) {
      result.add(create(MandantKonfigurationWerte.TWO_FACTOR_AUTHENTICATION_STATUS, getDefaultTwoFactorAuthentificationStatus()));
      result.add(create(MandantKonfigurationWerte.TWO_FACTOR_AUTHENTICATION_TAN, getDefaultTwoFactorAuthentificationTanliste()));
      result.add(create(MandantKonfigurationWerte.TWO_FACTOR_AUTHENTICATION_TOTP, getDefaultTwoFactorAuthentificationTotp()));
      result.add(create(MandantKonfigurationWerte.TWO_FACTOR_AUTHENTICATION_TOTP_ANZAHL_ZEITSCHLITZE_SPIELRAUM, getDefaultTwoFactorAuthentificationTotpSpielraum()));
      result.add(create(MandantKonfigurationWerte.TWO_FACTOR_AUTHENTICATION_TOTP_SYSTEM_NAME, getDefaultTwoFactorAuthentificationTotpSystemName()));
      result.add(create(MandantKonfigurationWerte.TWO_FACTOR_AUTHENTICATION_SECRET_LAENGE, getDefaultTwoFactorAuthentificationSecretLaenge()));
    }

    return result;
  }

  protected String getDefaultTwoFactorAuthentificationTotpSystemName() {
    return "elect iT-" + currentCustomer.getCustomer().getBezeichnung().replaceAll(" ", "") + "@" + systemName.getSystemName();
  }

  protected String getDefaultTwoFactorAuthentificationSecretLaenge() {
    return "32"; // ELECTKOM-332
  }

  private Long getDefaultTwoFactorAuthentificationTotpSpielraum() {
    return 1L;
  }

  protected TwoFactorAuthenticationTyp getDefaultTwoFactorAuthentificationTotp() {
    return TwoFactorAuthenticationTyp.AKTIVIERT_KONFIGURATION_BEI_LOGIN_OPTIONAL;
  }

  protected TwoFactorAuthenticationTyp getDefaultTwoFactorAuthentificationTanliste() {
    return TwoFactorAuthenticationTyp.AKTIVIERT_KONFIGURATION_BEI_LOGIN;
  }

  protected TwoFactorAuthenticationStatus getDefaultTwoFactorAuthentificationStatus() {
    return TwoFactorAuthenticationStatus.DEAKTIVIERT; // WASDE-1586 Erstmal nur für Destatis relevant
  }

  protected String getDefaultLoginServicePasswortFormatBeschreibung() {
    return DEFAULT_LOGIN_SERVICE_PASSWORT_FORMAT_BESCHREIBUNG;
  }

  protected String getDefaultLoginServicePasswortFormatPattern() {
    return DEFAULT_LOGIN_SERVICE_PASSWORT_FORMAT_PATTERN;
  }
}
