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.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 org.apache.commons.lang.StringUtils;
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 Create Cif and Onboard a Card")
public class ClipAndDeclipForCreditCards {

    String birthNumber;
    String testCard;

    public void clipCreditCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.banka.secondBanker) Credentials secondBanker, String birthNumberOfClient, String testCardOfClient) {

        birthNumber = birthNumberOfClient;
        testCard = testCardOfClient;
        String testCardLastNumbers = StringUtils.right(testCard, 4);
        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        setup(harness, secondBanker, birthNumber);

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRcWithAlert(birthNumber))
                .then(bankaTasks.cardsServicing().clipRetailCreditCardBeforeSecondBankerPartOne(testCardLastNumbers))
                .then(bankaTasks.cardsServicing().clipRetailCreditCardBeforeSecondBankerFinalPart(harness));

        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsSecondBanker(secondBanker))
                .then(bankaTasks.cardsServicing().secondBankerApplicationRetailSign());

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRC(birthNumber))
                .then(bankaTasks.cardsServicing().clipRetailCreditCardAfterSecondBanker());
    }

    public void setup(Harness harness, @Key(Keys.ufo.banka.secondBanker) Credentials secondBanker, String clientNumber) {
        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(secondBanker))
                .globalStoreBankerName(harness)
                .then(bankaTasks.search().findByRC(clientNumber))
                .then(bankaTasks.cardsServicing().cancelOldRequestIfPossible())
                .then(bankaTasks.cardsServicing().confirmPIM());
    }

    @Order(1)
    @TestCase(name = "Clip Smart credit card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9939")
    public void clipSmartCreditCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.banka.secondBanker) Credentials secondBanker) {
        clipCreditCard(harness, credentials, secondBanker, harness.getConfig("cards_team.retail.rc.for.CC.clip1"), harness.getConfig("cards_team.retail.smart.CC.clip"));
        harness.storeGlobal("CLIP_SMART_OK", true);
    }

    @Order(2)
    @EnableIfSet(globalKey = "CLIP_SMART_OK")
    @TestCase(name = "Declip Smart credit card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9944")
    public void declipSmartCreditCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        birthNumber = harness.getConfig("cards_team.retail.rc.for.CC.clip1");
        testCard = harness.getConfig("cards_team.retail.smart.CC.clip");
        String testCardLastNumbers = StringUtils.right(testCard, 4);
        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRcWithAlert(birthNumber))
                .then(bankaTasks.cardsServicing().declipRetailCreditCard(testCardLastNumbers));

    }

    @Order(3)
    @TestCase(name = "Clip Gold credit card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9940")
    public void clipGoldCreditCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.banka.secondBanker) Credentials secondBanker) {
        clipCreditCard(harness, credentials, secondBanker, harness.getConfig("cards_team.retail.rc.for.CC.clip3"), harness.getConfig("cards_team.retail.gold.CC.clip"));
        harness.storeGlobal("CLIP_GOLD_OK", true);
    }

    @Order(4)
    @EnableIfSet(globalKey = "CLIP_GOLD_OK")
    @TestCase(name = "Declip Gold credit card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9942")
    public void declipGoldCreditCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        birthNumber = harness.getConfig("cards_team.retail.rc.for.CC.clip3");
        testCard = harness.getConfig("cards_team.retail.gold.CC.clip");
        String testCardLastNumbers = StringUtils.right(testCard, 4);
        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRcWithAlert(birthNumber))
                .then(bankaTasks.cardsServicing().declipRetailCreditCard(testCardLastNumbers));

    }

    @Order(5)
    @TestCase(name = "Clip Account credit card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9941")
    public void clipAccountCreditCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.banka.secondBanker) Credentials secondBanker) {
        clipCreditCard(harness, credentials, secondBanker, harness.getConfig("cards_team.retail.rc.for.CC.clip2"), harness.getConfig("cards_team.retail.account.CC.clip"));
        harness.storeGlobal("CLIP_ACCOUNT_OK", true);
    }

    @Order(6)
    @EnableIfSet(globalKey = "CLIP_ACCOUNT_OK")
    @TestCase(name = "Declip Account credit card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9943")
    public void declipAccountCreditCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        birthNumber = harness.getConfig("cards_team.retail.rc.for.CC.clip2");
        testCard = harness.getConfig("cards_team.retail.account.CC.clip");
        String testCardLastNumbers = StringUtils.right(testCard, 4);
        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByRcWithAlert(birthNumber))
                .then(bankaTasks.cardsServicing().declipRetailCreditCard(testCardLastNumbers));

    }

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