package kz.com.aman.kassa.bank;

import android.app.AlertDialog;
import android.widget.EditText;
import android.widget.Toast;

import ru.m4bank.mpos.service.result.Result;
import ru.m4bank.mpos.library.external.authorization.ActivationCallbackHandler;
import ru.m4bank.mpos.service.data.dynamic.objects.ActivationType;
import timber.log.Timber;

public class ActivationCallbackHandlerImpl implements ActivationCallbackHandler {
    private final M4BankActivity activity;

    public ActivationCallbackHandlerImpl(M4BankActivity activity) {
        this.activity = activity;
    }

    @Override
    public void onActivationTypeAndInfoRequested() {
        Timber.i("FIRST STEP STARTED");
        activity.getClientInterface().getAuthorizationManager().sendActivationFirstStepData(ActivationType.EMAIL, "dm@centercorptech.net");
    }

    @Override
    public void onActivationCodeRequested(Integer codeLifetime) {
        Timber.i("FIRST STEP COMPLETED");
        Timber.i("SECOND STEP STARTED");
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("ADD activation code:");

        EditText editext = new EditText(activity);

        builder.setPositiveButton("OK", (dialog, which) -> {
            activity.getClientInterface().getAuthorizationManager().sendActivationCode(editext.getText().toString());
            dialog.dismiss();
        });

        builder.setView(editext);
        activity.runOnUiThread(() -> builder.create().show());
    }

    @Override
    public void onActivationConfirmRequested() {
        Timber.i("SECOND STEP COMPLETED");
        Timber.i("LAST STEP STARTED");
        activity.getClientInterface().getAuthorizationManager().confirmActivation("1234", null);
    }


    @Override
    public void onCompleted(Result result) {
        Timber.i("COMPLETED WITH RESULT - " + result.getResultType());
    }

    @Override
    public void onWrongApiCalled() {
        Timber.i("WRONG API CALLED");
    }

    @Override
    public void onRepeat(int attemptNumber) {
        activity.runOnUiThread(() -> Toast.makeText(activity, "Repeat attempt number " + attemptNumber, Toast.LENGTH_SHORT).show());
    }
}
