package kz.com.aman.kassa.bank;

import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ru.m4bank.mpos.library.external.transactions.CashPaymentCallbackHandler;
import ru.m4bank.mpos.service.data.FiscalStatusData;
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.Transaction;
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.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 CashPaymentCallbackHandlerImpl implements CashPaymentCallbackHandler<Result> {

    private final M4BankActivity activity;

    public CashPaymentCallbackHandlerImpl(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() {
        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());

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


    @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() {
        Timber.d("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 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, "notAuthorized", 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) {

    }
}
