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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.CardTypeAndDesign;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.NonClient;
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.TestScenarioWithOrder;
import cz.moneta.test.harness.endpoints.ods.OdsEndpoint;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import cz.moneta.test.harness.support.web.Lookup;
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;

@TestScenarioWithOrder(name = "Teller must be able to Onboard a Design Cards on new cif")
public class NewRetailCifAndOneClickAndVirtualCardsOnboarding {

    String newClientCif;
    String birthNumber;
    Long accountNumber;
    String accountWithoutCardXpath = "//span[contains(text(), 'Běžný účet')]//following::div[contains(@class, 'gwt-TreeItemChildSpan')][not(descendant::div[contains(@class, 'gwt-TreeItemChildSpanLast')])]//span[contains(text(), 'BÚ-FO- KONTO GENIUS')]";//span[contains(text(), 'Běžný účet')]//following::div[contains(@class, 'gwt-TreeItemChildSpan')][not(following-sibling::div[contains(@class, 'gwt-TreeItemChildSpan')])]//span[contains(text(), 'BÚ-FO- KONTO GENIUS')]";

    public void createAccountAndVirtualCard(Harness harness, Credentials credentials, String cardTypeAndDesign) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withBirthNumber(birthNumber);

        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        HKO101_S001_MainClientPage firstBanker = harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRcWithAlert(nonClient.birthNumber()));
        UfoEndpoint endpoint = harness.getEndpoint(UfoEndpoint.class);

        /*Zkontroluje zda má client účet bez založené karty.
        Pokud ano, uloží číslo takového účtu a založí na něm kartu, pokud ne založí nový účet a až na něm kartu.*/
        if (endpoint.isElementPresent(accountWithoutCardXpath, Lookup.XPATH)) {
            accountNumber = Long.valueOf(endpoint.getText(accountWithoutCardXpath, Lookup.XPATH).split(";")[1].trim());
            firstBanker
                    .then(bankaTasks.cardsOnboarding().createNewStandardDebitCardForExistingCurrentAccount(cardTypeAndDesign, "Virtual", accountNumber));
        } else {
            firstBanker
                    .then(bankaTasks.cardsOnboarding().createAccountForGoldCard());
            accountNumber = Long.valueOf(endpoint.getText(accountWithoutCardXpath, Lookup.XPATH).split(";")[1].trim());
            firstBanker
                    .then(bankaTasks.cardsOnboarding().createNewStandardDebitCardAfterNewAccount(cardTypeAndDesign, "Virtual", accountNumber));
        }
    }

    @Order(1)
    @TestCase(name = "Set up client", environments = {TST1, PPE})
    public void setup(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        newClientCif = harness.dataPrepare()
                .createNewFoCustomerViaWso2AndGetCif(credentials.getUsername());

        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().searchByCif(newClientCif))
                .getBirthNumber()
                .then(bankaTasks.cardsServicing().setUpPIM());

        birthNumber = harness.get(HKO101_S001_MainClientPage.BIRTH_NUMBER_STORE_KEY);
        harness.storeGlobal("CLIENT_OK", true);
    }

    @Order(2)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard plastic debit card for new client through ONECLICK", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T907")
    public void OneClickCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRC(birthNumber))
                .then(bankaTasks.cardsOnboarding().createOneClickRetailForCards());
    }

    @Order(3)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T10799")
    public void DcVirtualSwimming(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.SWIMMING.getValue());
    }

    @Order(4)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T10800")
    public void DcVirtualCycling(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.CYCLING.getValue());
    }

    @Order(5)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T10801")
    public void DcVirtualTennis(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.TENNIS.getValue());
    }

    @Order(6)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T10802")
    public void DcVirtualBeach(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.BEACH.getValue());
    }

    @Order(7)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9771")
    public void DcVirtualPrague(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.PRAGUE.getValue());
    }

    @Order(8)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9774")
    public void DcVirtualCentralBohemia(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.CENTRAL_BOHEMIAN_REGION.getValue());
    }

    @Order(9)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9760")
    public void DcVirtualSouthBohemia(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.SOUTH_BOHEMIAN_REGION.getValue());
    }

    @Order(10)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9770")
    public void DcVirtualPlzen(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.PLZEN.getValue());
    }

    @Order(11)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9762")
    public void DcVirtualKarlovyVary(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.KARLOVY_VARY.getValue());
    }

    @Order(12)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9775")
    public void DcVirtualUsti(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.USTI.getValue());
    }

    @Order(13)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9765")
    public void DcVirtualLiberec(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.LIBEREC.getValue());
    }

    @Order(14)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9764")
    public void DcVirtualHradec(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.HRADEC.getValue());
    }

    @Order(15)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9769")
    public void DcVirtualPardubice(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.PARDUBICE.getValue());
    }

    @Order(16)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9763")
    public void DcVirtualVysocina(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.VYSOCINA.getValue());
    }

    @Order(17)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9761")
    public void DcVirtualSouthMoravia(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.SOUTH_MORAVIA_REGION.getValue());
    }

    @Order(18)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9776")
    public void DcVirtualZlin(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.ZLIN.getValue());
    }

    @Order(19)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9768")
    public void DcVirtualOlomouc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.OLOMOUC.getValue());
    }

    @Order(20)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9767")
    public void DcVirtualMoraviaSilesian(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.MORAVIAN_SILESIAN_REGION.getValue());
    }

    @Order(21)
    @EnableIfSet(globalKey = "CLIENT_OK")
    @TestCase(name = "Standard virtual debit card and new account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9377")
    public void DcVirtualPride(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createAccountAndVirtualCard(harness, credentials, CardTypeAndDesign.PRIDE.getValue());
    }

    @AfterEach
    public void tearDown(Harness harness) {
        Result<Record> resultList = harness.getEndpoint(OdsEndpoint.class).executeSql(CreditCardSQL.FIND_ONBOARDED_CARDS_BY_RC.replaceAll("ID_NUMBER", birthNumber));
        harness.store("reports.tmfj.execution.comment", "Rodné číslo: " +
                birthNumber + "\n Číslo karty: " + resultList.getValue(0, 1).toString() +
                "\n" + harness.tasks().ufo().banka().cardsServicing().getPrintedPages());
    }
}
