package cz.moneta.test.regression.commercial.smallbusiness.digital.jenkinstests.banka;

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.pages.cards.debit.OBR003_S001_ClientContractSignPage;
import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.ufo.banka.tasks.products.NewSmeAutomatFopTasks;
import cz.moneta.test.dsl.ufo.banka.tasks.products.NewSmeAutomatSubTasks;
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.*;

import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.OPN008_S001_SignContractPage.APPLICATION_ID_KEY;
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.HKO101_S001_MainClientPage.CLIENT_ID_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPage.DOCUMENT_URLS_STORE_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.SMA002_S001_SMEAutomatsApplicationOverview.ACCOUNT_NUMBER_KEY;
import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenarioWithOrder(name = "Create New FOP Client with IB, Income module, Saving Account, Debit Card and EB/FB")
public class CreateNewFopBuExpresFlexiDebitSavingAccount {

    private NonClient nonClient;
    private BankaTasks bankaTasks;
    private NewSmeAutomatFopTasks newSmeAutomatFopTasks;
    private NewSmeAutomatSubTasks newSmeAutomatSubTasks;

    @BeforeAll
    protected void setUp(Harness harness) {
        bankaTasks = harness.tasks().ufo().banka();
        nonClient = DataPrepare.setUpFop(harness);
        newSmeAutomatFopTasks = bankaTasks.newSmeAutomatFopTasks();
        newSmeAutomatSubTasks = bankaTasks.newSmeAutomatSubTasks();
    }

    @TestCase(name = "Create New FOP Client", environments = {TST1, PPE})
    @Order(1)
    @JiraTestCase(id = "SBD-T480")
    public void createNewFopCif(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));

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

    @EnableIfSet(globalKey = "FOP_CIF_PASSED")
    @TestCase(name = "Create Current Account with Internet Banking", environments = {TST1, PPE})
    @Order(2)
    @JiraTestCase(id = "SBD-T402")
    public void createCurrentAccountForFop(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.newCurrentAccountTasks().createCurrentAccountFop(harness, nonClient))
                .verifyThatClientHasSomeActiveProducts();

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

    @EnableIfSet(globalKey = "CURR_ACC_PASSED")
    @TestCase(name = "Fill Income Module", environments = {TST1, PPE})
    @Order(3)
    @JiraTestCase(id = "SBD-T497")
    public void fillIncomeModuleFop(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        String lastYear = DataPrepare.getLastYear();
        String yearBeforeLast = DataPrepare.getYearBeforeLast();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIco(nonClient.ico()))
                .then(bankaTasks.newIncomeModuleTasks().fillIncomeModuleFop(lastYear, yearBeforeLast))
                .then(bankaTasks.sharedTasks().storeFullNameAndIco());

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

    @EnableIfSet(globalKey = "FOP_INC_MOD_PASSED")
    @TestCase(name = "Create SME Automats - Expres Business/Flexi Business", environments = {TST1, PPE})
    @Order(4)
    @JiraTestCase(id = "SBD-T433")
    public void createSmeAutomatsExpresAndFlexiBusiness(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        String dummyEmail = "dummy.a1a2a3a4a5a6a7a8@ge.com";
        String loanExpresAmount = "250000";
        String loanFlexiAmount = "250000";
        String lastYear = DataPrepare.getLastYear();
        String yearBeforeLast = DataPrepare.getYearBeforeLast();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIco(nonClient.ico()))
                .then(bankaTasks.newSmeAutomatFopTasks().withExpresBusinessAndFlexiBusiness(harness, dummyEmail, loanExpresAmount, loanFlexiAmount, lastYear, yearBeforeLast));
        harness.storeGlobal("FOP_SME_AUTOMATS_PASSED", "PASS");
    }

    @EnableIfSet(globalKey = "CURR_ACC_PASSED")
    @TestCase(name = "Create Debit Card", environments = {TST1, PPE})
    @Order(5)
    @JiraTestCase(id = "SBD-T487")
    public void createDebitCard(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));
    }

    @EnableIfSet(globalKey = "CURR_ACC_PASSED")
    @TestCase(name = "Saving Account", environments = {TST1, PPE})
    @Order(6)
    @JiraTestCase(id = "SBD-T456")
    public void createSavingAccountFop(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.newProduct().createNewSavingsAccount("Spořící účet"));
    }

    @EnableIfSet(globalKey = "FOP_SME_AUTOMATS_PASSED")
    @TestCase(name = "Draw Down Fee Express And Flexi Business", environments = {TST1, PPE})
    @Order(7)
    @JiraTestCase(id = "SBD-T939")
    public void drawDownFeeExpressBusinessAndFlexiBusiness(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials,
                                                           @Key(Keys.ufo.banka.secondBanker) Credentials secondBanker) {

        HLO001_S001_ClientSearchPage second_instance = harness.withUfoSecondInstance()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsSecondBanker(secondBanker))
                .storeBankerName();

        String bankerName = BankaUtils.formatBankerName(harness.get("BANKER_NAME"));

        OBR003_S001_ClientContractSignPage first_instance = harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIco(nonClient.ico()))
                .storeClientIdNumber()
                .selectFromListOfRequests()
                .then(newSmeAutomatFopTasks.drawDownFeeExpresAndFlexiBusiness(harness, bankerName))
                .then(newSmeAutomatFopTasks.printAndSignExpresBusiness(harness, bankerName));

        second_instance
                .then(bankaTasks.cardsServicing().secondBankerApplicationSign(harness.get(APPLICATION_ID_KEY), harness.get(CLIENT_ID_KEY)));

        first_instance
                .then(newSmeAutomatFopTasks.finishDrawingDown(harness, "Úvěr načerpán"))
                .clickRequest(harness.get(APPLICATION_ID_KEY))
                .clickOpenRequestDetail()
                .onSMEAutomatsApplicationsOverview()
                .then(newSmeAutomatFopTasks.printAndSignFlexiBusiness(harness, bankerName));

        second_instance
                .then(bankaTasks.cardsServicing().secondBankerApplicationSign(harness.get(APPLICATION_ID_KEY), harness.get("CLIENT_ID")));

        first_instance
                .then(newSmeAutomatFopTasks.finishDrawingDown(harness, "Rámec otevřen"))
                .clickBackToMainPage()
                .then(newSmeAutomatSubTasks.storeAccountNumber(harness.get(APPLICATION_ID_KEY), "Expres Business"))
                .then(newSmeAutomatSubTasks.checkEstablishedExpresBusinessAccountDetail(harness))
                .then(newSmeAutomatSubTasks.storeAccountNumber(harness.get(APPLICATION_ID_KEY), "Flexi Business"))
                .then(newSmeAutomatSubTasks.checkEstablishedKontokorentDetail(harness.get(ACCOUNT_NUMBER_KEY)));

    }

    @AfterEach
    public void tearDown(Harness harness) {
        harness.store("reports.tmfj.execution.comment",
                "\nRČ klienta: " + nonClient.birthNumber() +
                        "\nIČO klienta: " + nonClient.ico() +
                        "\nJméno klienta: " + nonClient.fullName() +
                        "\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(CLIENT_CIF_STORE_KEY, true));
    }

}