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.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.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.Assertions;

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

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

    @TestCase(name = "End to end testcase create customer and then create payment via CA GW", environments = {TST1})
    @JiraTestCase(id = "AO-T426")
    public void tc_create_payment_via_ca_gw_psd2(Harness harness, @Key("api.client") Credentials client) {
        String mobileKey = "12341234";
        String currencyAmount = "CZK";
        String identificationDebtorAccount = "214789641/0600";
        String ibanCreditorAccount = "CZ0735000000001000377902";
        String oauth2ClientId = "l7xx91bb88abcbda4b0db4cc8906803d56c4";
        String oauth2ClientSecret = "2f43959c06f14c4f9b60c1776b7f0b7b";
        String requestedExecutionDate = TestUtils.getTodayDate("yyyy-MM-dd");
        String unstructured = "Domaci platba z automatu pres CAGW";
        String reference = "VS:1111111111,KS:2222222222,SS:3333333333";
        int valueAmount = 5;
        String transactionsType = "Domácí 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.setRequestValuesForDomesticPayment(authToken, currencyAmount, valueAmount,
                        ibanCreditorAccount, identificationDebtorAccount, reference, unstructured, requestedExecutionDate))
                .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()
                .fillDateFrom(TestUtils.getTodayDate())
                .selectTransactionsTypes(transactionsType)
                .clickDisplayTransactions()
                .clickOrderTransactionsById()
                .clickOrderTransactionsById()
                .checkElementContentPackageId(packageId);
    }
}