package cz.moneta.test.regression.data.api.psd2.aisp;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.cagw.api.cbl.psd.aisp.my.accounts.AccountsResponse.Accounts;
import cz.moneta.test.dsl.cagw.api.cbl.psd.aisp.my.accounts.id.transactions.TransactionsResponse.Transactions;
import cz.moneta.test.dsl.cagw.oauth2.token.Oauth2TokenResponse;
import cz.moneta.test.dsl.dmbsib.ThirdPartyApplicationPageWithStatusCode;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.NonConcurrentTestScenario;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.Assertions;
import cz.moneta.test.harness.support.auth.Credentials;
import org.junit.jupiter.api.Disabled;

import java.util.Date;

import static cz.moneta.test.harness.annotations.Environment.TST1;

@SuppressWarnings("unchecked")
@NonConcurrentTestScenario(name = "Check client transactions via CA GW - PSD2")
public class Psd2CaGwGetAccountTransactions {

    private static final String MOBILE_KEY = "12341234";
    private static final String OAUTH2_CLIENT_ID = "l7xx91bb88abcbda4b0db4cc8906803d56c4";
    private static final String OAUTH_2_CLIENT_SECRET = "2f43959c06f14c4f9b60c1776b7f0b7b";
    private static final String DEBTOR_ACCOUNT_NUMBER = "214789641/0600";
    private static final String CREDIT_ACCOUNT_IBAN = "CZ0735000000001000377902";
    private static final String ACTUAL_DATE = TestUtils.getTodayDate("yyyy-MM-dd");
    private static final String CURRENCY = "CZK";
    private static final int PAYMENT_AMOUNT = 1;
    private static final String VS = (String.valueOf(new Date().getTime())).substring(0, 10);
    private static final String KS = "2222222222";
    private static final String SS = "3333333333";
    private static final String REFERENCE = "VS:" + VS + ",KS:" + KS + ",SS:" + SS;
    private static final String UNSTRUCTURED = "Domaci platba z automatu pres CAGW";

