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

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.widget.EditText;

import java.nio.charset.StandardCharsets;

import io.trustbuilder.mfa.android.demoapp.R;
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.secretcodechange.CredValues;
import io.trustbuilder.mfa.lib.usecase.definition.secretcodechange.SecretCodeChange;
import io.trustbuilder.mfa.lib.usecase.definition.secretcodechange.SecretCodeChangeParams;
import io.trustbuilder.mfa.lib.usecase.definition.secretcodechange.SecretCodeChangeResult;
import io.trustbuilder.mfa.lib.usecase.definition.secretcodechange.SecretCodeChangeSetupResult;

public class SecretCodeChangeActivity extends TrustBuilderAuthenticationActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: SecretCodeChangeUseCase");
        setTitle(this.getClass().getSimpleName());
        setContentView(R.layout.activity_change_secret_code);
        this.proceed = this.findViewById(R.id.proceed_ChangeSecretCode);
        this.tknAlias = this.findViewById(R.id.tknAlias_ChangeSecretCode);
        this.result = this.findViewById(R.id.result_ChangeSecretCode);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: SecretCodeChangeUseCase");
        this.proceed.setOnClickListener((v) -> {
            if (checkMandatoryField(tknAlias, "tknAlias")) return;
            SecretCodeChangeParams request = new SecretCodeChangeParams(tknAlias.getText().toString());
            try {
                SecretCodeChange secretCodeChange = mfaLib.startSecretCodeChange(request);
                SecretCodeChangeSetupResult setupResult = secretCodeChange.setup();
                if (setupResult.isError())
                    displayJsonResult(setupResult.getError());
                else {
                    CredentialProvider credentialProvider = setupResult.getResponse().getCurrentCredentialProvider();
                    displayCredentials(credentialProvider.getCredType(), credentialProvider.getCredMode(), credentialProvider.getCredProperties().getBioChallenge(), secretCodeChange);
                }
            } catch (Exception e) {
                displayException(e);
            }
        });
    }

    @Override
    protected <T extends IUseCase> void executeUseCase(CredValue cred, T usecase) throws Exception {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Define your secret code");
        final EditText password = new EditText(this);
        password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        builder.setView(password);
        builder.setPositiveButton("Validate", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    CredValue newCredValue = CredValue.builder().value(password.getText().toString().getBytes(StandardCharsets.UTF_8)).credType(cred.getCredType()).build();
                    CredValues credValues = CredValues.builder().currentCredValue(cred).newCredValue(newCredValue).build();
                    SecretCodeChangeResult res = (SecretCodeChangeResult) usecase.execute(credValues);
                    displayJsonResult(res);
                } catch (Exception ex) {
                    displayException(ex);
                }
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();

    }
}
