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.oauth2.token.Oauth2TokenResponse;
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.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.Disabled;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

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

@SuppressWarnings("unchecked")
@NonConcurrentTestScenario(name = "Check client accounts, account balance and verify it in UFO Banka")
public class Psd2CaGwGetAccountInformation {

    private static final String PATTERN = "###,###.##";
    private static final String BIRTH_NUMBER = "8202128627";
    private static final String OAUTH_2_CLIENT_SECRET = "2f43959c06f14c4f9b60c1776b7f0b7b";
    private static final String OAUTH_2_CLIENT_ID = "l7xx91bb88abcbda4b0db4cc8906803d56c4";
    private static final String MOBILE_KEY = "12341234";

    @Deprecated
    @Disabled("Obsolete, to be reviewed.")
    @TestCase(name = "Check client accounts, account balance - end to end testcase via CA GW", environments = {TST1})
    @JiraTestCase(id = "AO-T423")
    public void tc_check_accounts_via_ca_gw_psd2(Harness harness, @Key("api.client") Credentials client) {

        harness.withOauth2Web()
                .onOauth2WebTransitions()
                .openApproveAccessToAccountPage(OAUTH_2_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(OAUTH_2_CLIENT_ID, OAUTH_2_CLIENT_SECRET))
                .send()
                .getAccess_token();

        //AISP - GET /psd/aisp/my/accounts?sort=&order=
        Accounts account = harness.withCaGw()
                .api().cbl().psd().aisp().my()
                .prepareAccountsWithParamsRequest()
                .then(harness.tasks().caGw().api().accounts().setRequestValuesAccountsWithParams(authToken,
                        "identification.other", "asc"))
                .send()
                .getAccounts()[0];

        String accountNumber = account.getIdentification().getOther();
        accountNumber = accountNumber.substring(0, accountNumber.indexOf("/"));
        harness.log("Account number is: " + accountNumber);

        String accountId = account.getId();

        //AISP - GET /psd/aisp/my/accounts/{id}/balance
        double amountValue = harness.withCaGw()
                .api().cbl().psd().aisp().my().accounts().id()
                .prepareBalanceRequest()
                .then(harness.tasks().caGw().api().balance().setRequestValues(authToken, accountId))
                .send()
                .getBalances()[0].getAmount().getValue();


        DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(new Locale("cs_CZ"));
        formatSymbols.setDecimalSeparator(',');
        formatSymbols.setGroupingSeparator(' ');

        DecimalFormat decimalFormat = new DecimalFormat(PATTERN, formatSymbols);
        String formatAmountValue = decimalFormat.format(amountValue);
        harness.log("Balance of account is: " + formatAmountValue);

        harness.withUfo()
                .openLoginPage()
                .then(harness.tasks().ufo().banka().login().asBanker())
                .fillSearchTerm(BIRTH_NUMBER)
                .clickSearchByRc()
                .onClientDetailPage()
                .checkProductActive(accountNumber)
                .checkProductActive(formatAmountValue);
    }
}