package cz.moneta.test.regression.creditdistribution.deposits;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.tasks.NewIbTasks;
import cz.moneta.test.dsl.util.task.monetaportal.childbuildingsavings.ChildBuildingSavingsUtils;
import cz.moneta.test.dsl.szrmock.SzrMockData;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.dsl.util.task.monetaportal.childbuildingsavings.ChildBuildingSavingsTasks;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.NonConcurrentTestScenario;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.BeforeAll;

import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.banker;
import static cz.moneta.test.harness.annotations.Environment.*;

@NonConcurrentTestScenario(name = "Regression tests for Child Building Savings")
public class ChildBuildingSavingsTests {
    NonClient father;
    NonClient mother;
    NonClient youngerChild;
    NonClient olderChild;
    SzrMockData szrData;
    Credentials ibCred;
    Credentials credentials;

    @BeforeAll
    public void beforeAll(Harness harness, @Key(banker) Credentials credentials) {
        this.credentials = credentials;
        ChildBuildingSavingsUtils utils = new ChildBuildingSavingsUtils(harness);
        father = utils.createClientFatherData();
        mother = utils.createClientMotherData(father);
        youngerChild = utils.createChildClientData(father, 0, 14);
        olderChild = utils.createChildClientData(father, 15, 18);
        szrData = SzrMockData.builder().client(father).build();
        ibCred = utils.createClient(szrData);
        utils.createChildExist(olderChild);
        harness.withIlods().setSegment("AML1_SAS", "M", father.birthNumber(), "");
    }

    @TestCase(name = "SS pro dítě - NTB 0 - 14 let", environments = {DEV, TST1, PPE})
    @JiraTestCase(id = "MSS-T1462", project = "MSS Development")
    public void childBuildingSavings_NTB_young(Harness harness) {
        doFLow(harness, true, youngerChild);
    }

    @TestCase(name = "SS pro dítě - ETB 15-17 let", environments = {DEV, TST1, PPE})
    @JiraTestCase(id = "MSS-T1463", project = "MSS Development")
    public void childBuildingSavings_ETB_old(Harness harness) {
        doFLow(harness, false, olderChild);
    }

    private void doFLow(Harness harness, boolean isLessThan15YearsOld, NonClient child) {
        ChildBuildingSavingsTasks tasks = harness.tasks().monetaPortal().childBuildingSavingsTasks();
        NewIbTasks ibTasks = harness.tasks().newIb();
        harness.withNewIb().openLoginPage()
                .then(ibTasks.loginWithUsernamePasswordAndDefaultMobKey(ibCred.getUsername(), ibCred.getPassword()))
                .goToChildBuildingSavings(harness)
                .then(tasks.fillCalculator("1000", "10", true))
                .then(tasks.logTestData(szrData.getCif(), child, father, mother))
                .then(tasks.fillConsents(isLessThan15YearsOld))
                .then(tasks.fillPersonalDetail())
                .then(tasks.fillPersonalAddress())
                .then(tasks.takeIdentity(child, isLessThan15YearsOld, mother.birthNumber(), father.birthNumber()))
                .then(tasks.fillPermanentAddress(isLessThan15YearsOld))
                .then(tasks.fillAdditionalInformation(child.nationality()))
                .then(tasks.fillAml())
                .then(tasks.fillPreContract())
                .then(tasks.fillContract())
                .then(tasks.processManualFraudCheckTask(child, isLessThan15YearsOld, credentials));
    }
}