package cz.moneta.test.regression.backoffice.callcenter.jenkinstests.ccretail.cczakladbanka;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.operations.search.SearchBy;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.Address;
import cz.moneta.test.dsl.util.task.ufo.operations.OperationsTasks;
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 org.junit.jupiter.api.BeforeAll;

import static cz.moneta.test.dsl.ufo.operations.client.clientDetail.STS701_S002_ClientDetailPage.ACTUAL_ADDRESS_KEY;
import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenario(name = "Change address - sending debit card on OM")
public class SendingDebitCardOnOM {

    private OperationsTasks operationsTasks;
    private String rcOfExistingClient;
    private String icoOfExistingClient;
    private final Address ADDRESS_BENESOV = Address.builder().street("Tyršova").city("Benešov").zip("25601").descriptiveNumber("2077").build();
    private final Address ADDRESS_BEROUN = Address.builder().street("Havlíčkova").city("Beroun").zip("26601").descriptiveNumber("115").build();
    private final String expectedPostOffice = "MONETA Money Bank, a.s.";
    private final String expectedResultText = "Call centrum Banka - PLATEBNÍ KARTA - Žádost o akci - změna adresy PIN";

    @BeforeAll
    public void setupInfo(Harness harness) {
        operationsTasks = harness.tasks().ufo().operations();
        rcOfExistingClient = harness.getSystemOrConfigValue("cc.sendingDebitCardOnOmFO.rc");
        icoOfExistingClient = harness.getSystemOrConfigValue("cc.sendingDebitCardOnOmFOP.ico");
    }

    @TestCase(name = "Change address - sending debit card on OM - FO", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2207")
    public void addressChangeSendingDebitCardOnOmFO(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        harness.withUfo()
                .openLoginPage()
                .then(operationsTasks.login().withCredentials(credentials))
                .then(operationsTasks.searchClient().byRc(rcOfExistingClient))
                .clickDebitCard()
                .storeDebitCardSendingAddressStreetLabel()
                .clickDataChangesButton()
                .then(operationsTasks.debitCardChangesTasks()
                        .setSendingAddressAndCheckChange(getAnotherAddress(harness.get(ACTUAL_ADDRESS_KEY)), expectedPostOffice, expectedResultText)
                );
    }

    @TestCase(name = "Change address - sending on OM with debit card - FOP", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2208")
    public void addressChangeSendingDebitCardOnOmFOP(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasks.login().withCredentials(credentials))
                .fillSearchInput(icoOfExistingClient)
                .searchBy(SearchBy.ICO)
                .onClientDetailsPage()
                .clickDebitCard()
                .storeDebitCardSendingAddressStreetLabel()
                .clickDataChangesButton()
                .then(operationsTasks.debitCardChangesTasks()
                        .setSendingAddressAndCheckChange(getAnotherAddress(harness.get(ACTUAL_ADDRESS_KEY)), expectedPostOffice, expectedResultText)
                );
    }

    private Address getAnotherAddress(String actualAddress) {
        if (actualAddress.equalsIgnoreCase(ADDRESS_BENESOV.getStreet() + " " + ADDRESS_BENESOV.getDescriptiveNumber())) {
            return ADDRESS_BEROUN;
        } else {
            return ADDRESS_BENESOV;
        }
    }
}
