package cz.moneta.test.regression.commercial.smallbusiness.digital.localtests.banka.products.smeautomats.fop;

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.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.*;
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 cz.moneta.test.regression.commercial.smallbusiness.digital.DataPrepare;
import cz.moneta.test.regression.commercial.smallbusiness.digital.TearDown;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;

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_ID_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;

@TestScenario(name = "Draw Down Fee Express And Flexi Business")
public class DrawDownFeeExpressBusinessAndFlexiBusiness {

    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 = "Draw Down Fee Expres Business And Flexi Business", environments = {TST1, PPE})
    @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()))
                .then(bankaTasks.sharedTasks().storeFullNameAndIco())
                .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_KEY)));

        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
    protected void tearDown(Harness harness) {
        TearDown.storeIcoFullNameCifSessionId(harness);
    }
}