    @Deprecated
    @Disabled("Obsolete, to be reviewed.")
    @TestCase(name = "Check client transactions via PSD2 - end to end testcase via CA GW with create new payment," +
            " booking and check via PSD2 api", environments = {TST1})
    @JiraTestCase(id = "AO-T424")
    public void tc_check_accounts_via_ca_gw_psd2(Harness harness, @Key("api.client") Credentials client) {

        harness.withOauth2Web()
                .onOauth2WebTransitions()
                .openApproveAccessToAccountPage(OAUTH2_CLIENT_ID)
                .then(harness.tasks().oauth2().web().loginToApproveAccessPage(client.getUsername(), client.getPassword()))
                .fillSmsKey(MOBILE_KEY)
                .clickApproveAccessButton()
                .getAuthTokenAndStoreIt();

        String authToken = harness.withCaGw()
                .oauth2().token()
                .prepareOauth2TokenRequest()
                .log("Oauth2 token is: {}", Oauth2TokenResponse::getAccess_token)
                .then(harness.tasks().oauth2().token().setRequestValues(OAUTH2_CLIENT_ID, OAUTH_2_CLIENT_SECRET))
                .send()
                .getAccess_token();

        //AISP - GET /psd/aisp/my/accounts
        Accounts account = harness.withCaGw()
                .api().cbl().psd().aisp().my()
                .prepareAccountsRequest()
                .then(harness.tasks().caGw().api().accounts().setRequestValues(authToken))
                .send()
                .getAccounts()[0];

        String accountId = account.getId();

        //AISP - GET /psd/aisp/my/accounts/{id}/transactions
        harness.withCaGw()
                .api().cbl().psd().aisp().my().accounts().id()
                .prepareTransactionsRequest()
                .then(harness.tasks().caGw().api().transactions().setRequestValuesTransactions(authToken, accountId))
                .send();

        String transId = harness.withCaGw()
                .api().cbl().psd().pisp().my()
                .preparePaymentRequest()
                .log("Transaction id is: {}", r -> r.getPaymentIdentification().getTransactionIdentification())
                .then(harness.tasks().caGw().api().payment().setRequestValuesForDomesticPayment(authToken, CURRENCY,
                        PAYMENT_AMOUNT, CREDIT_ACCOUNT_IBAN, DEBTOR_ACCOUNT_NUMBER, REFERENCE, UNSTRUCTURED, ACTUAL_DATE))
                .send()
                .getPaymentIdentification()
                .getTransactionIdentification();

        harness.withDmbsIb()
                .onDmbsIbTransitions()
                .openLoginPage(transId)
                .then(harness.tasks().dmbsib().login(client.getUsername(), client.getPassword(), MOBILE_KEY))
                .clickSendMobileKey()
                .fillMobileKey(MOBILE_KEY)
                .verifyMobileKeyAndContinueToSuccessPage()
                .getAuthTokenAndStoreIt();

        Assertions.assertEquals("OK", harness.get(ThirdPartyApplicationPageWithStatusCode.STATUS_CODE_STORE));

        harness.withCaGw()
                .api().cbl().psd().pisp().my().payments().paymentId()
                .prepareStatusRequest()
                .then(harness.tasks().caGw().api().status().setRequestValues(authToken, transId))
                .then(harness.tasks().caGw().api().status().sendAndWaitForStatus("ACSC"));

        /*AISP - GET /psd/aisp/my/accounts/{id}/transactions?size={size}&page={page}&fromDate={fromDate}
        &toDate={toDate}&sort={sort}&order={order}*/
        Transactions transaction = harness.withCaGw()
                .api().cbl().psd().aisp().my().accounts().id()
                .prepareTransactionsWithParamsRequest()
                .then(harness.tasks().caGw().api().transactions().setRequestValuesTransactionsWithAllParams(authToken,
                        accountId, ACTUAL_DATE, ACTUAL_DATE, "1", "0", "date", "DESC"))
                .then(harness.tasks().caGw().api().transactions().sendAndWaitForTransaction((VS)))
                .getTransactions()[0];

        Assertions.assertEquals(PAYMENT_AMOUNT, transaction.getAmount().getValue());
        Assertions.assertEquals(CURRENCY, transaction.getAmount().getCurrency());
        Assertions.assertEquals("DBIT", transaction.getCreditDebitIndicator());
        Assertions.assertEquals(TestUtils.getTodayDate("yyyy-MM-dd"), transaction
                .getBookingDate().getDate());
        Assertions.assertEquals("PAYMENT_ORDER_DOMESTIC", transaction.getEntryDetails().getTransactionDetails()
                .getReferences().getClearingSystemReference());
        Assertions.assertEquals("PŘÍKAZ K ÚHRADĚ ", transaction.getEntryDetails().getTransactionDetails()
                .getReferences().getTransactionDescription());
        Assertions.assertEquals(DEBTOR_ACCOUNT_NUMBER, transaction.getEntryDetails().getTransactionDetails()
                .getRelatedParties().getDebtorAccount().getIdentification().getOther().getIdentification());
        Assertions.assertEquals(UNSTRUCTURED, transaction.getEntryDetails().getTransactionDetails()
                .getRemittanceInformation().getUnstructured());
        Assertions.assertEquals(VS, transaction.getEntryDetails().getTransactionDetails()
                .getReferences().getEndToEndIdentification());
        Assertions.assertEquals(KS, transaction.getEntryDetails().getTransactionDetails()
                .getReferences().getInstructionIdentification());
        Assertions.assertEquals(SS, transaction.getEntryDetails().getTransactionDetails()
                .getReferences().getPaymentInformationIdentification());
        Assertions.assertEquals(REFERENCE.replace(",", " "), transaction.getEntryDetails().getTransactionDetails()
                .getRemittanceInformation().getStructured().getCreditorReferenceInformation().getReference());
        Assertions.assertEquals(ACTUAL_DATE, transaction.getValueDate().getDate());
        Assertions.assertEquals(ACTUAL_DATE, transaction.getBookingDate().getDate());
    }
}