package com.airlock.iam.flow.shared.infrastructure.factory;

import com.airlock.iam.base.api.infrastructure.plugin.ModuleForConfig;
import com.airlock.iam.common.api.domain.model.authmethod.AuthenticationMethod;
import com.airlock.iam.core.misc.impl.authen.AuthenticationMethodFactory;
import com.airlock.iam.core.misc.impl.authen.MatrixAuthenticator;
import com.airlock.iam.core.misc.impl.authen.MatrixcardAuthenticator;
import com.airlock.iam.flow.api.domain.model.session.FlowSession;
import com.airlock.iam.flow.infrastructure.step.StepSessionModuleHelper;
import com.airlock.iam.flow.shared.application.configuration.matrix.CommonMatrixCheckingStepConfig;
import com.airlock.iam.flow.shared.domain.model.matrix.MatrixCheckingStepSession;
import com.google.inject.Exposed;
import com.google.inject.PrivateModule;
import com.google.inject.Provides;

@ModuleForConfig(CommonMatrixCheckingStepConfig.class)
public final class CommonMatrixStepModule extends PrivateModule {
   @Exposed
   @Provides
   MatrixAuthenticator provideMatrixAuthenticator(CommonMatrixCheckingStepConfig config) {
      return MatrixcardAuthenticator.tanChallengeAuthenticator(config.getTanListType().getTanChallengeAuthenticatorType())
         .maxRetries(config.getMaxRetries())
         .maxUnansweredChallenges(config.getMaxUnansweredChallenges())
         .newChallengeOnRetry(config.isNewChallengeOnRetry())
         .responseValidityMillis(config.getResponseValidityMillis() == null ? 0 : config.getResponseValidityMillis())
         .startIndex(config.getStartIndex())
         .tanService(config.getTanService())
         .tokenListRenderer(config.getTokenListRenderer())
         .countUnansweredChallenges(config.isCountUnansweredChallenges())
         .unansweredChallengeTimeout(config.getUnansweredChallengeTimeout())
         .build();
   }

   @Exposed
   @Provides
   AuthenticationMethod provideAuthenticationMethod(CommonMatrixCheckingStepConfig config) {
      return AuthenticationMethodFactory.authMethodFor(config.getAuthenticationMethodId());
   }

   @Exposed
   @Provides
   MatrixCheckingStepSession provideMatrixAuthStepSession(FlowSession flowSession) {
      return StepSessionModuleHelper.retrieveStepSession(flowSession, MatrixCheckingStepSession.class);
   }

   protected void configure() {
   }
}
