package cz.moneta.test.regression.channels.digital.ib.newib.exchange;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.tasks.NewIbTasks;
import cz.moneta.test.dsl.newib.util.ExchangeFormData;
import cz.moneta.test.harness.annotations.Environment;
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 org.junit.jupiter.api.Order;

@TestScenarioWithOrder(name = "Exchange test")
public class NewIbExchangeTest {

    private Credentials loadCredentials(Harness harness, String configKey) {
        String rc = harness.getConfig(configKey);
        return harness.tasks()
                .udebs()
                .findCredentialsByRC(rc);
    }

    @Order(1)
    @TestCase(name = "Exchange test client with EUR and USD accounts", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9184")
    public void checkExchange(Harness harness) {

        Credentials clientCredentials = loadCredentials(harness, "nib.rc.exchange.all.accounts");

        ExchangeFormData formData = ExchangeFormData.builder()
                .firstAmount("2000")
                .secondAmount("200")
                .build();

        NewIbTasks newIbTasks = harness.tasks().newIb();

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.checkExchange(formData));
    }

    @Order(2)
    @TestCase(name = "Exchange test client without EUR and USD accounts", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9183")
    public void checkExchangeWithoutEurAndUsdAccounts(Harness harness) {

        Credentials clientCredentials = loadCredentials(harness, "nib.rc.exchange.no.foreign.account");

        NewIbTasks newIbTasks = harness.tasks().newIb();

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .goToForeignCurrencyAccountFromExchange();
    }

    @Order(3)
    @TestCase(name = "Exchange test client with EUR account", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9182")
    public void checkExchangeWithEurAccount(Harness harness) {

        Credentials clientCredentials = loadCredentials(harness, "nib.rc.exchange.eur.foreign.account");

        NewIbTasks newIbTasks = harness.tasks().newIb();

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .goToExchange()
                .checkForeignTitleIsPresent("USD")
                .clickOnCreateAccount()
                .checkForeignCurrencyIsPresent("USD");
    }

    @Order(4)
    @TestCase(name = "Exchange test client with USD account", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9748")
    public void checkExchangeWithUsdAccount(Harness harness) {

        Credentials clientCredentials = loadCredentials(harness, "nib.rc.exchange.usd.foreign.account");

        NewIbTasks newIbTasks = harness.tasks().newIb();

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .goToExchange()
                .checkForeignTitleIsPresent("EUR")
                .clickOnCreateAccount()
                .checkForeignCurrencyIsPresent("EUR");
    }
}
