package kz.com.aman.kassa.bank;

import android.app.AlertDialog;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ru.m4bank.mpos.library.external.transactions.GetTransactionsListCallbackHandler;
import ru.m4bank.mpos.library.external.transactions.ResendReceiptCallbackHandler;
import ru.m4bank.mpos.service.data.dynamic.objects.Transaction;
import ru.m4bank.mpos.service.result.Result;

class ResendReceiptCallbackHandlerImpl implements ResendReceiptCallbackHandler<Result>,
        GetTransactionsListCallbackHandler<Result> {
    private final M4BankActivity activity;

    ResendReceiptCallbackHandlerImpl(M4BankActivity activity) {
        this.activity = activity;
    }

    @Override
    public void notAuthorized() {
        activity.runOnUiThread(() -> Toast.makeText(activity, "Not authorized", Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onCompleted(Result result) {
        activity.runOnUiThread(() -> Toast.makeText(activity, result.getResultType() + " " + (result.getDescription() == null ? "" : result.getDescription()), Toast.LENGTH_SHORT)
                .show());
    }

    @Override
    public void onWrongApiCalled() {
        activity.runOnUiThread(() -> Toast.makeText(activity, "Wrong method has been called", Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onTransactionsListReceived(List<Transaction> transactionList) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("Choose transaction");

        Spinner transactionListSpinner = new Spinner(activity);
        ArrayAdapter<Transaction> adapter = new ArrayAdapter<>(activity, android.R.layout.simple_spinner_item, new ArrayList<>(transactionList));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        transactionListSpinner.setAdapter(adapter);

        builder.setPositiveButton("OK", (dialog, which) -> {
            Transaction transaction = (Transaction) transactionListSpinner.getSelectedItem();
            activity.getClientInterface().getTransactionManager().resendReceipt(this, transaction, "vsa@centercorptech.net", null);
            dialog.dismiss();
        });

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

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