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

import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import io.trustbuilder.mfa.android.demoapp.config.TrustBuilderMfaLibHolder;
import io.trustbuilder.mfa.lib.TrustBuilderMfaLib;

public abstract class TrustBuilderActivity extends AppCompatActivity {

    protected TextView result;
    protected Button proceed;
    protected EditText tknAlias;
    protected TrustBuilderMfaLib mfaLib;

    protected static final String ANDROID_KEY_STORE = "AndroidKeyStore";
    protected static final String CLIENT_ID = "TrustBuilderMfaSampleApp";
    protected static final String TAG = "MY_APP_TAG";
    protected static final String BIO_SHARED_PREFS_KEY_NAME = "BioSharedPref";
    protected static final String CANCEL_BUTTON = "Cancel";
    protected static byte[] challengeToSignForBio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mfaLib = TrustBuilderMfaLibHolder.getInstance(this);
    }

    protected void displayException(Exception ex) {
        result.setText(String.format("Unexpected error : %s", ex.getMessage()));
    }

    protected void displayJsonResult(Object res) {
        Gson gson = new Gson();
        result.setText(gson.toJson(res));
    }

    protected boolean checkMandatoryField(EditText field, String name) {
        if (field == null || field.getText() == null ||
                field.getText().toString().trim().isEmpty()){
            result.setText(String.format("Error: %s is empty", name));
            return true;
        }
        return false;
    }

    protected boolean checkMandatoryAndMaxSizeField(EditText field, String name) {
        boolean mandatoryCheck = checkMandatoryField(field, name);
        if (mandatoryCheck) {
            return true;
        }
        if (field.getText().length() > 128 ){
            result.setText(String.format("Error: %s is too long", name));
            return true;
        }
        return false;
    }
}
