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

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

import io.tb.userapi.model.MfaBoolResponseMfaUnlockSecureTokenErrorsMfaApiResponse;
import io.trustbuilder.mfa.android.demoapp.R;
import io.trustbuilder.mfa.android.demoapp.screen.TrustBuilderAuthenticationActivity;
import io.trustbuilder.mfa.android.demoapp.user_api.UserApiService;
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.IUseCase;
import io.trustbuilder.mfa.lib.usecase.definition.CredValue;
import io.trustbuilder.mfa.lib.usecase.definition.inappauthentication.InAppAuthenticationResult;

public class UnlockSecureTokenActivity extends TrustBuilderAuthenticationActivity {
    private EditText tknAliasToUnLock;
    private SecureTokenDTO secureToken;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: UnLockUserApiUseCase");
        setTitle(this.getClass().getSimpleName());
        setContentView(R.layout.activity_unlock_secure_token);
        this.proceed = this.findViewById(R.id.proceed_UnLock);
        this.tknAlias = this.findViewById(R.id.tknAlias_UnLock);
        this.tknAliasToUnLock = this.findViewById(R.id.tnkAliasToUnLock_UnLock);
        this.result = this.findViewById(R.id.result_UnLock);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: UnLockUserApiUseCase");
        this.proceed.setOnClickListener(v -> {
            if (checkMandatoryAndMaxSizeField(tknAlias, "tknAlias")) return;
            if (checkMandatoryAndMaxSizeField(tknAliasToUnLock, "tknAliasToUnLock")) return;
            Failable<SelfCareAuthorizeResponse, ErrorCode> failable = SelfCareAuthorizeHelper.authorizeInAppAuthentication(this.mfaLib, tknAlias.getText().toString());
            if (failable.isError()) {
                displayJsonResult(failable.getError());
            } else {
                SelfCareAuthorizeResponse authorizeResponse = failable.getResponse();
                displayCredentials(authorizeResponse.getCredentialProvider().getCredType(), authorizeResponse.getCredentialProvider().getCredMode(), authorizeResponse.getCredentialProvider().getCredProperties().getBioChallenge(), authorizeResponse.getInAppAuthentication());
                this.secureToken = authorizeResponse.getSecureToken();
            }
        });
    }

    @Override
    protected <T extends IUseCase> void executeUseCase(CredValue cred, T usecase) throws Exception {
        Object obj = usecase.execute(cred);
        InAppAuthenticationResult inAppAuthenticationResult = (InAppAuthenticationResult)obj;
        UserApiService userApi = new UserApiService();
        MfaBoolResponseMfaUnlockSecureTokenErrorsMfaApiResponse res = userApi.UnLock(secureToken, inAppAuthenticationResult.getResponse().getCode(), tknAliasToUnLock.getText().toString());
        displayJsonResult(res);
    }
}
