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.EasyAlipayRefundCallbackHandler;
import ru.m4bank.mpos.library.external.transactions.GetTransactionsListCallbackHandler;
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.Transaction;
import ru.m4bank.mpos.service.data.dynamic.objects.transaction.RefundType;
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;

public class EasyAlipayRefundCallbackHandlerImpl implements EasyAlipayRefundCallbackHandler<Result>, GetTransactionsListCallbackHandler<Result> {

    private final M4BankActivity activity;
    private Transaction transaction;

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

    @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() {

    }

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

    @Override
    public void onError(ErrorHandler errorHandler, TransactionErrorData transactionErrorData) {
        activity.runOnUiThread(() -> Toast.makeText(activity, errorHandler.getFinalDescription(), Toast.LENGTH_LONG).show());
    }

    @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 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 onRefundDataRequested() {
        activity.getClientInterface().getTransactionManager().setRefundData(transaction, transaction.getAmount(), RefundType.General);
    }

    @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) transactionListSpinner.getSelectedItem();
            activity.getClientInterface().getTransactionManager().makeAlipayRefund(this);
            dialog.dismiss();
        });

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

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

    @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<PrinterErrorData, PrinterBaseData> printerErrorDataPrinterBaseDataPrinterResult) {

    }

    @Override
    public void onStatusFiscalModule(FiscalStatusData fiscalStatusData) {

    }

    @Override
    public void onPrinterForRegistrationGettingCompleted() {

    }

    @Override
    public void onNoBoundedPrinterFound() {

    }

    @Override
    public void onPrinterToUseSelectionRequested(List<String> printerList) {

    }
}
