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

import android.app.AlertDialog;
import android.content.DialogInterface;
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.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.sealing.Sealing;
import io.trustbuilder.mfa.lib.usecase.definition.sealing.SealingParams;
import io.trustbuilder.mfa.lib.usecase.definition.sealing.SealingSetupResult;

public class SealingActivity extends TrustBuilderAuthenticationActivity {
    private EditText sessionId;
    private EditText cnxAlias;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: SealingUseCase");
        setTitle(this.getClass().getSimpleName());
        setContentView(R.layout.activity_sealing);
        this.proceed = this.findViewById(R.id.proceed_Sealing);
        this.tknAlias = this.findViewById(R.id.tknAlias_Sealing);
        this.result = this.findViewById(R.id.result_Sealing);
        this.cnxAlias = this.findViewById(R.id.cnxAlias_Sealing);
        this.sessionId = this.findViewById(R.id.sessionId_Sealing);
        Bundle b = getIntent().getExtras();
        if(b != null) {
            this.tknAlias.setText(b.getString("tknAlias"));
            this.sessionId.setText(b.getString("authSessionId"));
            this.cnxAlias.setText(b.getString("cnxAlias"));
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: SealingUseCase");
        this.proceed.setOnClickListener((v) -> {
            if (checkMandatoryField(tknAlias, "tknAlias")) return;
            if (checkMandatoryField(sessionId, "sessionId")) return;
            if (checkMandatoryField(cnxAlias, "cnxAlias")) return;
            SealingParams request = new SealingParams(tknAlias.getText().toString(), cnxAlias.getText().toString(),
                    sessionId.getText().toString());
            try {
                Sealing sealing = mfaLib.startSealing(request);
                SealingSetupResult setupResult = sealing.setup();
                if (setupResult == null) {
                    displayJsonResult("null");
                } else if (setupResult.isError()) {
                    displayJsonResult(setupResult.getError());
                } else {
                    AlertDialog.Builder builderData = new AlertDialog.Builder(this);
                    builderData.setTitle("SealData");
                    builderData.setMessage(setupResult.getResponse().getContent());
                    builderData.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            CredentialProviderHandler credentialProviderHandler = new CredentialProviderHandler();
                            CredentialProvider credentialProvider = credentialProviderHandler.resolveCredentialProviderFromList(setupResult.getResponse().getCredentialProviders());
                            displayCredentials(credentialProvider.getCredType(), credentialProvider.getCredMode(), credentialProvider.getCredProperties().getBioChallenge(), sealing);
                        }
                    });
                    builderData.setNegativeButton(android.R.string.no, null);
                    builderData.show();
                }
            } 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);
    }
}
