package cz.moneta.test.regression.cards.jenkinstests;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.tasks.NewIbTasks;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.NewClientWithCurrentAccountInformation;
import cz.moneta.test.dsl.util.task.retail.cards.constants.CreditCardSQL;
import cz.moneta.test.harness.annotations.EnableIfSet;
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.harness.endpoints.ods.OdsEndpoint;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.apache.commons.lang.StringUtils;
import org.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Order;

import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenario(name = "Teller must be able to onboard card in IB")
public class CardsIbOnboarding {

    NewClientWithCurrentAccountInformation newClientWithCurrentAccount;
    String clientBirthNumber;
    Credentials clientCredentials;
    String testCardForJira;

    public String createNewClientWithAccountAndIB(Harness harness, Credentials credentials) {
        newClientWithCurrentAccount = harness.dataPrepare()
                .createNewFoCustomerWithCurrentAccountAndIbViaWso2(credentials.getUsername());
        return (newClientWithCurrentAccount.getClientData().birthNumber());
    }

    @Order(1)
    @TestCase(name = "Set up clients", environments = {TST1, PPE})
    public void setup(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        clientBirthNumber = createNewClientWithAccountAndIB(harness, credentials);
        NewIbTasks newIbTasks = harness.tasks().newIb();

       clientCredentials = harness.tasks()
                .udebs()
                .findCredentialsByRC(clientBirthNumber);

        harness.withNewIb().
                openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.newClientSetupCards());

        harness.storeGlobal("IB_CARDS_SETUP", true);
    }

    @Order(2)
    @EnableIfSet(globalKey = "IB_CARDS_SETUP")
    @TestCase(name = "Set up clients", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T10895")
    public void onboardPlasticCardInIB(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb().
                openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.onboardingOfPlasticDebitCard());

        Result<Record> resultList = harness.getEndpoint(OdsEndpoint.class).executeSql(CreditCardSQL.FIND_ONBOARDED_CARDS_BY_RC.replaceAll("ID_NUMBER", clientBirthNumber));
        testCardForJira = resultList.getValue(0, 1).toString();

        harness.storeGlobal("IB_CARDS_PLASTIC", testCardForJira);
    }

    @Order(3)
    @EnableIfSet(globalKey = "IB_CARDS_SETUP")
    @TestCase(name = "Set up clients", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12502")
    public void onboardVirtualCardInIB(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb().
                openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.onboardingOfVirtualDebitCard());

        Result<Record> resultList = harness.getEndpoint(OdsEndpoint.class).executeSql(CreditCardSQL.FIND_ONBOARDED_CARDS_BY_RC.replaceAll("ID_NUMBER", clientBirthNumber));
        testCardForJira = resultList.getValue(0, 1).toString();

        harness.storeGlobal("IB_CARDS_VIRTUAL", testCardForJira);
    }

    @Order(4)
    @EnableIfSet(globalKey = "IB_CARDS_PLASTIC")
    @EnableIfSet(globalKey = "IB_CARDS_VIRTUAL")
    @TestCase(name = "Set up clients", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T11138")
    public void checkMaxCardOnboarded(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb().
                openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.checkMaxNumberOfCardsOnboarded());
    }

    @Order(5)
    @EnableIfSet(globalKey = "IB_CARDS_PLASTIC")
    @TestCase(name = "Set up clients", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12329")
    public void cancelDebitCard(Harness harness) {
        testCardForJira = harness.getGlobal("IB_CARDS_PLASTIC");
        String testCardLastNumbers = StringUtils.right(testCardForJira, 4);
        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb().
                openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.getIntoBlockingScreen(testCardLastNumbers, "Neaktivní"))
                .then(newIbTasks.cancelDebitCard());
    }

    @AfterEach
    public void tearDown(Harness harness) {
        harness.store("reports.tmfj.execution.comment",
                "\n Rodné číslo: " + clientBirthNumber +
                        "\n Číslo karty: " + testCardForJira);
    }
}
