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.MfaBoolResponseMfaLockSecureTokenErrorsMfaApiResponse;
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 LockSecureTokenActivity extends TrustBuilderAuthenticationActivity {
    private EditText tknAliasToLock;
    private SecureTokenDTO secureToken;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: LockUserApiUseCase");
        setTitle(this.getClass().getSimpleName());
        setContentView(R.layout.activity_lock_secure_token);
        this.proceed = this.findViewById(R.id.proceed_Lock);
        this.tknAlias = this.findViewById(R.id.tknAlias_Lock);
        this.tknAliasToLock = this.findViewById(R.id.tknAliasToLock_Lock);
        this.result = this.findViewById(R.id.result_Lock);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: LockUserApiUseCase");
        this.proceed.setOnClickListener(v -> {
            if (checkMandatoryAndMaxSizeField(tknAlias, "tknAlias")) return;
            if (checkMandatoryAndMaxSizeField(tknAliasToLock, "tknAliasToLock")) 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;
            if(inAppAuthenticationResult.isError())
            {
                displayJsonResult(inAppAuthenticationResult);
            }
            else {
                UserApiService userApi = new UserApiService();
                MfaBoolResponseMfaLockSecureTokenErrorsMfaApiResponse res = userApi.Lock(this.secureToken, inAppAuthenticationResult.getResponse().getCode(), tknAliasToLock.getText().toString());
                displayJsonResult(res);
            }
        }
}