package com.airlock.iam.saml2.infrastructure.state;

import com.airlock.iam.common.api.infrastructure.ApplicationInitializationHook;
import com.airlock.iam.common.domain.service.state.StateRepository;
import com.airlock.iam.openfedlib.saml2.state.Saml2DataMapProvider;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.inject.Inject;
import jakarta.inject.Provider;

public final class Saml2StateStoreMapsInitializationHook implements ApplicationInitializationHook {
   private final Provider<StateRepository> stateStore;

   @Inject
   public Saml2StateStoreMapsInitializationHook(Provider<StateRepository> stateStore) {
      this.stateStore = stateStore;
   }

   @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
   @Override
   public void onAppInitializationFinished() {
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.IDP_AUTHN_CONTEXT_CACHE,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.authnContextCache", new AuthnContextSetSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.IDP_AUTHN_REQUEST_CACHE,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.authnRequestCache", new AuthnRequestSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.IDP_IDP_AUTHN_CONTEXT_CACHE,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.idpAuthnContextCache", new AuthnContextSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.IDP_IDP_SESSIONS_BY_INDICES,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.idpSessionsByIndices", new IDPSessionSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.IDP_IS_SESSION_UPGRADE_CACHE,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.isSessionUpgradeCache", new IdentitySerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.IDP_OLD_IDP_SESSION_CACHE,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.oldIDPSessionCache", new IDPSessionSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.IDP_RELAY_STATE_CACHE,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.relayStateCache", new IdentitySerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.IDP_RESPONSES_BY_ARTIFACT,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.responsesByArtifacts", new ResponseSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.SP_ASSERTION_BY_ID_CACHE,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "IDPCache.assertionByIdCache", new IdentitySerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.SP_FED_SESSION_LISTS_BY_NAME_ID,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "SPCache.fedSessionListsByNameIDInfoKey", new SPFedSessionListSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.SP_LOGOUT_REQUEST_ID_HASH,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "SPCache.logoutRequestIDHash", new LogoutRequestSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.SP_REQUEST_HASH,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "SPPCache.requestHash", new AuthnRequestSerializer())
      );
      Saml2DataMapProvider.registerDataMap(
         Saml2DataMapProvider.DataMapId.SP_RELAY_STATE_HASH,
         new StateStoreBackedSaml2DataMap<>(this.stateStore, "SPCache.relayStateHash", new IdentitySerializer())
      );
   }
}
