package cz.moneta.test.regression.data.api.ufobanka;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.ufo.banka.tasks.accountenums.CurrentAccountsEnum;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.auth.AuthSupport;
import org.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;

import java.text.SimpleDateFormat;

import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.CLIENT_CIF_STORE_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.CURRENT_ACCOUNT_NUMBER;
import static cz.moneta.test.dsl.ufo.banka.pages.main.adc.ADC026_S001_BankApiDetailPage.THIRD_PARTY_ACTIVE_DATE_FROM;
import static cz.moneta.test.dsl.ufo.banka.pages.main.adc.ADC026_S001_BankApiDetailPage.THIRD_PARTY_HISTORY_DATE_FROM;
import static cz.moneta.test.dsl.ufo.banka.pages.main.adc.ADC026_S001_BankApiDetailPage.THIRD_PARTY_HISTORY_DATE_TO;
import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.banker;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenario(name = "UFOBanka - action with Authorization CISP")
public class AuthorizationCispUfoBanka {

    private static final String THIRD_PARTY_NUMBER = "111456121";
    private static final String THIRD_PARTY_NAME = "Testovaci klient";

    @Deprecated
    @Disabled("Obsolete, to be reviewed.")
    @TestCase(name = "Enable PSD2 Cisp via UFOBanka", environments = {TST1})
    @JiraTestCase(id = "AO-T433")
    public void tc_enable_psd2_cisp_via_ufobanka(Harness harness) {

        BankaTasks tasks = harness.tasks().ufo().banka();
        NonClient nonClient = harness.data().newNonClient();
        String bankerUsername = AuthSupport.getCredentials(banker, harness).getUsername();

        harness.withUfo()
                .openLoginPage()
                .then(tasks.login().asBanker())
                .then(tasks.login().turnSignpadOff())
                .then(tasks.newClient().create(nonClient, bankerUsername))
                .then(tasks.newPackagedProductTasks().ibWithMobileKeyAndCurrentAccount(nonClient, CurrentAccountsEnum.GENIUS_FREE_FLEXI))
                .getCurrentAccountNumberAndStore()
                .clickInternetBankWithMobileKey()
                .activationBankApiChannel()
                .acceptAlert()
                .clickGenerateLater()
                .onMainClientPage()
                .clickBankApi()
                .editBankApiChannel()
                .selectThirdParty(THIRD_PARTY_NAME + "|" + THIRD_PARTY_NUMBER)
                .selectAccount(harness.get(CURRENT_ACCOUNT_NUMBER))
                .activateCisp()
                .clickRightButton()
                .onBankApiDetailPage()
                .checkThirdPartyTableAccount(harness.get(CURRENT_ACCOUNT_NUMBER))
                .checkThirdPartyTableNumber(THIRD_PARTY_NUMBER)
                .checkThirdPartyTableName(THIRD_PARTY_NAME)
                .storeThirdPartyDateFrom()
                .selectThirdParty(THIRD_PARTY_NAME + "|" + THIRD_PARTY_NUMBER)
                .selectAccount(harness.get(CURRENT_ACCOUNT_NUMBER))
                .activateCispValidationMessage()
                .checkAlertAndAcceptActivation();

        Result<Record> records = harness.withUdebs()
                .thirdParty()
                .getThirdPartyRights("0" + harness.get(CURRENT_ACCOUNT_NUMBER));

        Assertions.assertEquals(records.get(0).getValue(0), THIRD_PARTY_NUMBER);
        Assertions.assertEquals(records.get(0).getValue(3), harness.get(CLIENT_CIF_STORE_KEY));
        Assertions.assertEquals(new SimpleDateFormat("d.M.yyyy H:mm:ss")
                .format(records.get(0).getValue(1)), harness.get(THIRD_PARTY_ACTIVE_DATE_FROM));
        Assertions.assertNull(records.get(0).getValue(2));
    }

    @Deprecated
    @Disabled("Obsolete, to be reviewed.")
    @TestCase(name = "Diseable PSD2 Cisp via UFOBanka", environments = {TST1})
    @JiraTestCase(id = "AO-T434")
    public void tc_diseable_psd2_cisp_via_ufobanka(Harness harness) {

        BankaTasks tasks = harness.tasks().ufo().banka();
        NonClient nonClient = harness.data().newNonClient();
        String bankerUsername = AuthSupport.getCredentials(banker, harness).getUsername();

        harness.withUfo()
                .openLoginPage()
                .then(tasks.login().asBanker())
                .then(tasks.login().turnSignpadOff())
                .then(tasks.newClient().create(nonClient, bankerUsername))
                .then(tasks.newPackagedProductTasks().ibWithMobileKeyAndCurrentAccount(nonClient, CurrentAccountsEnum.GENIUS_FREE_FLEXI))
                .getCurrentAccountNumberAndStore()
                .clickInternetBankWithMobileKey()
                .activationBankApiChannel()
                .acceptAlert()
                .clickGenerateLater()
                .onMainClientPage()
                .clickBankApi()
                .editBankApiChannel()
                .selectThirdParty(THIRD_PARTY_NAME + "|" + THIRD_PARTY_NUMBER)
                .selectAccount(harness.get(CURRENT_ACCOUNT_NUMBER))
                .activateCisp()
                .clickRightButton()
                .onBankApiDetailPage()
                .selectFirstRow()
                .deactivateCisp()
                .clickRightButton()
                .onBankApiDetailPage()
                .historyCispSettings()
                .checkHistoryTableAccount(harness.get(CURRENT_ACCOUNT_NUMBER))
                .checkHistoryTableNumber(THIRD_PARTY_NUMBER)
                .checkHistoryTableName(THIRD_PARTY_NAME)
                .storeThirdPartyHistoryDates()
                .closeThirdPartyHistory()
                .deactivateCispValidationMessage()
                .checkAlertAndAcceptDeactivate();

        Result<Record> records = harness.withUdebs()
                .thirdParty()
                .getThirdPartyRights("0" + harness.get(CURRENT_ACCOUNT_NUMBER));

        Assertions.assertEquals(records.get(0).getValue(0), THIRD_PARTY_NUMBER);
        Assertions.assertEquals(records.get(0).getValue(3), harness.get(CLIENT_CIF_STORE_KEY));
        Assertions.assertEquals(new SimpleDateFormat("d.M.yyyy H:mm:ss")
                .format(records.get(0).getValue(1)), harness.get(THIRD_PARTY_HISTORY_DATE_FROM));
        Assertions.assertEquals(new SimpleDateFormat("d.M.yyyy H:mm:ss")
                .format(records.get(0).getValue(2)), harness.get(THIRD_PARTY_HISTORY_DATE_TO));
    }
}