package io.trustbuilder.mfa.android.demoapp.screen.selfcare;


import java.util.Optional;

import io.trustbuilder.mfa.lib.TrustBuilderMfaLib;
import io.trustbuilder.mfa.lib.definition.AuthScope;
import io.trustbuilder.mfa.lib.definition.AuthorizeScope;
import io.trustbuilder.mfa.lib.definition.ErrorCode;
import io.trustbuilder.mfa.lib.definition.Failable;
import io.trustbuilder.mfa.lib.definition.SecureTokenDTO;
import io.trustbuilder.mfa.lib.usecase.definition.CredentialProvider;
import io.trustbuilder.mfa.lib.usecase.definition.authorizerequest.AuthorizeRequest;
import io.trustbuilder.mfa.lib.usecase.definition.authorizerequest.AuthorizeRequestErrors;
import io.trustbuilder.mfa.lib.usecase.definition.authorizerequest.AuthorizeRequestParams;
import io.trustbuilder.mfa.lib.usecase.definition.authorizerequest.AuthorizeRequestResult;
import io.trustbuilder.mfa.lib.usecase.definition.inappauthentication.InAppAuthentication;
import io.trustbuilder.mfa.lib.usecase.definition.inappauthentication.InAppAuthenticationParams;
import io.trustbuilder.mfa.lib.usecase.definition.inappauthentication.InAppAuthenticationSetupResult;
import io.trustbuilder.mfa.lib.usecase.definition.securetokenlisting.SecureTokenListing;
import io.trustbuilder.mfa.lib.usecase.definition.securetokenlisting.SecureTokenListingResult;

public class SelfCareAuthorizeHelper {

    private SelfCareAuthorizeHelper(){
    }

    public static Failable<SelfCareAuthorizeResponse, ErrorCode> authorizeInAppAuthentication(TrustBuilderMfaLib mfaLib, String tknAlias ){
        SecureTokenListing secureTokenListing = mfaLib.startSecureTokenListing();
        SecureTokenListingResult resExecute  = secureTokenListing.execute();
        Optional<SecureTokenDTO> secureToken = resExecute.getResponse().stream().filter(st -> st.getTknAlias().equals(tknAlias)).findFirst();
        if(secureToken.isEmpty()){
            return Failable.fail(AuthorizeRequestErrors.LOCAL_TKN_NOT_FOUND, "Secure token not found from local storage");
        }
        AuthorizeRequestParams requestAuth = new AuthorizeRequestParams(tknAlias,
                null , AuthorizeScope.SESSION);
        AuthorizeRequest authorizeRequest = mfaLib.startAuthorizeRequest(requestAuth);
        AuthorizeRequestResult executeAuth = authorizeRequest.execute();
        if (executeAuth.isError()) {
            return Failable.fail( executeAuth.getError().getErrorCode() ,executeAuth.getError().getErrorMessage());
        } else {
            InAppAuthenticationParams requestInApp = new InAppAuthenticationParams(tknAlias,
                executeAuth.getResponse().getSessionId(), AuthScope.SESSION);
            InAppAuthentication inAppAuthentication = mfaLib.startInAppAuthentication(requestInApp);
            InAppAuthenticationSetupResult setupResult = inAppAuthentication.setup();
            if (setupResult.isError()) {
                return  Failable.fail( setupResult.getError().getErrorCode(),setupResult.getError().getErrorMessage()) ;
            } else {
                CredentialProvider credentialProvider = setupResult.getResponse().get(0);
                SelfCareAuthorizeResponse authorizeResponse = new SelfCareAuthorizeResponse(secureToken.get(), credentialProvider, inAppAuthentication);
                return Failable.ok(authorizeResponse);
            }
        }
    }
}
