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

import cz.moneta.test.dsl.Harness;
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.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.AfterEach;

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 generate PIN for Card")
public class GeneratePinForCard {

    NonClient nonClient;
    String testCard;
    String reportingId;

    public void generatePinForRetailCardsStandard(Harness harness, Credentials credentials, String birthNum, String cardNumber) {
        nonClient = harness.data().newFVNonClient()
                .withBirthNumber(birthNum);
        String testCardLastNumbers = StringUtils.right(cardNumber, 4);

        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRcWithAlert(nonClient.birthNumber()))
                .then(bankaTasks.cardsServicing().generatePinForRetailDebitCard(testCardLastNumbers));

        testCard = cardNumber;
        reportingId = "Rč klienta " + nonClient.birthNumber();
    }

    public void generatePinForRetailCreditCards(Harness harness, Credentials credentials, String birthNum, String cardNumber) {
        nonClient = harness.data().newFVNonClient()
                .withBirthNumber(birthNum);
        String testCardLastNumbers = StringUtils.right(cardNumber, 4);

        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRcWithAlert(nonClient.birthNumber()))
                .then(bankaTasks.cardsServicing().generatePinForRetailCreditCard(testCardLastNumbers));

        testCard = cardNumber;
        reportingId = "Rč klienta " + nonClient.birthNumber();
    }

    public void generatePinForCommercialDebitCards(Harness harness, Credentials credentials, String icoNumber, String cardNumber) {
        nonClient = harness.data().newFVNonClient()
                .withIco(icoNumber);
        String testCardLastNumbers = StringUtils.right(cardNumber, 4);
        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIcoWithAlert(nonClient.ico()))
                .then(bankaTasks.cardsServicing().generatePinForCommercialDebitCard(testCardLastNumbers));

        testCard = cardNumber;
        reportingId = "Ičo: " + nonClient.ico();
    }

    @TestCase(name = "Generate Pin for plastic retail standard Dc", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9934")
    public void generatePinForStandardDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForRetailCardsStandard(harness, credentials, harness.getConfig("cards_team.retail.rc"), harness.getConfig("cards_team.retail.standard.DC"));
    }

    @TestCase(name = "Generate Pin for virtual retail standard Dc", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9935")
    public void generatePinForVirtualStandardDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForRetailCardsStandard(harness, credentials, harness.getConfig("cards_team.retail.rc"), harness.getConfig("cards_team.retail.standard.virtual.DC"));
    }

    @TestCase(name = "Generate Pin for retail plastic gold Dc", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9936")
    public void generatePinForPlasticGoldDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForRetailCardsStandard(harness, credentials, harness.getConfig("cards_team.retail.rc"), harness.getConfig("cards_team.retail.gold.DC"));
    }

    @TestCase(name = "Generate Pin for eur Dc", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9937")
    public void generatePinForEurDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForRetailCardsStandard(harness, credentials, harness.getConfig("cards_team.retail.rc"), harness.getConfig("cards_team.retail.eur.DC"));
    }

    @TestCase(name = "Generate Pin for usd Dc", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9938")
    public void generatePinForUsdDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForRetailCardsStandard(harness, credentials, harness.getConfig("cards_team.retail.rc"), harness.getConfig("cards_team.retail.usd.DC"));
    }

    @TestCase(name = "Generate Pin for retail smart CC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T329")
    public void generatePinForSmartCC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForRetailCreditCards(harness, credentials, harness.getConfig("cards_team.retail.rc2"), harness.getConfig("cards_team.retail.smart.CC"));
    }

    @TestCase(name = "Generate Pin for retail gold CC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12504")
    public void generatePinForGoldCC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForRetailCreditCards(harness, credentials, harness.getConfig("cards_team.retail.rc5"), harness.getConfig("cards_team.retail.gold.CC"));
    }

    @TestCase(name = "Generate Pin for retail credit account CC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12503")
    public void generatePinForCreditAccountCC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForRetailCreditCards(harness, credentials, harness.getConfig("cards_team.retail.rc4"), harness.getConfig("cards_team.retail.account.CC"));
    }

    @TestCase(name = "Generate PIN for commercial DC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12505")
    public void generatePinForCommercialDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForCommercialDebitCards(harness, credentials, harness.getConfig("cards_team.commercial.ico.with.cards"), harness.getConfig("cards_team.commercial.DC"));
    }

    @TestCase(name = "Generate PIN for commercial premium DC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12510")
    public void generatePinForCommercialPremiumDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForCommercialDebitCards(harness, credentials, harness.getConfig("cards_team.commercial.ico.with.cards"), harness.getConfig("cards_team.commercial.premium.DC"));
    }

    @TestCase(name = "Generate PIN for commercial eur DC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12508")
    public void generatePinForCommercialEurDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForCommercialDebitCards(harness, credentials, harness.getConfig("cards_team.commercial.ico.with.cards"), harness.getConfig("cards_team.commercial.EUR"));
    }

    @TestCase(name = "Generate PIN for commercial usd DC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12507")
    public void generatePinForCommercialUsdDc(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        generatePinForCommercialDebitCards(harness, credentials, harness.getConfig("cards_team.commercial.ico.with.cards"), harness.getConfig("cards_team.commercial.USD"));
    }

    @TestCase(name = "Generate PIN for MBC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12509")
    public void generatePinForMBC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withIco(harness.getConfig("cards_team.commercial.ico.with.cards"));
        testCard = harness.getConfig("cards_team.commercial.CC");
        String testCardLastNumbers = StringUtils.right(testCard, 4);

        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIcoWithAlert(nonClient.ico()))
                .then(bankaTasks.cardsServicing().generatePinForMBC(testCardLastNumbers));

        reportingId = "Ičo klienta " + nonClient.ico();
    }

    @TestCase(name = "Generate PIN for Child Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T12506")
    public void generatePinForChildCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withBirthNumber(harness.getConfig("cards_team.child.rc"));
        testCard = harness.getConfig("cards_team.child.card");
        String testCardLastNumbers = StringUtils.right(testCard, 4);

        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findChildByRC(nonClient.birthNumber()))
                .then(bankaTasks.cardsServicing().generatePinForRetailDebitCard(testCardLastNumbers));

        reportingId = "Rč klienta " + nonClient.birthNumber();
    }

    @AfterEach
    public void tearDown(Harness harness) {
        harness.store("reports.tmfj.execution.comment",
                reportingId + "\n Číslo karty: " + testCard +
                        "\n" + harness.tasks().ufo().banka().cardsServicing().getPrintedPages());
    }
}
