package kz.com.aman.kassa.bank;

import android.widget.Toast;

import java.util.List;

import ru.m4bank.mpos.library.external.transactions.EcomPaymentCallbackHandler;
import ru.m4bank.mpos.service.data.FiscalStatusData;
import ru.m4bank.mpos.service.data.dynamic.objects.AdditionalTransactionData;
import ru.m4bank.mpos.service.data.dynamic.objects.EmailStatus;
import ru.m4bank.mpos.service.data.dynamic.objects.Transaction;
import ru.m4bank.mpos.service.hardware.error.ErrorHandler;
import ru.m4bank.mpos.service.result.PrinterResult;
import ru.m4bank.mpos.service.result.Result;
import ru.m4bank.mpos.service.transactions.data.TransactionData;
import ru.m4bank.mpos.service.transactions.data.TransactionExecutionStatus;
import ru.m4bank.mpos.service.transactions.data.error.TransactionErrorData;
import timber.log.Timber;

public class EcomPaymentCallbackHandlerImpl implements EcomPaymentCallbackHandler<Result> {

    private final M4BankActivity activity;

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

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

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

    @Override
    public void onTransactionDataRequested() {
        activity.getClientInterface().getTransactionManager().setAdditionalTransactionData(new AdditionalTransactionData.Builder(null).build());
    }


    @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 onTransactionInit() {
        activity.getClientInterface().getTransactionManager().processOnlineTransaction(false);
    }

    @Override
    public void onTransactionExecutionStatusChanged(TransactionExecutionStatus newStatus) {

    }

    @Override
    public void onError(ErrorHandler errorHandler, TransactionErrorData transactionErrorData) {

    }

    @Override
    public void onErrorWithPossibilityToRetry(ErrorHandler errorHandler) {
        activity.runOnUiThread(() -> Toast.makeText(activity, errorHandler.getFinalDescription(), Toast.LENGTH_LONG).show());
        activity.getClientInterface().getTransactionManager().tryToRepeatExecuteLastRequestInTransaction();
    }

    @Override
    public void onTransactionDataReceivedError(TransactionData data) {

    }

    @Override
    public void onUserInformationRequested(boolean signNeeded) {
        activity.getClientInterface()
                .getTransactionManager()
                .sendTransactionUserData("signature", "phone", "email");
    }

    @Override
    public void onFormUrlRequested(String formUrl) {

        activity.getClientInterface()
                .getTransactionManager()
                .sendEcomEmail("qrCode", null, "ap@m4bank.com", EmailStatus.SEND);

        activity.runOnUiThread(() -> Toast.makeText(activity, "onFormUrlRequested " + formUrl, Toast.LENGTH_LONG)
                .show());

    }

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

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

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

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

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

    @Override
    public void onSuccessStatusTransaction(Transaction transaction) {

    }

    @Override
    public void onErrorStatusTransaction() {

    }

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

    @Override
    public void onRequestedReportZ() {

    }

    @Override
    public void onCompletedPrinting(PrinterResult printerResult) {

    }

    @Override
    public void onStatusFiscalModule(FiscalStatusData fiscalStatusData) {

    }

    @Override
    public void onPrinterForRegistrationGettingCompleted() {

    }

    @Override
    public void onNoBoundedPrinterFound() {

    }

    @Override
    public void onPrinterToUseSelectionRequested(List printerList) {

    }
}
