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.DateUtils;
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.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;

@TestScenarioWithOrder(name = "Teller must be able to block card")
public class TemporarilyBlockCard {

    String endBlockTime = DateUtils.getFormattedTodayDateTime("HH:59:59");
    String reportingId;
    String testCardForJira;

    public void temporarilyBlockDebitCard(Harness harness, Credentials credentials, String birthNumber, String testCard) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withBirthNumber(birthNumber);
        String testCardLastNumbers = StringUtils.right(testCard, 4);

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

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

    public void temporarilyBlockCommercialDebitCard(Harness harness, Credentials credentials, String icoNumber, String testCard) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withIco(icoNumber);
        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().temporarilyBlockCommercialDebitCard(testCardLastNumbers, endBlockTime));

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

    public void checkCardIsNotBlockedAndLog(Harness harness, String testCard, String logID) {
        Result<Record> resultList = harness.getEndpoint(OdsEndpoint.class).executeSql(CreditCardSQL.FIND_CARD_BLOCK_CODE_BY_CARD_NUMBER.replaceAll("CARD_NUMBER", harness.getConfig(testCard)));
        if (resultList.getValue(0, 1).toString().equals("XNA")) {
            harness.storeGlobal(logID, "PASS");
        }
    }

    @Order(1)
    @TestCase(name = "Temporarily block Retail Standard Debit Card", environments = {TST1, PPE})
    public void checkThatCardsAreNotBlocked(Harness harness) {
        checkCardIsNotBlockedAndLog(harness, "cards_team.retail.standard.DC.for.temporary.block", "CT-T9583");
        checkCardIsNotBlockedAndLog(harness, "cards_team.retail.gold.DC.for.temporary.block", "CT-T6640");
        checkCardIsNotBlockedAndLog(harness, "cards_team.retail.usd.DC.for.temporary.block", "CT-T9581");
        checkCardIsNotBlockedAndLog(harness, "cards_team.retail.eur.DC.for.temporary.block", "CT-T9582");
        checkCardIsNotBlockedAndLog(harness, "cards_team.retail.smart.CC.for.temporary.block", "CT-T9579");
        checkCardIsNotBlockedAndLog(harness, "cards_team.retail.gold.CC.for.temporary.block", "CT-T9578");
        checkCardIsNotBlockedAndLog(harness, "cards_team.retail.account.CC.for.temporary.block", "CT-T9580");
        checkCardIsNotBlockedAndLog(harness, "cards_team.commercial.DC.for.temporary.block", "CT-T9585");
        checkCardIsNotBlockedAndLog(harness, "cards_team.commercial.premium.DC.for.temporary.block", "CT-T6687");
        checkCardIsNotBlockedAndLog(harness, "cards_team.commercial.usd.DC.for.temporary.block", "CT-T9586");
        checkCardIsNotBlockedAndLog(harness, "cards_team.commercial.eur.DC.for.temporary.block", "CT-T9584");
        checkCardIsNotBlockedAndLog(harness, "cards_team.MBC.for.temporary.block", "CT-T6843");
        checkCardIsNotBlockedAndLog(harness, "cards_team.child.card.for.temporary.block", "CT-T6651");
    }

    @EnableIfSet(globalKey = "CT-T9583")
    @TestCase(name = "Temporarily block Retail Standard Debit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9583")
    public void temporarilyBlockRetailStandardDebitCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        temporarilyBlockDebitCard(harness, credentials, harness.getConfig("cards_team.retail.rc.for.temporary.block"), harness.getConfig("cards_team.retail.standard.DC.for.temporary.block"));
    }

    @EnableIfSet(globalKey = "CT-T6640")
    @TestCase(name = "Temporarily block Retail Gold Debit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T6640")
    public void temporarilyBlockRetailGoldDebitCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        temporarilyBlockDebitCard(harness, credentials, harness.getConfig("cards_team.retail.rc.for.temporary.block.2"), harness.getConfig("cards_team.retail.gold.DC.for.temporary.block"));
    }

    @EnableIfSet(globalKey = "CT-T9581")
    @TestCase(name = "Temporarily block Retail USD Debit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9581")
    public void temporarilyBlockRetailUsdDebitCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        temporarilyBlockDebitCard(harness, credentials, harness.getConfig("cards_team.retail.rc.for.temporary.block.2"), harness.getConfig("cards_team.retail.usd.DC.for.temporary.block"));
    }

    @EnableIfSet(globalKey = "CT-T9582")
    @TestCase(name = "Temporarily block Retail EUR Debit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9582")
    public void temporarilyBlockRetailEurDebitCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        temporarilyBlockDebitCard(harness, credentials, harness.getConfig("cards_team.retail.rc.for.temporary.block"), harness.getConfig("cards_team.retail.eur.DC.for.temporary.block"));
    }

    @EnableIfSet(globalKey = "CT-T9579")
    @TestCase(name = "Temporarily block Retail Smart Credit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9579")
    public void temporarilyBlockRetailSmartCC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withBirthNumber(harness.getConfig("cards_team.retail.rc.for.temporary.block.CC"));
        String testCard = harness.getConfig("cards_team.retail.smart.CC.for.temporary.block");
        String testCardLastNumbers = StringUtils.right(testCard, 4);

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

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

    @EnableIfSet(globalKey = "CT-T9578")
    @TestCase(name = "Temporarily block Retail Gold Credit Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9578")
    public void temporarilyBlockRetailGoldCC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withBirthNumber(harness.getConfig("cards_team.retail.rc.for.temporary.block.CC.3"));
        String testCard = harness.getConfig("cards_team.retail.gold.CC.for.temporary.block");
        String testCardLastNumbers = StringUtils.right(testCard, 4);

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

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

    @EnableIfSet(globalKey = "CT-T9580")
    @TestCase(name = "Temporarily block Retail Credit Account", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9580")
    public void temporarilyBlockRetailAccountCC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withBirthNumber(harness.getConfig("cards_team.retail.rc.for.temporary.block.CC.2"));
        String testCard = harness.getConfig("cards_team.retail.account.CC.for.temporary.block");
        String testCardLastNumbers = StringUtils.right(testCard, 4);

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

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

    @EnableIfSet(globalKey = "CT-T9585")
    @TestCase(name = "Temporarily block Commercial DC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9585")
    public void temporarilyBlockCommercialDC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        temporarilyBlockCommercialDebitCard(harness, credentials, harness.getConfig("cards_team.commercial.ico.for.temporary.block"), harness.getConfig("cards_team.commercial.DC.for.temporary.block"));
    }

    @EnableIfSet(globalKey = "CT-T6687")
    @TestCase(name = "Temporarily block Commercial Premium DC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T6687")
    public void temporarilyBlockCommercialPremiumDC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        temporarilyBlockCommercialDebitCard(harness, credentials, harness.getConfig("cards_team.commercial.ico.for.temporary.block"), harness.getConfig("cards_team.commercial.premium.DC.for.temporary.block"));
    }

    @EnableIfSet(globalKey = "CT-T9586")
    @TestCase(name = "Temporarily block Commercial USD DC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9586")
    public void temporarilyBlockCommercialUsdDC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        temporarilyBlockCommercialDebitCard(harness, credentials, harness.getConfig("cards_team.commercial.ico.for.temporary.block"), harness.getConfig("cards_team.commercial.usd.DC.for.temporary.block"));
    }

    @EnableIfSet(globalKey = "CT-T9584")
    @TestCase(name = "Temporarily block Commercial EUR DC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T9584")
    public void temporarilyBlockCommercialEurDC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        temporarilyBlockCommercialDebitCard(harness, credentials, harness.getConfig("cards_team.commercial.ico.for.temporary.block"), harness.getConfig("cards_team.commercial.eur.DC.for.temporary.block"));
    }

    @EnableIfSet(globalKey = "CT-T6843")
    @TestCase(name = "Temporarily block MBC", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T6843")
    public void temporarilyBlockMBC(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withIco(harness.getConfig("cards_team.commercial.ico.for.temporary.block"));
        String testCard = harness.getConfig("cards_team.MBC.for.temporary.block");
        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().temporarilyBlockMBC(testCardLastNumbers, endBlockTime));

        reportingId = "Ičo klienta " + nonClient.birthNumber();
        testCardForJira = testCard;
    }

    @EnableIfSet(globalKey = "CT-T6651")
    @TestCase(name = "Temporarily block Child Card", environments = {TST1, PPE})
    @JiraTestCase(id = "CT-T6651")
    public void temporarilyBlockChildCard(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withBirthNumber(harness.getConfig("cards_team.child.rc.for.temporary.block"));
        String testCard = harness.getConfig("cards_team.child.card.for.temporary.block");
        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().temporarilyBlockRetailDebitCard(testCardLastNumbers, endBlockTime));

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

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