package kz.com.aman.kassa.bank;

import java.util.ArrayList;
import java.util.List;
import ru.m4bank.mpos.service.data.dynamic.objects.CurrencyEnum;
import ru.m4bank.mpos.service.data.dynamic.objects.Transaction;
import ru.m4bank.mpos.service.hardware.printer.dto.data.ReconciliationData;
import ru.m4bank.mpos.service.hardware.printer.dto.data.ReconciliationOperationData;
import ru.m4bank.mpos.service.hardware.printer.dto.data.TransactionType;
import ru.m4bank.mpos.service.transactions.data.TransactionMoneyInteractionType;


public class ReconciliationPrinterDataBuilder {

    public ReconciliationData build(List<Transaction> transactionList, boolean closeDay) {
        ArrayList<ReconciliationOperationData> reconciliationOperationDataList = new ArrayList<ReconciliationOperationData>();
        if (transactionList == null) return null;
        for (Transaction transaction : transactionList) {
            if (!isErrorStatus(transaction)) {
                reconciliationOperationDataList.add(buildOneElement(transaction));
            }
        }
        return new ReconciliationData(reconciliationOperationDataList, closeDay);
    }

    private ReconciliationOperationData buildOneElement(Transaction transaction) {
        return new ReconciliationOperationData.Builder(
                transaction.getAmount(),
                transaction.getMaskedPan(),
                CurrencyEnum.getCurrencyEnum(Integer.parseInt(transaction.getCurrency3DigitCode())),
                getPrinterOperationType(transaction.getTransactionType()),
                transaction.getExternalTerminalId(), transaction.getPiDataType().getTypePIData().equals(TransactionMoneyInteractionType.CASH.toString()))
                .cardType(transaction.getCardType())
                .date(transaction.getTransactionDate())
                .checkId(transaction.getCheck())
                .externalMerchantId(transaction.getExternalMerchantId())
                .externalMerchantName(transaction.getExternalMerchantName())
                .externalMerchantAdress(transaction.getExternalMerchantAddress())
                .authorizationCode(transaction.getAuthorizationCode())
                .refNumber(transaction.getRrn())
                .time(transaction.getTransactionTime())
                .build();
    }

    private TransactionType getPrinterOperationType(String type) {
        if (type == null) return null;
        switch (type.toUpperCase()) {
            case "PAYMENT":
                return TransactionType.INCOMING;
            case "REFUND":
                return TransactionType.INCOMING_REFUND;
            case "REVERSAL":
                return TransactionType.REVERSAL;
        }
        return null;
    }

    public static boolean isErrorStatus(Transaction transaction) {
        if (transaction == null || transaction.getOperationStatus() == null) {
            return true;
        }

        switch (transaction.getOperationStatus()) {
            case COMPLETED:
            case REVERSED:
            case READER_TIMEOUT:
                return false;

            default:
                return true;
        }
    }
}
