package kz.com.aman.kassa.bank;

import java.util.Calendar;
import java.util.Date;

import ru.m4bank.mpos.service.commons.Lists;
import ru.m4bank.mpos.service.data.dynamic.objects.CurrencyEnum;
import ru.m4bank.mpos.service.data.dynamic.objects.ExternalVerificationType;
import ru.m4bank.mpos.service.data.dynamic.objects.GoodsData;
import ru.m4bank.mpos.service.data.dynamic.objects.ProductsAndTransactionData;
import ru.m4bank.mpos.service.data.dynamic.objects.TransactionStatus;
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.printer.enums.TaxSystem;
import ru.m4bank.mpos.service.hardware.external.cardreaderlib.data.enums.CardTransTypeConv;
import ru.m4bank.mpos.service.hardware.external.cardreaderlib.data.enums.TransactionTypeConv;
import ru.m4bank.mpos.service.hardware.printer.dto.data.FiscalPrinterMerchantData;
import ru.m4bank.mpos.service.hardware.printer.dto.data.PrinterInformationCheck;
import ru.m4bank.mpos.service.hardware.printer.dto.data.TransactionSlipData;
import ru.m4bank.mpos.service.transactions.data.AccountingSign;
import ru.m4bank.mpos.service.transactions.data.CorrectionType;
import ru.m4bank.mpos.service.transactions.data.external.CurrencyAmount;
import ru.m4bank.mpos.service.transactions.data.external.ExternalCorrectionData;
import ru.m4bank.mpos.service.transactions.data.external.ExternalFiscalData;
import ru.m4bank.mpos.service.transactions.data.external.ExternalSlipData;
import ru.m4bank.mpos.service.transactions.data.external.ExternalStubData;

public class DataCreatorUtils {
    public static ExternalCorrectionData makeCorrectionData() {
        Calendar date = Calendar.getInstance();
        date.set(2019, 3, 1);
        return new ExternalCorrectionData.Builder()
                .currencyAmount(makeCurrencyAmount())
                .calculationSign(AccountingSign.INCOMING)
                .correctionType(CorrectionType.INDEPENDENTLY)
                .taxRate(TaxRate.Tax_20)
                .cash(true)
                .docNumber("ПС345454")
                .docDate(date)
                .docDescription(" ")
                .stubData(makeStubData())
                .build();
    }

    public static ExternalFiscalData makeFiscalData() {
        return new ExternalFiscalData.Builder()
                .cash(false)
                .currency("643")
                .phone("12345678")
                .informationCheckData(makeInformationCheckData())
                .productsAndTransactionData(makeProductsAndTransactionData())
                .externalStubData(makeStubData())
                .transactionTypeConv(TransactionTypeConv.PAYMENT)
                .build();
    }

    public static ExternalSlipData makeSlipData(boolean newCheck) {
        return new ExternalSlipData.Builder()
                .currencyAmount(makeCurrencyAmount())
                .cash(false)
                .merchantData(makeMerchantData())
                .newCheck(newCheck)
                .transactionData(makeTransactionData())
                .transactionType(TransactionTypeConv.PAYMENT)
                .cashierName("Петров В.В.")
                .build();
    }

    public static CurrencyAmount makeCurrencyAmount() {
        return new CurrencyAmount.Builder().amount(10000).currencyCode(CurrencyEnum.RUS.getStringCurrency()).build();
    }

    public static FiscalPrinterMerchantData makeMerchantData() {
        return new FiscalPrinterMerchantData.Builder()
                .merchantAddress("улица Твардовского, Москва Россия")
                .merchantId("1732056")
                .merchantInn("123456489123")
                .merchantName("Center of corporate technologies")
                .build();
    }

    public static TransactionSlipData makeTransactionData() {
        return new TransactionSlipData.Builder()
                .applicationId("A40000100023056")
                .authCode("447041")
                .cardHolderName("IVANOV/I")
                .currencyCode("643")
                .hostResultCode("00")
                .interaction(CardTransTypeConv.MAGNETIC_STRIPE)
                .originalPan("************8888")
                .pan("************8176")
                .rrn("123456789")
                .terminalNumber("00687879")
                .terminalVerificationResults("00000")
                .transactionDate(new Date())
                .transactionStatus(TransactionStatus.APPROVED)
                .typeCard("Mastercard")
                .typeVerification(ExternalVerificationType.PIN)
                .phone("88008008008562")
                .build();
    }

    public static ExternalStubData makeStubData() {
        return new ExternalStubData.Builder()
                .shiftNumber("0001")
                .factoryNumberKKT("0000000000")
                .registerNumberKKT("00000000000000000")
                .fiscalDocNumber("2780")
                .fiscalMemoryNumber("9999078900003063")
                .fiscalSign("32323933373534383633")
                .formOfTaxation("УСН")
                .merchantData(makeMerchantData())
                .phone("12-12-12")
                .productsAndTransactionData(makeProductsAndTransactionData())
                .build();
    }

    private static ProductsAndTransactionData makeProductsAndTransactionData() {
        return new ProductsAndTransactionData(
                Lists.newArrayList(new GoodsData.Builder()
                        .setOriginalPrice(1500L)
                        .setRealPrice(1400L)
                        .setName("Товар")
                        .setQuantity(1)
                        .setCode("1")
                        .setUnits("00")
                        .setExponent(2)
                        .setDiscount(100L)
                        .setTaxRate(TaxRate.Tax_10)
                        .setAccountingSubject(AccountingSubject.OTHER)
                        .build()),
                1400L, 100L, 1, CurrencyEnum.RUS, TaxSystem.Common);
    }

    public static PrinterInformationCheck makeInformationCheckData() {
        return new PrinterInformationCheck.Builder()
                .setCheckNumber("00")
                .setDocNumber("1")
                .setPd("00")
                .setDateAndTime(new Date())
                .setInn("7777777777")
                .build();
    }
}
