package cz.moneta.test.regression.transactional.payments.cashless.payment_engine.analytical;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.enums.AccountStates;
import cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_clients.PaymentEngineClientsListOfClients;
import cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_clients.PaymentEngineClientsListOfClientsAccounts;
import cz.moneta.test.dsl.payment_engine.tasks.PaymentEngineTasks;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.Address;
import cz.moneta.test.dsl.util.data.client.NonClient;
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 cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;

import java.util.concurrent.ThreadLocalRandom;

import static cz.moneta.test.dsl.payment_engine.enums.AccountStates.*;

@TestScenarioWithOrder(name = "Send online static account data")
public class SendStaticAccountDataTest {

    private String clientId;
    private String clientsAccount;
    private String timeAccount;
    private String loanAccount;
    private String TOKEN_CBL;
    private String TOKEN_UFO;
    private String TOKEN_HUB;
    private String randomNameNumber;
    private static final String oldBranchCode = "704";
    private static final String newBranchCode = "770";
    private NonClient nonClient;
    private PaymentEngineTasks tasks;

    @BeforeAll
    void testSetUp(Harness harness) {
        randomNameNumber = String.valueOf(ThreadLocalRandom.current().nextInt(100, 999));
        clientId = harness.getConfig("payments_cashless.domesticPayment.clientId");
        clientsAccount = harness.getConfig("payments_cashless.domesticPayment.clientsAccount");
        timeAccount = harness.getConfig("payments_cashless.domesticPayment.timeAcc");
        loanAccount = harness.getConfig("payments_cashless.domesticPayment.loanAcc");
        TOKEN_CBL = harness.tasks().wso2().authorization().getToken("DigitalchannelsSB_CBL");
        TOKEN_UFO = harness.tasks().wso2().authorization().getToken("UFO");
        TOKEN_HUB = harness.tasks().wso2().authorization().getToken("HUB");
        nonClient = harness.data().newNonClient()
                .withFirstName("Sylvester" + randomNameNumber)
                .withLastName("Stallone")
                .withResidencyAddress(Address.builder()
                        .street("Nerudova")
                        .orientationNumber("8")
                        .city("Praha")
                        .cityPart("Malá Strana")
                        .zip("118 00")
                        .country("Česká republika")
                        .build());
    }

    @BeforeEach
    void testSetUpUniqueData(Harness harness) {
        tasks = harness.tasks().paymentEngineTasks();
    }

    private void changeClientsAccountState(Harness harness, String clientId, NonClient nonClient) {
        String request = harness.tasks()
                .wso2()
                .payments()
                .createRequestForClientNameChange(clientId, nonClient);
        harness.tasks()
                .wso2()
                .payments()
                .changeClientName(TOKEN_CBL, request);
    }

    private void changeClientsAccountState(Harness harness, String account, AccountStates clientsAccountState) {
        String request = harness.tasks()
                .wso2()
                .payments()
                .createRequestForClientsAccountStateChange(account, clientsAccountState);
        harness.tasks()
                .wso2()
                .payments()
                .changeClientsAccountState(TOKEN_CBL, request);
    }

    private void changeClientsTimeAccountState(Harness harness, String account, AccountStates clientsAccountState) {
        String request = harness.tasks()
                .wso2()
                .payments()
                .createRequestForClientsTimeAccountStateChange(account, clientsAccountState);
        harness.tasks()
                .wso2()
                .payments()
                .changeClientsTimeAccountState(TOKEN_UFO, request);
    }

    private void changeClientsLoanAccountBranchCode(Harness harness, String account, String branchCode) {
        String request1 = harness.tasks()
                .wso2()
                .payments()
                .createRequestForClientsLoanAccountBranchCodeChange(account, branchCode);
        harness.tasks()
                .wso2()
                .payments()
                .changeClientsLoanAccountBranchCode(TOKEN_HUB, request1);
    }

    @JiraTestCase(id = "PAY-T3562")
    @Order(1)
    @TestCase(name = "Send online static account data - change CIF name", environments = {Environment.TST1, Environment.PPE})
    void changeClientNameInICBSAndCheckInPAE(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1) {

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAndStoreClientName(clientId));
        String clientName1 = harness.get(PaymentEngineClientsListOfClients.CLIENT_NAME_KEY);
        harness.withPaymentEngine().closePaymentEngine();

