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

import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

import io.trustbuilder.mfa.android.demoapp.R;
import io.trustbuilder.mfa.android.demoapp.utils.CredentialProviderHandler;
import io.trustbuilder.mfa.lib.definition.AuthScope;
import io.trustbuilder.mfa.lib.usecase.IUseCase;
import io.trustbuilder.mfa.lib.usecase.definition.CredValue;
import io.trustbuilder.mfa.lib.usecase.definition.CredentialProvider;
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;

public class InAppAuthenticationActivity extends TrustBuilderAuthenticationActivity {
    private EditText sessionId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: InAppAuthenticationUseCase");
        setTitle(this.getClass().getSimpleName());
        setContentView(R.layout.activity_in_app_authenticate);
        this.proceed = this.findViewById(R.id.proceed_InAppAuthenticate);
        this.tknAlias = this.findViewById(R.id.tknAlias_InAppAuthenticate);
        this.sessionId = this.findViewById(R.id.sessionId_InAppAuthenticate);
        this.result = this.findViewById(R.id.result_InAppAuthenticate);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: InAppAuthenticationUseCase");
        this.proceed.setOnClickListener(v -> {
            if (checkMandatoryField(tknAlias, "tknAlias")) return;
            if (checkMandatoryField(sessionId, "sessionId")) return;
            InAppAuthenticationParams request = new InAppAuthenticationParams(tknAlias.getText().toString(),
                    sessionId.getText().toString(), AuthScope.SESSION);
            try {
                InAppAuthentication inAppAuthentication = mfaLib.startInAppAuthentication(request);
                InAppAuthenticationSetupResult setupResult = inAppAuthentication.setup();
                if (setupResult.isError()) {
                    displayJsonResult(setupResult.getError());
                } else {
                    CredentialProviderHandler credentialProviderHandler = new CredentialProviderHandler();
                    CredentialProvider credentialProvider = credentialProviderHandler.resolveCredentialProviderFromList(setupResult.getResponse());
                    displayCredentials(credentialProvider.getCredType(), credentialProvider.getCredMode(), credentialProvider.getCredProperties().getBioChallenge(), inAppAuthentication);
                }
            } catch (Exception e) {
                displayException(e);
            }
        });
    }

    @Override
    protected <T extends IUseCase> void executeUseCase(CredValue cred, T usecase) throws Exception {
        Object obj = usecase.execute(cred);
        displayJsonResult(obj);
    }
}