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.LoadTerminalKeysCallbackHandler;
import ru.m4bank.mpos.service.data.dynamic.objects.Reader;
import ru.m4bank.mpos.service.data.dynamic.objects.result.ResultCode;
import ru.m4bank.mpos.service.hardware.network.external.TerminalKeyType;
import ru.m4bank.mpos.service.result.Result;
import timber.log.Timber;

class LoadTerminalKeysCallbackHandlerImpl implements LoadTerminalKeysCallbackHandler<Result> {

    private final M4BankActivity activity;

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

    @Override
    public void notAuthorized() {
        Timber.d("Not authorized");
        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 onKeyLoad(TerminalKeyType terminalKeyType, String description, ResultCode resultCode) {
        Timber.d("key loaded".concat(terminalKeyType.name()));
    }

    @Override
    public void onKeyReceived(TerminalKeyType terminalKeyType, String description, ResultCode resultCode) {
        Timber.d("key loaded".concat(terminalKeyType.name()));
    }

    @Override
    public void onError(String description, ResultCode resultCode) {
        Timber.d("key loaded" + " onError");
    }

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

    @Override
    public void onCardReaderForTransactionGettingCompleted() {
        Timber.d("Reader has been received");
    }

    @Override
    public void onDeviceToConnectSelectionRequested(List<String> deviceList) {
        activity.runOnUiThread(() -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("Choose device");

            Spinner deviceListSpinner = new Spinner(activity);
            ArrayAdapter<String> adapter = new ArrayAdapter<>(activity, android.R.layout.simple_spinner_item, deviceList);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            deviceListSpinner.setAdapter(adapter);

            builder.setPositiveButton("OK", (dialog, which) -> {
                activity.getClientInterface()
                        .getCardReaderManager()
                        .selectCardReader(deviceListSpinner.getSelectedItem().toString());
                dialog.dismiss();
            });

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

    @Override
    public void onDeviceToUseSelectionRequested(List<Reader> deviceList) {
        activity.runOnUiThread(() -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("Choose device");

            Spinner deviceListSpinner = new Spinner(activity);
            ArrayAdapter<Reader> adapter = new ArrayAdapter<>(activity, android.R.layout.simple_spinner_item, new ArrayList<>(deviceList));
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            deviceListSpinner.setAdapter(adapter);

            builder.setPositiveButton("OK", (dialog, which) -> {
                activity.getClientInterface()
                        .getCardReaderManager()
                        .setCardReaderToUse((Reader) deviceListSpinner.getSelectedItem());
                dialog.dismiss();
            });

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

    @Override
    public void onNoBoundedDevicesFound() {
        Timber.d("No bounded readers");
    }

    @Override
    public void onConnectingProcessStarted(String deviceName) {
        Timber.d("onConnectingProcessStarted");
    }

    @Override
    public void onDisconnectedEventReceivedOrCanNotEstablishConnection(boolean reconnectAvailable, String description) {
        if (reconnectAvailable) {
            activity.runOnUiThread(() -> {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setMessage("Connection to cardreader has been lost, try to reconnect and repeat last operation?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", (dialog, which) -> activity.getClientInterface().tryToReconnectAndRepeatLast());
                builder.setNegativeButton("No", (dialog, which) -> activity.getClientInterface().cancel());
                builder.create().show();
            });
        }
    }
}
