package kz.com.aman.kassa.handler;

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 kz.com.aman.kassa.bank.M4BankActivity;
import kz.com.aman.kassa.plugins.BankNfcPlugins;
import ru.m4bank.mpos.library.external.transactions.GetTransactionDetailsCallbackHandler;
import ru.m4bank.mpos.library.external.transactions.GetTransactionsListCallbackHandler;
import ru.m4bank.mpos.service.data.dynamic.objects.Transaction;
import ru.m4bank.mpos.service.result.Result;

public class TransactionDetailsHandlerImpl implements GetTransactionDetailsCallbackHandler<Result>, GetTransactionsListCallbackHandler<Result> {
    private final BankNfcPlugins plugin;

    public TransactionDetailsHandlerImpl(BankNfcPlugins plugin) {
        this.plugin = plugin;
    }

    @Override
    public void onTransactionDetailsReceived(Transaction transaction) {
        plugin.getActivity().runOnUiThread(() -> {
            System.out.println("getCheck:"+ transaction.getCheck());
            System.out.println("getCheckNumber:"+ transaction.getCheckNumber());
            Toast.makeText(plugin.getActivity(), "Transaction details received: " + transaction, Toast.LENGTH_SHORT)
                    .show();
        });

    }

    @Override
    public void onSuccessStatusTransaction(Transaction transaction) {

    }

    @Override
    public void onErrorStatusTransaction() {

    }

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

            Spinner transactionListSpinner = new Spinner(plugin.getActivity());
            ArrayAdapter<Transaction> adapter = new ArrayAdapter<>(plugin.getActivity(), 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();
                plugin.getClientInterface()
                        .getTransactionManager()
                        .getTransactionDetails(this, transaction.getOperationalDayNumber(), transaction
                                .getTransactionNumber(), transaction.getMobileTerminalId());
                dialog.dismiss();
            });

            builder.setView(transactionListSpinner);
            builder.create().show();
        });

    }

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

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

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

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