package cz.moneta.test.sanity.transactional.cards.firstvision;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.DateFormatPatterns;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.sanity.transactional.SmokeData;
import org.junit.jupiter.api.BeforeAll;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Random;

import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.FV;
import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.SVC_RETURN;
import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.VMX_MSGIN;
import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.VMX_MSGOUT;

@TestScenario(name = "R2 M.CMS.FASTER.PMT.EMEA.E8V2")
public class Payments {
    private String account;

    public static synchronized String getFpsId() {
        LocalDateTime january1st = LocalDateTime.of(2019, 9, 1, 0, 0);
        LocalDateTime now = LocalDateTime.now();
        long seconds = Duration.between(january1st, now).getSeconds();
        seconds += 500_000_000;
        String timeStamp = String.format("%015d", seconds);
        int random = new Random().nextInt(900) + 100;
        return timeStamp + random;
    }

    @BeforeAll
    public void getInfo(Harness harness) {
        account = SmokeData.getInstance(harness).getCreditData().getAccount();
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1907")
    public void M_CMS_FASTER_PMT_EMEA(Harness harness) {
        String date = DateUtils.getFormattedTodayDate(DateFormatPatterns.FIRST_VISION_DATE_FORMAT_WITHOUT_SEPARATOR);
        String fpsID = getFpsId() + "10" + date + "CZK0000SMOKETE";
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.CMS.FASTER.PMT.EMEA", "E8V2"))
                .addField(VMX_MSGIN, "ACCT_NO", account)
                .addField(VMX_MSGIN, "FPS_ID", fpsID)
                .addField(VMX_MSGIN, "SRC_CNL", "BNK")
                .addField(VMX_MSGIN, "AMT", "400")
                .addField(VMX_MSGIN, "SVC_TYPE", "PYMT")
                .addField(VMX_MSGIN, "SORT_CODE", "PAYCORE")
                .addField(VMX_MSGIN, "SENDING_ACC_NO", "PAYCORE")
                .addField(VMX_MSGIN, "VAL_DATE", "20180410")
                .addField(VMX_MSGIN, "SETT_ID", "20190410")
                .addField(VMX_MSGIN, "TIME_STAMP", "20190410 1010010")
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".ACCT_NO", account);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2778")
    public void M_ASM_MONETARY_UPD_EMEA_E8V6(Harness harness) {
        int amount = new Random().nextInt(1000) + 1;
        NumberFormat formatter = new DecimalFormat("#.00");
        String expectedAmount = formatter.format(amount / 100.0);
        harness.withWso2()
                .prepareRequestXml()
                .withPath(FV)
                .withPayloadFromFile("regression/transactional/cards/sme/M_ASM_MONETARY_UPD_EMEA.xml")
                .addField(VMX_MSGIN, "ACCT_NBR", account)
                .addField(VMX_MSGIN, "FOREIGN_USE", "0")
                .addField(VMX_MSGIN, "MCC", "0")
                .addField(VMX_MSGIN + ".TXN_DATA", "ACTION_CODE", "INTR")
                .addField(VMX_MSGIN + ".TXN_DATA", "TXN_AMOUNT", amount)
                .addField(VMX_MSGIN + ".TXN_DATA", "PLAN_NBR", "10002")
                .addField(VMX_MSGIN + ".TXN_DATA", "STORE_NBR", "999999998")
                .addField(VMX_MSGIN + ".MEMO_DATA", "LINE_1", "Automation test transaction")
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_DATA.ACCT_NBR", account)
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_DATA.TXN_DATA.TXN_AMOUNT", expectedAmount);
    }
}