        changeClientsAccountState(harness, clientId, nonClient);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.checkAndStoreNewClientName(clientId, nonClient));
        String clientName2 = harness.get(PaymentEngineClientsListOfClients.CLIENT_NAME_KEY);
        PaymentEngineUtils.logChangeOfClientName(harness, clientName1, clientName2, clientId);
        harness.withPaymentEngine().closePaymentEngine();
        Assertions.assertNotEquals(clientName1, clientName2, "The account's name does not equal the previous one after the change");
    }

    @JiraTestCase(id = "PAY-T3563")
    @Order(2)
    @TestCase(name = "Send online static account data - change clients account state", environments = {Environment.TST1, Environment.PPE})
    void changeAccountStatusInICBSAndCheckInPAE(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1) {

        changeClientsAccountState(harness, clientsAccount, ACTIVE);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAccount(clientsAccount))
                .then(tasks.checkClientAccountStateActive());
        harness.withPaymentEngine().closePaymentEngine();

        changeClientsAccountState(harness, clientsAccount, BLOCKED);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAccount(clientsAccount))
                .wait10sec() //PayHub cannot process the change on PPE environment faster than 10 seconds
                .clickOnRefresh()
                .then(tasks.checkClientAccountStateBlocked());
        harness.withPaymentEngine().closePaymentEngine();

        changeClientsAccountState(harness, clientsAccount, ACTIVE);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAccount(clientsAccount))
                .wait10sec() //PayHub cannot process the change on PPE environment faster than 10 seconds
                .clickOnRefresh()
                .then(tasks.checkClientAccountStateActive());
        PaymentEngineUtils.logChangeOfClientAccountState(harness, clientsAccount, ACTIVE, BLOCKED);
        harness.withPaymentEngine().closePaymentEngine();
    }

    @JiraTestCase(id = "PAY-T3564")
    @Order(3)
    @TestCase(name = "Send online static account data - change clients TIME account state", environments = {Environment.TST1, Environment.PPE})
    void changeTimeAccountStatusInICBSAndCheckInPAE(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1) {

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAccount(timeAccount))
                .then(tasks.checkClientAccountStateActive());
        harness.withPaymentEngine().closePaymentEngine();

        changeClientsTimeAccountState(harness, timeAccount, CLOSED_FOR_ENROLLMENT);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAccount(timeAccount))
                .wait5sec()
                .then(tasks.checkClientAccountStateBlocked());
        harness.withPaymentEngine().closePaymentEngine();

        changeClientsTimeAccountState(harness, timeAccount, ACTIVE);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAccount(timeAccount))
                .wait5sec()
                .then(tasks.checkClientAccountStateActive());
        PaymentEngineUtils.logChangeOfClientAccountState(harness, timeAccount, ACTIVE, CLOSED_FOR_ENROLLMENT);
        harness.withPaymentEngine().closePaymentEngine();
    }

    @JiraTestCase(id = "PAY-T3565")
    @Order(4)
    @TestCase(name = "Send online static account data - change clients LOAN account branch code", environments = {Environment.TST1, Environment.PPE})
    void changeLoanAccountBranchCodeInICBSAndCheckInPAE(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1) {

        changeClientsLoanAccountBranchCode(harness, loanAccount, newBranchCode);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAccount(loanAccount))
                .waitForValue(harness, newBranchCode)
                .then(tasks.storeClientAccountBranchCode());
        harness.withPaymentEngine().closePaymentEngine();

        String branchCode1 = harness.get(PaymentEngineClientsListOfClientsAccounts.BRANCH_CODE_KEY);
        Assertions.assertEquals(newBranchCode, branchCode1);

        changeClientsLoanAccountBranchCode(harness, loanAccount, oldBranchCode);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchClientAccount(loanAccount))
                .waitForValue(harness, oldBranchCode)
                .then(tasks.storeClientAccountBranchCode());
        harness.withPaymentEngine().closePaymentEngine();

        String branchCode2 = harness.get(PaymentEngineClientsListOfClientsAccounts.BRANCH_CODE_KEY);
        Assertions.assertNotEquals(newBranchCode, branchCode2, "The account's branch code does not equal the previous one after the change");

        PaymentEngineUtils.logChangeOfClientAccountBranchCode(harness, loanAccount, branchCode1, branchCode2);
    }
}