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

import cz.moneta.test.dsl.Harness;
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.dsl.util.task.cagw.api.PaymentTasks;
import cz.moneta.test.harness.annotations.*;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;

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

@SuppressWarnings("unchecked")
@NonConcurrentTestScenario(name = "Create Foreign payment via CA GW, verify it in ib")
public class Psd2CaGwCreateForeignPaymentTest {

    @Deprecated
    @Disabled("Obsolete, to be reviewed.")
    @TestCase(name = "End to end testcase create customer and then create payment via CA GW", environments = {TST1})
    @JiraTestCase(id = "AO-T427")
    public void tc_create_payment_via_ca_gw_psd2(Harness harness, @Key("api.client-with-allowed-international-payments") Credentials client) {
        String mobileKey = "12341234";
        String currencyAmount = "USD";
        String ibanDebtorAccount = "CZ4006000000000191684002";
        String identificationCreditAccount = "400789392838";
        String oauth2ClientId = "l7xx91bb88abcbda4b0db4cc8906803d56c4";
        String oauth2ClientSecret = "2f43959c06f14c4f9b60c1776b7f0b7b";
        String requestedExecutionDate = TestUtils.getTodayDate("yyyy-MM-dd");
        String unstructured = "Zahranicni platba z automatu pres CAGW";
        String chargeBearer = "OUR";
        int valueAmount = 1;
        String transactionConfirmation = "mailer@mailme.com";
        String nameCreditorAgent = "INTESA SANPAOLO SPA";
        String addressLineCreditorAgent = "1 QUEENS ROAD CENTRAL";
        String countryCreditorAgent = "HK";
        String bicCreditorAgent = "HSBCHKHHHKH";
        String nameCreditor = "SHENZHEN SHENZE ELECTRONIC CO.";
        String addressLineCreditor = "HONGKONG";
        String transactionsType = " Zahraniční příkaz";
        PaymentTasks paymentTasks = harness.tasks().caGw().api().payment();


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

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

        String transId = harness.withCaGw()
                .api().cbl().psd().pisp().my()
                .preparePaymentRequest()
                .log("Transaction id is: {}", r -> r.getPaymentIdentification().getTransactionIdentification())
                .then(paymentTasks.setRequestValuesForForeignPayment(authToken, currencyAmount, valueAmount, chargeBearer,
                        nameCreditor, addressLineCreditor, identificationCreditAccount, nameCreditorAgent,
                        bicCreditorAgent, addressLineCreditorAgent, countryCreditorAgent, ibanDebtorAccount,
                        unstructured, requestedExecutionDate, transactionConfirmation))
                .send()
                .getPaymentIdentification()
                .getTransactionIdentification();

        harness.withDmbsIb()
                .onDmbsIbTransitions()
                .openLoginPage(transId)
                .then(harness.tasks().dmbsib().login(client.getUsername(), client.getPassword(), mobileKey))
                .clickSendMobileKey()
                .fillMobileKey(mobileKey)
                .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"));

        Result<Record> records = harness.withUdebs()
                .fromWPsd2Tokens()
                .selectFromW_psd2_tokens(transId);

        String packageId = records.get(0).getValue("PACKAGEID").toString();
        harness.log(records.format());

        harness.withIB()
                .openLoginPage()
                .then(harness.tasks().ib().loginWithUsernamePasswordAndMobileKey(client.getUsername(), client.getPassword(), mobileKey))
                .onWelcomePage()
                .clickAccountsAndTransactions()
                .clickPaymentOrders()
                .selectTransactionsTypes(transactionsType)
                .fillDateFrom(TestUtils.getTodayDate())
                .clickDisplayTransactions()
                .clickOrderTransactionsById()
                .clickOrderTransactionsById()
                .checkElementContentPackageId(packageId);
    }
}