package cz.moneta.test.regression.feeincome.insurance;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.ufo.banka.tasks.accountenums.CurrentAccountsEnum;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.NonClient;
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 org.junit.jupiter.api.*;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.CLIENT_CIF_STORE_KEY;
import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "Creating a new client and setting up insurance")
public class NewClientWithInsuranceUfo {

    private NonClient nonClient;
    private String birthNumber;

    @BeforeAll
    void setUpNewClient(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        nonClient = harness.data().newNonClient();
        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.newClient().create(nonClient, credentials.getUsername()))
                .then(bankaTasks.newPackagedProductTasks().ibWithMobileKeyAndCurrentAccount(nonClient, CurrentAccountsEnum.GENIUS_FREE_FLEXI));
    }

    @Order(1)
    @TestCase(name = "Log on UFO BANKA and new accident insurance junior", environments = {TST1, PPE})
    void arrangementInsuranceJunior(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoBanker) {
        String productName = "Úrazové pojištění - varianta Junior - rodičovský";
        birthNumber = nonClient.birthNumber();
        String rcJunior = harness.tasks().common().getRcByMaxAge(7);
        String account = harness.withUdebs().accountNumbers().getTopCurrentAccount(birthNumber).substring(1);
        BankaTasks tasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(tasks.login().withCredentialsAndTurnOffSignpad(ufoBanker))
                .then(tasks.newInsuranceGroupTasks().findCifAndCreateGroupInsuranceProduct(harness.get(CLIENT_CIF_STORE_KEY), productName, account))
                .fillRc(rcJunior)
                .fillGivenName("Junior")
                .fillFamilyName("Pojisteny")
                .clickCheckBox()
                .then(tasks.newInsuranceGroupTasks().completeAndCheckActiveInsurance(productName));
    }

    @Order(2)
    @TestCase(name = "Log on UFO BANKA and new Long Term Travel Insurance Europe Family", environments = {TST1, PPE})
    void createLongTermTravelEuropeFamilyInsuranceUfo(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoBanker) {
        String productName = "Dlouhodobé cestovní pojištění Evropa 2019 (family)";
        String account = harness.withUdebs().accountNumbers().getTopCurrentAccount(birthNumber).substring(1);
        BankaTasks tasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(tasks.login().withCredentialsAndTurnOffSignpad(ufoBanker))
                .then(tasks.newInsuranceGroupTasks().findCifAndCreateGroupInsuranceProduct(harness.get(CLIENT_CIF_STORE_KEY), productName, account))
                .then(tasks.newInsuranceGroupTasks().completeAndCheckActiveInsurance(productName));
    }

    @AfterEach
    void cancelInsurance(Harness harness) {
        harness.tasks()
                .insuranceTasks()
                .setInsuranceStatusTask()
                .cancelInsurance(birthNumber);
    }
}