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

import cz.moneta.test.dsl.Harness;
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.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.*;
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 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.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.po.ZAS003_S001_StatutoryAuthorityEvidencePage.BIRTH_NUMBER_KEY;
import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenarioWithOrder(name = "Create PO CIF, Income module, statutory authority, OneClick, Expres Business, Saving Account")
public class CreateNewPoOneclickExpresSavingAccount {

    private NonClient nonPoClient;
    private BankaTasks bankaTasks;

    @BeforeAll
    protected void setUp(Harness harness) {
        bankaTasks = harness.tasks().ufo().banka();
        nonPoClient = DataPrepare.setUpPo(harness);
    }

    @TestCase(name = "Create New PO Client", environments = {TST1, PPE})
    @Order(1)
    @JiraTestCase(id = "SBD-T481")
    public void createNewCifForPo(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByNewIco(nonPoClient.ico()))
                .then(bankaTasks.newPoClient().createPoCif(nonPoClient));

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

    @EnableIfSet(globalKey = "PO_CIF_PASSED")
    @TestCase(name = "Create FON Statutory Authority", environments = {TST1, PPE})
    @Order(2)
    @JiraTestCase(id = "SBD-T486")
    public void createFonStatutoryAuthority(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByNewRc(nonPoClient.birthNumber()))
                .then(bankaTasks.newStatutoryAuthority().createStatutoryAuthority(nonPoClient));

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

    @EnableIfSet(globalKey = "STAT_AUTH_PASSED")
    @TestCase(name = "OneClick PO", environments = {TST1, PPE})
    @Order(3)
    @JiraTestCase(id = "SBD-T483")
    public void oneClickPo(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIcoPo(nonPoClient.ico()))
                .then(bankaTasks.oneClick().createOneClickPo(harness));

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

    @EnableIfSet(globalKey = "ONECLICK_PASSED")
    @TestCase(name = "Fill Income Module", environments = {TST1, PPE})
    @Order(4)
    @JiraTestCase(id = "SBD-T498")
    public void incomeModulePo(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().findByIcoPo(nonPoClient.ico()))
                .then(bankaTasks.newIncomeModuleTasks().fillIncomeModulePo(lastYear, yearBeforeLast));

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

    @EnableIfSet(globalKey = "PO_INC_MOD_PASSED")
    @TestCase(name = "Create Expres Business PO", environments = {TST1, PPE})
    @Order(5)
    @JiraTestCase(id = "SBD-T455")
    public void createExpresBusinessForPo(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        String lastYear = DataPrepare.getLastYear();
        String yearBeforeLast = DataPrepare.getYearBeforeLast();
        String loanExpresAmount = "500000";
        String financingReason = "nákup strojů, výrobních zařízení";

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIcoPo(nonPoClient.ico()))
                .then(bankaTasks.newSmeAutomatPoTasks().withExpresBusiness(harness, loanExpresAmount, lastYear, yearBeforeLast, financingReason));
        harness.storeGlobal("CREATE_EB_PASSED", "PASS");
    }

    @EnableIfSet(globalKey = "ONECLICK_PASSED")
    @TestCase(name = "Saving Account PO", environments = {TST1, PPE})
    @Order(6)
    @JiraTestCase(id = "SBD-T457")
    public void savingAccountPo(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIcoPo(nonPoClient.ico()))
                .then(bankaTasks.newProduct().createSavingAccountWithInternetBanking());
    }

    @EnableIfSet(globalKey = "CREATE_EB_PASSED")
    @TestCase(name = "Draw Down Fee Expres Business", environments = {TST1, PPE})
    @Order(7)
    @JiraTestCase(id = "SBD-T1021")
    public void drawDownFeeExpressBusiness(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()
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(nonPoClient.ico())
                .clickIcoRadioAndContinueToStatutoryAuthorityEvidencePage()
                .storeBirthNumber()
                .clickEnd()
                .then(bankaTasks.search().findByRC(harness.get(BIRTH_NUMBER_KEY)))
                .storeClientIdNumber()
                .clickSearch();

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

        OBR003_S001_ClientContractSignPage first_instance = harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findLegalRepresentativeByIco(nonPoClient.ico()))
                .selectFromListOfRequests()
                .then(bankaTasks.newSmeAutomatPoTasks().drawDownFeeExpresBusiness(harness, bankerName));

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

        first_instance
                .then(bankaTasks.newSmeAutomatPoTasks().finishDrawingDownPo(harness, "Úvěr načerpán"))
                .then(bankaTasks.newSmeAutomatSubTasks().storeAccountNumber(harness.get(APPLICATION_ID_KEY), "Expres Business"))
                .then(bankaTasks.newSmeAutomatSubTasks().checkEstablishedExpresBusinessAccountDetail(harness));
    }

    @AfterEach
    protected void tearDown(Harness harness) {
        harness.store("reports.tmfj.execution.comment",
                "\nIČO klienta: " + nonPoClient.ico() +
                        "\nRČ klienta: " + nonPoClient.birthNumber() +
                        "\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)));
    }
}
