package cz.moneta.test.regression.transactional.cards.retail.creditcard.origination;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.HPO019_S001_CreditCardDetailPage;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.annotations.Environment;
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.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;

import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.CreditCardsAdvantages.TRAVEL_AND_SPORT;
import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.CreditCardsPromo.NO_PROMO;
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.tasks.accountenums.CurrentAccountsEnum.GENIUS_FREE_FLEXI;
import static cz.moneta.test.dsl.util.credentials.Keys.ufo;

@TestScenario(name = "ONB.RET.CC.01 - Retail CC - Application Origination - MoneyCard Smart - BRANCH")
public class Client_inBranch_ApplicationOnly {

    NonClient nonClient;
    private static final double DELTA = 1e-4;

    @BeforeEach
    void setUp(Harness harness, @Key(ufo.banka.banker) Credentials credentials) {
        nonClient = harness.data().newFVNonClient()
                .withNetMonthlyIncome(100000)
                .withHomeIncome(100000)
                .withOtherExpenses(5000)
                .withOtherLoanPayments(0);

        BankaTasks bankaTasks = harness.tasks().ufo().banka();
//TODO MWF CIF create, remove notes after it works in TST3
//        IHubTasks iHubTasks = new IHubTasks(harness);
//        harness.store(CLIENT_CIF_STORE_KEY, iHubTasks.createCifFo(nonClient));
//        harness.log("New CIF was created via iHub CIF is: " + harness.get(CLIENT_CIF_STORE_KEY));
//        harness.withUfo()
//                .openLoginPage()
//                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
//                .fillSearchTerm(harness.get(CLIENT_CIF_STORE_KEY))
//                .clickSearchByCif()
//                .onClientDetailPage()
//                .then(bankaTasks.newProduct().ibWithMobileKey(GENIUS_FREE_FLEXI));

//TODO UFO CIF create, remove this code after add MWF CIF create
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.newClient().create(nonClient, credentials.getUsername()))
                .then(bankaTasks.newPackagedProductTasks().ibWithMobileKeyAndCurrentAccount(nonClient, GENIUS_FREE_FLEXI));
    }

    @JiraTestCase(id = "FVP-T13")
    @TestCase(name = "Retail Customer wants to get his Credit Card on Moneta BRANCH and he will complete origination process on Branch", environments = {Environment.TST3})
    public void Retail_CC_Application_Origination_MoneyCard_Smart_BRANCH(Harness harness, @Key(ufo.banka.banker) Credentials credentials) {

        String creditAmount = "50000";

        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .fillSearchTerm(harness.get(CLIENT_CIF_STORE_KEY))
                .clickSearchByCif()
                .onClientDetailPage()
                .then(bankaTasks.newProduct().createNewCreditCardByQuickRequest(nonClient, TRAVEL_AND_SPORT, NO_PROMO, creditAmount))
                .openProductDetail("MONEYCARD SMART")
                .onCreditCardDetailPage()
                .storeCreditCardValues()
                .clickBackToClient()
                .selectFromListOfRequests()
                .checkSelectedLinesTextContent("Credit card", "PODEPSANÁ", "50\u00a0000"); //UFO is using &nbsp; instead of space, this sucks, I know.

        double actualCreditAmount = TestUtils.toNumber(harness.get(HPO019_S001_CreditCardDetailPage.TOTAL_CREDIT_STORE)).doubleValue();
        double actualCreditforCashWithdraw = TestUtils.toNumber(harness.get(HPO019_S001_CreditCardDetailPage.CREDIT_FOR_CASH_WITHDRAV_STORE)).doubleValue();

        Assertions.assertEquals(TestUtils.toNumber(creditAmount).doubleValue(), actualCreditAmount, DELTA);
        Assertions.assertEquals(TestUtils.toNumber(creditAmount).doubleValue(), actualCreditforCashWithdraw, DELTA);
        Assertions.assertEquals("Inkasem z účtu", harness.get(HPO019_S001_CreditCardDetailPage.METHOD_OF_PAYMENT_STORE_KEY));
    }
}
