package cz.moneta.test.regression.commercial.salesforce.web;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.salesforce.pages.accounts.AccountDetailPage;
import cz.moneta.test.dsl.salesforce.tasks.SalesForceTasks;
import cz.moneta.test.dsl.salesforceapi.tasks.SalesForceApiTasks;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.NonConcurrentTestScenario;
import cz.moneta.test.harness.annotations.TestCase;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;

@NonConcurrentTestScenario(name = "Search and lock account")
public class LockUnlockAccountTests {
    private String account;
    private String accountRemedial;
    private String accountId;
    private SalesForceTasks tasks;
    private SalesForceApiTasks apiTasks;

    @BeforeAll
    public void checkAccountLock(Harness harness) {
        account = harness.getConfig("salesforce.test.account");
        accountRemedial = harness.getConfig("salesforce.test.accountRemedialYes");
        accountId = harness.getConfig("salesforce.test.accountId");
        tasks = harness.tasks().salesForce();
        apiTasks = harness.tasks().salesForceApiTasks();
    }

    //TODO SalesForce on PPE is not up to date, needs to be added and retested after its update
    @TestCase(name = "Search and lock account", environments = {Environment.TST1})
    @JiraTestCase(id = "SC-T710")
    public void lockAccount(Harness harness) {
        String accountOwner;
        String accountLocked;
        AccountDetailPage onAccountDetailPage;

        apiTasks.unlockAccount(accountId);

        onAccountDetailPage = harness.withSalesForce()
                .openLoginPage()
                .then(tasks.loginAsBanker())
                .then(tasks.openAccountDetail(account))
                .clickLockAccount()
                .selectAndSaveAccountOwner("Automation Test")
                .refreshPage();
        accountLocked = onAccountDetailPage.getAccountLock();
        accountOwner = onAccountDetailPage.getAccountOwner();
        onAccountDetailPage.logoutUser();

        Assertions.assertEquals("Ano", accountLocked, String.format("Account %s is not locked. Should be locked.", account));
        Assertions.assertEquals("Test Automation", accountOwner, String.format("Account owner is: %s. Expected: Test Automation", accountOwner));
    }

    //TODO SalesForce on PPE is not up to date, needs to be added and retested after its update
    @TestCase(name = "Search and unlock account", environments = {Environment.TST1})
    @JiraTestCase(id = "SC-T766")
    public void unlockAccount(Harness harness) {
        String accountOwner;
        String accountLocked;
        AccountDetailPage onAccountDetailPage;

        apiTasks.lockAccount(accountId);

        onAccountDetailPage = harness.withSalesForce()
                .openLoginPage()
                .then(tasks.loginAsBanker())
                .then(tasks.openAccountDetail(account))
                .clickUnlockAccount()
                .refreshPage();
        accountLocked = onAccountDetailPage.getAccountLock();
        accountOwner = onAccountDetailPage.getAccountOwner();
        onAccountDetailPage.logoutUser();

        Assertions.assertEquals("Ne", accountLocked, String.format("Account %s is locked. Should be unlocked.", account));
        Assertions.assertEquals("-", accountOwner, String.format("Account owner is: %s. Expected: -", accountOwner));
    }

    //TODO SalesForce on PPE is not up to date, needs to be added and retested after its update
    @TestCase(name = "Search and unlock account with Remedial flag - YES", environments = {Environment.TST1})
    @JiraTestCase(id = "SC-T773")
    public void lockAccountRemedial(Harness harness) {
        AccountDetailPage onAccountDetailPage;

        onAccountDetailPage = harness.withSalesForce()
                .openLoginPage()
                .then(tasks.loginAsBanker())
                .then(tasks.openAccountDetail(accountRemedial))
                .clickLockAccount()
                .checkIfSubjectInRemedial()
                .cancelDialogWindow();
        onAccountDetailPage.logoutUser();
    }

}
