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.HashMap;
import java.util.List;

import kz.com.aman.kassa.bank.gui.CustomKeyBoardDilalog;
import ru.m4bank.mpos.library.external.transactions.CardPaymentCallbackHandler;
import ru.m4bank.mpos.service.commons.data.ResultType;
import ru.m4bank.mpos.service.data.FiscalStatusData;
import ru.m4bank.mpos.service.data.PrinterBaseData;
import ru.m4bank.mpos.service.data.PrinterErrorData;
import ru.m4bank.mpos.service.data.dynamic.objects.AdditionalTransactionData;
import ru.m4bank.mpos.service.data.dynamic.objects.GoodsData;
import ru.m4bank.mpos.service.data.dynamic.objects.Reader;
import ru.m4bank.mpos.service.data.dynamic.objects.Transaction;
import ru.m4bank.mpos.service.data.dynamic.objects.VendorData;
import ru.m4bank.mpos.service.data.dynamic.objects.printer.enums.AccountingSubject;
import ru.m4bank.mpos.service.data.dynamic.objects.printer.enums.TaxRate;
import ru.m4bank.mpos.service.data.dynamic.objects.result.ResultCode;
import ru.m4bank.mpos.service.hardware.error.AllError;
import ru.m4bank.mpos.service.hardware.error.ErrorHandler;
import ru.m4bank.mpos.service.hardware.external.cardreaderlib.data.ApplicationIdConv;
import ru.m4bank.mpos.service.hardware.external.cardreaderlib.data.enums.TransactionTypeConv;
import ru.m4bank.mpos.service.hardware.external.cardreaderlib.data.enums.VirtualKeyboardStyleTypeConv;
import ru.m4bank.mpos.service.result.PrinterResult;
import ru.m4bank.mpos.service.result.Result;
import ru.m4bank.mpos.service.transactions.data.ButtonKeyboardDto;
import ru.m4bank.mpos.service.transactions.data.MessageButtonData;
import ru.m4bank.mpos.service.transactions.data.TransactionData;
import ru.m4bank.mpos.service.transactions.data.TransactionExecutionStatus;
import ru.m4bank.mpos.service.transactions.data.VirtualPinKeyboardData;
import ru.m4bank.mpos.service.transactions.data.error.TransactionErrorData;
import ru.m4bank.mpos.service.transactions.dto.OnlineCardDataDto;
import timber.log.Timber;

public class CardPaymentCallbackHandlerImpl implements CardPaymentCallbackHandler<Result> {
    private final M4BankActivity activity;
    private CustomKeyBoardDilalog customKeyBoardDilalog;
    private TransactionTypeConv transactionTypeConv;

    public CardPaymentCallbackHandlerImpl(M4BankActivity activity) {
        this.activity = activity;
        customKeyBoardDilalog = new CustomKeyBoardDilalog();
        transactionTypeConv = TransactionTypeConv.UNKNOWN;
    }

    public CardPaymentCallbackHandlerImpl setTransactionTypeConv(TransactionTypeConv transactionTypeConv) {
        this.transactionTypeConv = transactionTypeConv;
        return this;
    }

