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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.tasks.accountenums.DebitCardFeePlan;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.CardTypeAndDesign;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.util.BankaUtils;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.NonClient;
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.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import cz.moneta.test.regression.commercial.smallbusiness.digital.DataPrepare;
import cz.moneta.test.regression.commercial.smallbusiness.digital.TearDown;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;

import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.CLIENT_CIF_STORE_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPage.DOCUMENT_URLS_STORE_KEY;
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 commercial Cards on same depersonalized cif")
public class CommercialCardsOnboarding {
    private NonClient nonClient;
    private BankaTasks bankaTasks;

    @BeforeAll
    protected void setUp(Harness harness) {
        bankaTasks = harness.tasks().ufo().banka();
        nonClient = DataPrepare.setUpFop(harness).withEmail("dummy.A1A2A3A4A5A6A7A8@ge.com");
    }

    @TestCase(name = "Create New FOP Client", environments = {TST1, PPE})
    @Order(1)
    public void setUpFopCifForCardOnboarding(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByNewIco(nonClient.ico()))
                .then(bankaTasks.newFopClient().createFopCif(nonClient))
                .then(bankaTasks.newCurrentAccountTasks().createCurrentAccountFop(harness, nonClient))
                .verifyThatClientHasSomeActiveProducts();

        harness.storeGlobal("CIF_FOP_CARD_ONBOARDING", harness.get(CLIENT_CIF_STORE_KEY).toString());
    }

    @EnableIfSet(globalKey = "CIF_FOP_CARD_ONBOARDING")
    @TestCase(name = "Create Debit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T100")
    @Order(2)
    public void createDebitCardForFop(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIco(nonClient.ico()))
                .then(bankaTasks.newDebitCard().createDebitCard(CardTypeAndDesign.BUSINESS.getValue(), DebitCardFeePlan.FREE));

        harness.storeGlobal("FIRST_CARD_ONBOARDED", "PASS");
    }

    @EnableIfSet(globalKey = "FIRST_CARD_ONBOARDED")
    @TestCase(name = "Create second Debit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T196")
    @Order(3)
    public void createSecondDebitCardForFop(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIco(nonClient.ico()))
                .then(bankaTasks.cardsOnboarding().createBusinessPremiumCardForFop("Plastic"));

        harness.storeGlobal("FIRST_CARD_ONBOARDED", "PASS");
    }

    @EnableIfSet(globalKey = "FIRST_CARD_ONBOARDED")
    @TestCase(name = "Create EUR Debit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12517")
    @Order(3)
    public void createEurDebitCardForFop(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIco(nonClient.ico()))
                .then(bankaTasks.cardsOnboarding().createFopAccountWithDifferentCurrency("EUR"))
                .then(bankaTasks.cardsOnboarding().createBusinessCardForFop("Plastic", 3));
    }

    @EnableIfSet(globalKey = "FIRST_CARD_ONBOARDED")
    @TestCase(name = "Create USD Debit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12518")
    @Order(4)
    public void createUSDDebitCardForFop(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIco(nonClient.ico()))
                .then(bankaTasks.cardsOnboarding().createFopAccountWithDifferentCurrency("USD"))
                .then(bankaTasks.cardsOnboarding().createBusinessCardForFop("Plastic", 3));
    }

    @AfterEach
    public void tearDown(Harness harness) {
        harness.store("reports.tmfj.execution.comment",
                "\nIČO klienta: " + nonClient.ico() +
                        "\nCIF klienta: " + harness.get(CLIENT_CIF_STORE_KEY, true) +
                        "\nUFO Session ID: " + harness.get("reports.tmfj.execution.commentufosessionid") +
                        "\nURL dokumentov: \n" + BankaUtils.formatDocumentUrls(harness.get(DOCUMENT_URLS_STORE_KEY, true)));
    }

    @AfterAll
    public void finalTearDown(Harness harness) {
        TearDown.depersonalizeFopClient(harness, nonClient.ico(), harness.get("CIF_FOP_CARD_ONBOARDING", true));
    }
}
