package cz.moneta.test.regression.commercial.smallbusiness.digital.localtests.banka.testscenarios;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisProductClass;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
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.AfterEach;
import org.junit.jupiter.api.BeforeAll;
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 = "Saving Account PO")
public class CreatePoSavingAccount {

    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))
                .then(bankaTasks.sharedTasks().storeFullNameAndIco())
                .verifyThatCifIsValid();

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

    @EnableIfSet(globalKey = "PO_CIF_CREATED")
    @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))
                .then(bankaTasks.sharedTasks().storeFullNameAndIco());

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

    @EnableIfSet(globalKey = "FON_STAT_CREATED")
    @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))
                .then(bankaTasks.sharedTasks().storeFullNameAndIco())
        ;

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

    @EnableIfSet(globalKey = "ONECLICK_PASSED")
    @TestCase(name = "Saving Account PO", environments = {TST1, PPE})
    @Order(4)
    @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()))
                .createNewProduct(HCisProductClass.SAVINGS_ACCOUNT)
                .onRegistrationCertificatePage()
                .clickRightButton()
                .onScoringPage()
                .waitForLoader()
                .onSavingAccountPage()
                .selectProduct("SÚ_PRO_podnikání_4_PO")
                .selectFirstAccountNumber()
                .selectDistribution("Elektronicky IB")
                .selectPeriodicity("Měsíční")
                .clickSend()
                .onDocumentationSendPage()
                .sendDocumentsToClient()
                .clickLeftButton()
                .onAppendixSignedPage()
                .clickNoButton()
                .clickCenterButton()
                .onDispositionGeneratePage()
                .clickGenerateLater()
                .onProductsListPage()
                .clickContinueButtonAndContinueToMainClientPage()
                .then(bankaTasks.sharedTasks().storeFullNameAndIco());
    }

    @AfterEach
    public void tearDown(Harness harness) {
        TearDown.storeIcoFullNameCifSessionId(harness);
    }
}
