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

import com.fasterxml.jackson.databind.JsonNode;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.wso2.data.CardData;
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 cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;

import java.util.Random;

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

@TestScenario(name = "change card facade")
public class ChangeCardFacade {
    private String card;
    private String account;
    private String bu;
    private String pctTable;
    private String limitBankomat;
    private SmokeData smokeData;

    private String newBu;
    private String newMobile;
    private String newPctTable;
    private String newLimitBankomat;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = SmokeData.getInstance(harness);
        CardData creditData = smokeData.getCreditData();
        card = creditData.getCard();
        account = creditData.getAccount();
        bu = creditData.getBu();
        pctTable = getPctTable(harness);
        limitBankomat = getLimitBankomat(harness);
        //generated data
        Random random = new Random();
        newBu = random.nextInt(900_000) + 19_000_000 + "";
        newMobile = "420" + (100_000_000 + random.nextInt(899_999_999));
        newLimitBankomat = random.nextInt(50000) + "";
        newPctTable = random.nextInt(4) + 1001 + "";
    }

    private String getLimitBankomat(Harness harness) {
        JsonNode embosser = getEmbosser(harness);
        return embosser.path("ATM_CASH_AMT").asText();
    }

    private String getPctTable(Harness harness) {
        JsonNode embosser = getEmbosser(harness);
        return Integer.valueOf(embosser.path("SUPP_FEE_IND").asText()) + 1000 + "";
    }

    private JsonNode getEmbosser(Harness harness) {
        return harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.EMBOSSER.INQ", "E8V7B"))
                .addField("VMX_MSGIN", "ACCOUNT", card)
                .postExtended()
                .andAssertFieldValue("VMX_ROOT.VMX_MSGOUT.SVC_RETURN", "P")
                .extract("VMX_ROOT.VMX_MSGOUT");
    }

    private String getBu(Harness harness) {
        return harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.CARD.CUST.ACCT.INQ.CINEMA", "E8VCE"))
                .addField("VMX_MSGIN", "ACCOUNT", account)
                .postExtended()
                .andAssertFieldValue("VMX_ROOT.VMX_MSGOUT.SVC_RETURN", "P")
                .extract("VMX_ROOT.VMX_MSGOUT.ACCOUNT_OUTPUT.BANK_ACCT")
                .textValue()
                .substring(9);
    }

    private String getMobile(Harness harness) {
        return getEmbosser(harness).path("EMB_PIN_MOBILE_NBR").asText();
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1945")
    public void MWFAS6_CHG_CARD_FACADE(Harness harness) {
        Object activityId = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFAS6"))
                .addField("CisloKarty", card)
                .addField("CisloKaretnihoUctu", account)
                .addField("CisloBU", newBu)
                .addField("DrzitelTelefonMobilni", newMobile)
                .addField("LimitBankomat", newLimitBankomat)
                .addField("PCTTabulka", newPctTable)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("OnlineFlag", "1")
                .getHeader("activityId");
        String id = " activityId -> " + activityId;
        Assertions.assertEquals(newMobile, getMobile(harness), "Mobile number was not changed." + id);
        Assertions.assertEquals(newLimitBankomat + "00", getLimitBankomat(harness), "Bankomat limit was not changed." + id);
        Assertions.assertEquals(newBu, getBu(harness), "BU is not changed." + id);
    }

    @AfterAll
    public void restorePreviousData(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFAS6"))
                .addField("CisloKarty", card)
                .addField("CisloBU", bu)
                .addField("LimitBankomat", limitBankomat)
                .addField("PCTTabulka", pctTable)
                .post();
    }
}