    @Override
    public void onDeviceToConnectSelectionRequested(List<String> deviceList) {
        Timber.d("onDeviceToConnectSelectionRequested");
        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()
                        .getTransactionManager()
                        .selectCardReader(deviceListSpinner.getSelectedItem().toString());
                dialog.dismiss();
            });

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

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

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

    @Override
    public void onDeviceToUseSelectionRequested(List<Reader> deviceList) {
        Timber.d("onDeviceToUseSelectionRequested");
        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()
                        .getTransactionManager()
                        .setCardReaderToUse((Reader) deviceListSpinner.getSelectedItem());
                dialog.dismiss();
            });

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

    @Override
    public void onTransactionAmountRequested() {
        Timber.d("onTransactionAmountRequested");
        activity.getClientInterface().getTransactionManager().setTransactionAmount(activity.getAmount());
    }

    @Override
    public void onTransactionDataRequested() {
        Timber.d("onTransactionDataRequested");
        List<GoodsData> goodsDataList = new ArrayList<>();
        goodsDataList.add(new GoodsData.Builder()
                .setOriginalPrice(100000L)
                .setRealPrice(100000L)
                .setName("Принтер")
                .setQuantity(1)
                .setCode("000001")
                .setUnits("00")
                .setDiscount(0L)
                .setTaxRate(TaxRate.Tax_18)
                .setExponent(2)
                .setAccountingSubject(AccountingSubject.PRODUCT)
                .build());
        goodsDataList.add(new GoodsData.Builder()
                .setOriginalPrice(30065L)
                .setRealPrice(30065L)
                .setName("Услуга")
                .setQuantity(3)
                .setCode("000002")
                .setUnits("00")
                .setDiscount(0L)
                .setTaxRate(TaxRate.Tax_18)
                .setExponent(2)
                .setAccountingSubject(AccountingSubject.PRODUCT)
                .build());

        HashMap<String, String> vendorDescription = new HashMap<>();
        vendorDescription.put("vendor", "description");
        vendorDescription.put("vendor1", "description1");
        vendorDescription.put("vendor2", "description2");
        vendorDescription.put("vendor3", "description3");
        VendorData vendorData = new VendorData(vendorDescription);

        AdditionalTransactionData additionalTransactionData = new AdditionalTransactionData.Builder(goodsDataList).vendorData(vendorData).build();
        activity.getClientInterface().getTransactionManager().setAdditionalTransactionData(additionalTransactionData);
    }

    @Override
    public void onWorkFlowDataRequested() {
        Timber.d("WorkFlow requested");
    }

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

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

    @Override
    public void onReceiveCardData(OnlineCardDataDto data, boolean shouldConfirmBeManuallyCalled) {
        Timber.d("onReceiveCardData");
        if (shouldConfirmBeManuallyCalled) {
            activity.getClientInterface().getTransactionManager().processOnlineTransaction(false);
        }
    }

    @Override
    public void onTransactionInit() {
        //Impossible
        activity.getClientInterface().getTransactionManager().processOnlineTransaction(false);
    }

    @Override
    public void onTransactionExecutionStatusChanged(TransactionExecutionStatus newStatus) {
        Timber.d("onTransactionExecutionStatusChanged: " + newStatus.name());
        activity.runOnUiThread(() -> Toast.makeText(activity, newStatus.name(), Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onError(ErrorHandler errorHandler, TransactionErrorData transactionErrorData) {
        if (transactionErrorData.getError() != null) {
            activity.runOnUiThread(() -> Toast.makeText(activity, transactionErrorData.getError().getDescription(), Toast.LENGTH_LONG).show());
        } else {
            Timber.d("onError: %s, description: %s", ((AllError) errorHandler).name(), errorHandler.getDescription());
            activity.runOnUiThread(() -> Toast.makeText(activity, String.format("onError: %s, description: %s", ((AllError) errorHandler).name(), errorHandler.getFinalDescription()), Toast.LENGTH_LONG).show());
        }
    }

    @Override
    public void onErrorWithPossibilityToRetry(ErrorHandler errorHandler) {
        Timber.d("onErrorWithPossibilityToRetry: %s, description: %s", ((AllError) errorHandler).name(), errorHandler.getDescription());
        activity.runOnUiThread(() -> Toast.makeText(activity, String.format("onError: %s, description: %s", ((AllError) errorHandler).name(), errorHandler.getFinalDescription()), Toast.LENGTH_LONG).show());
        activity.getClientInterface().getTransactionManager().tryToRepeatExecuteLastRequestInTransaction();
    }

    @Override
    public void onTransactionDataReceivedError(TransactionData data) {
        Timber.d("onTransactionDataReceivedError");
    }

    @Override
    public void onUserInformationRequested(boolean signNeeded) {
        Timber.d("onUserInformationRequested");
        if (transactionTypeConv != TransactionTypeConv.CANCEL) {
            activity.getClientInterface()
                    .getTransactionManager()
                    .sendTransactionUserData("signature", "phone", "vsa@centercorptech.net");
        } else {
            onTransactionCompleted();
        }
    }

    @Override
    public void onTransactionDataReceived(TransactionData data) {
        Timber.d("onTransactionDataReceived");
        activity.runOnUiThread(() -> Toast.makeText(activity, "Transaction data has been received", Toast.LENGTH_LONG)
                .show());
    }

    @Override
    public void onTransactionCompleted() {
        Timber.d("onTransactionCompleted");
        activity.runOnUiThread(() -> Toast.makeText(activity, "Operation has been successfully completed", Toast.LENGTH_LONG)
                .show());
    }

    @Override
    public void onRequiredHostAddress() {
        Timber.d("onRequiredHostAddress");
        activity.runOnUiThread(() -> Toast.makeText(activity, "Set Host Address", Toast.LENGTH_LONG)
                .show());
        activity.getClientInterface().getTransactionManager().setHostAddress("213.79.122.128", "8011");
    }

    @Override
    public void onDisconnectedEventReceivedOrCanNotEstablishConnection(boolean reconnectAvailable, String description) {
        Timber.d("onDisconnectedEventReceivedOrCanNotEstablishConnection");
        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();
            });
        }
    }

    @Override
    public void onConnectingProcessStarted(String deviceName) {
        Timber.d("Connecting process started: %s", deviceName);
    }

    @Override
    public void onReconnectNeededToProcess() {
        Timber.d("onReconnectNeededToProcess");
        activity.runOnUiThread(() -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage("Connection to cardreader has been lost, try to reconnect and continue?");
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", (dialog, which) -> activity.getClientInterface().getTransactionManager().tryToReconnectToCardReader());
            builder.setNegativeButton("No", (dialog, which) -> activity.getClientInterface().cancel());
            builder.create().show();
        });
    }

    @Override
    public void onReconciliationRequiredToProcess() {
        Timber.d("onReconciliationRequiredToProcess");
        Timber.d("Reconciliation required!!!");
        activity.getClientInterface().getTransactionManager().makeReconciliationDuringTransaction();
    }

    @Override
    public void onReconciliationCompleted(Result result) {
        Timber.d("onReconciliationCompleted");
        Timber.d("Reconciliation completed!!!");
        if (result.getResultType() == ResultType.SUCCESSFUL) {
            activity.getClientInterface().getTransactionManager().continueTransactionAfterReconciliation();
        } else {
            activity.runOnUiThread(() -> Toast.makeText(activity, "Reconciliation failed " + result.getDescription(), Toast.LENGTH_LONG)
                    .show());
        }
    }

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

    @Override
    public void onTransactionDetailsReceived(Transaction transaction) {
        Timber.d("onTransactionDetailsReceived");
        activity.runOnUiThread(() -> Toast.makeText(activity, "Transaction details received", Toast.LENGTH_LONG)
                .show());
    }

    @Override
    public void onSuccessStatusTransaction(Transaction transaction) {
        Timber.d("onSuccessStatusTransaction");
    }

    @Override
    public void onErrorStatusTransaction() {
        Timber.d("onErrorStatusTransaction");
    }

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

    @Override
    public void onRequestedReportZ() {
        Timber.d("onRequestedReportZ");
    }

    @Override
    public void onCompletedPrinting(PrinterResult<PrinterErrorData, PrinterBaseData> printerErrorDataPrinterBaseDataPrinterResult) {
        Timber.d("onCompletedPrinting");
    }

    @Override
    public void onStatusFiscalModule(FiscalStatusData fiscalStatusData) {
        Timber.d("onStatusFiscalModule");
    }

    @Override
    public void onPrinterForRegistrationGettingCompleted() {
        Timber.d("onPrinterForRegistrationGettingCompleted");
    }

    @Override
    public void onNoBoundedPrinterFound() {
        Timber.d("onNoBoundedPrinterFound");

    }

    @Override
    public void onPrinterToUseSelectionRequested(List<String> printerList) {
        Timber.d("onPrinterToUseSelectionRequested");
    }

    @Override
    public void onRequiredReversal(String description, ResultCode resultCode) {
        Timber.d("onRequiredReversal: %s, resultCode: %s", description, resultCode.name());
        activity.runOnUiThread(() -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage("Revert last of operation?");
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", (dialog, which) -> activity.getClientInterface().getTransactionManager().revertCurrentOperation(new RevertCurrentOperationCallbackHandlerImpl(activity)));
            builder.setNegativeButton("No", (dialog, which) -> activity.getClientInterface().getTransactionManager().clearLastTransactionInformation());
            builder.create().show();
        });
    }

    @Override
    public void onRequiredApplicationSelection(List<ApplicationIdConv> list) {
        Timber.d("onRequiredApplicationSelection");
        activity.runOnUiThread(() -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("Choose application identifier");

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

            builder.setPositiveButton("OK", (dialog, which) -> {
                activity.getClientInterface()
                        .getTransactionManager()
                        .setTransactionApplicationIdentifier((ApplicationIdConv) deviceListSpinner.getSelectedItem());
                dialog.dismiss();
            });

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


    @Override
    public void onActivityUpdateUiThread() {
        Timber.d("onActivityUpdateUiThread");
        activity.getClientInterface().getTransactionManager().addPinpadClickListener(activity);
    }

    @Override
    public void onCreatePinPadButtons() {
        Timber.d("onCreatePinPadButtons");
        customKeyBoardDilalog.createDialog(activity);
        customKeyBoardDilalog.show(activity);
        ButtonKeyboardDto buttonKeyboardDto = new ButtonKeyboardDto.Build(
                customKeyBoardDilalog.getBtnb1(),
                customKeyBoardDilalog.getBtnb2(),
                customKeyBoardDilalog.getBtnb3(),
                customKeyBoardDilalog.getBtnb4(),
                customKeyBoardDilalog.getBtnb5(),
                customKeyBoardDilalog.getBtnb6(),
                customKeyBoardDilalog.getBtnb7(),
                customKeyBoardDilalog.getBtnb8(),
                customKeyBoardDilalog.getBtnb9(),
                customKeyBoardDilalog.getBtnb0(),
                customKeyBoardDilalog.getBtncancel(),
                customKeyBoardDilalog.getBtnconfirm(),
                customKeyBoardDilalog.getBtnclean(),
                activity.getWindowManager().getDefaultDisplay().getRotation()
        ).build();
        activity.getClientInterface().getTransactionManager().addPinPadButtons(buttonKeyboardDto);
    }

    @Override
    public void onShowPinPadKeyBoard(MessageButtonData messageButtonData) {
        Timber.d("onShowPinPadKeyBoard");
        customKeyBoardDilalog.setText(messageButtonData);
    }

    @Override
    public void onUpdateElementPin(int count) {
        Timber.d("onUpdateElementPin");
        activity.runOnUiThread(() -> Toast.makeText(activity, "count = " + count, Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onCloseKeyboard() {
        Timber.d("onCloseKeyboard");
        customKeyBoardDilalog.close(activity);
    }

    @Override
    public void onRequiredAdditionalVirtualKeyboardSettings(VirtualKeyboardStyleTypeConv virtualKeyboardStyleTypeConv) {
        activity.showSelectStyleDialog(additionalData ->
                activity.getClientInterface().getTransactionManager().addVirtualPinKeyboardData(additionalData));
    }

    public interface SelectStyleListener {
        void onSelectStyle(VirtualPinKeyboardData virtualPinKeyboardData);
    }
}
