package cz.moneta.test.regression.risk;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.CardBrand;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.HPO019_S001_CreditCardDetailPage;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.ProductType;
import cz.moneta.test.dsl.ufo.banka.pages.ib.HPO008_IbDetailPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP006_S001_ExpressScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.OPN008_S001_ExpressSignaturePage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.flexikredit.NFL006_S001_FlexikreditScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.util.code.lists.HCisAllStavZadosti;
import cz.moneta.test.dsl.util.data.ClientUtils;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.annotations.Defect;
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.util.credentials.Keys.ufo.banka.banker;
import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.secondBanker;

@TestScenario(name = "Test for Risk RETAIL & SMALL BUSINESS RISK - UNSECURED PRODUCTS")
public class RiskRetailTests {

    private BankaTasks bankaTasks;
    private NonClient applicant;
    private final String CARD_ACTIVATED = "Aktivovaná karta";
    private final String ID_CARD_TYPE = "OP";
    private final int DEFAULT_LOAN_AMOUNT = 50000;
    private final int DEFAULT_FLEXI_LOAN_AMOUNT = 30000;
    private final int DEFAULT_TERM = 60;
    private final int SHORT_TERM = 24;
    private final String SECOND_BANKER_NAME = "TestJB Harness";//zatím voláno pouze lokálně

    @BeforeEach
    public void setup(Harness harness) {
        bankaTasks = harness.tasks().ufo().banka();
        applicant = ClientUtils.getRiskDefaultClient(harness);
    }

    //6  CIF + BU + IBS + KK MC Smart a na ní žádost o Clip přes IBS- todo
    @Defect("Not finished")
    @TestCase(name = "IBS Credit Card clip")
    public void newClientWithIbsCreditCardClip(Harness harness, @Key(banker) Credentials credentials) {

        bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant)
                .then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplication(applicant, DEFAULT_LOAN_AMOUNT, HCisAllStavZadosti.APPLICATION_CONDITIONALLY_APROVED))
                .then(bankaTasks.cardsOnboarding().creditCards().createAccountsForNewCreditCardWithExistingIbAndBu(DEFAULT_LOAN_AMOUNT))
                .then(bankaTasks.cardsServicing().creditCards().activateCreditCardWithoutSignatureSelect(ProductType.MONEYCARD_SMART.getValue(), ID_CARD_TYPE, applicant.idCardNumber()))
                .then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplicationWithPrefilledData(DEFAULT_LOAN_AMOUNT));

        Assertions.assertEquals(CARD_ACTIVATED, harness.get(HPO019_S001_CreditCardDetailPage.PROD_STAT_STORE));
        //todo JBR - IBS clip
    }

    //7 žádost o product transfer přes POS z MC Smart (klidně stejná KK z bodu 4) na Kreditní účet - todo
    @Defect("Not working in UFO") //todo JBR
    @TestCase(name = "New client with Credit Card Product Transfer")//8
    public void newClientWithCreditCardProductTransfer(Harness harness, @Key(banker) Credentials credentials) {

        ProductType productType = ProductType.CREDIT_ACCOUNT;
        CardBrand cardBrand = CardBrand.VISA;

        bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant)
                .then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplicationDetailed(applicant, DEFAULT_LOAN_AMOUNT, productType, cardBrand, HCisAllStavZadosti.APPLICATION_CONDITIONALLY_APROVED))
                .then(bankaTasks.cardsOnboarding().creditCards().createAccountsForNewCreditCardWithExistingIbAndBu(DEFAULT_LOAN_AMOUNT))
                .then(bankaTasks.cardsServicing().creditCards().activateCreditCardWithoutSignatureSelect(productType.getValue(), ID_CARD_TYPE, applicant.idCardNumber()))
                .then(bankaTasks.cardsOnboarding().creditCards().createProductTransferApplication(productType.getValue(), applicant));

        //Assertions.assertEquals(expectedCardStat, harness.get(HPO019_CreditCardDetailPage.PROD_STAT_STORE));
    }

    @TestCase(name = "New client with two Credit Card applications")//8
    public void newClientWithTwoCreditCardApplications(Harness harness, @Key(banker) Credentials credentials) {

        ProductType productType = ProductType.CREDIT_ACCOUNT;
        CardBrand cardBrand = CardBrand.VISA;

        bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant)
                .then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplicationDetailed(applicant, DEFAULT_LOAN_AMOUNT, productType, cardBrand, HCisAllStavZadosti.APPLICATION_CONDITIONALLY_APROVED))
                .then(bankaTasks.cardsOnboarding().creditCards().createAccountsForNewCreditCardWithExistingIbAndBu(DEFAULT_LOAN_AMOUNT))
                .then(bankaTasks.cardsServicing().creditCards().activateCreditCardWithoutSignatureSelect(productType.getValue(), ID_CARD_TYPE, applicant.idCardNumber()))
                .then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplicationWithPrefilledData(DEFAULT_LOAN_AMOUNT));

        Assertions.assertEquals(CARD_ACTIVATED, harness.get(HPO019_S001_CreditCardDetailPage.PROD_STAT_STORE));

    }

    //   9.	žádost o 1. KK (jakýkoliv typ) s externí UVA vyšší než 500 tis Kč
    @Defect("Not finished")//todo JBR
    @TestCase(name = "New client with external UVA > 500K Kc")//9
    public void newClientWithExternalUva(Harness harness, @Key(banker) Credentials credentials) {
        HKO101_S001_MainClientPage window = bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant);

        //set external UVA > 500

        window.then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplication(applicant, DEFAULT_LOAN_AMOUNT));

    }

    @Defect("Not finished")//todo JBR
    @TestCase(name = "New client with scorecard CCON01S1 and score 483-528")//10
    public void newClientWithScorecardCcon01s1(Harness harness, @Key(banker) Credentials credentials) {

        HKO101_S001_MainClientPage window = bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant);

        //set scorecard CCON01S1 in ILODS

        window.then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplication(applicant, DEFAULT_LOAN_AMOUNT));

    }

    @Defect("Not finished")//todo JBR
    @TestCase(name = "New client with scorecard CCON01S2 and score 483-528")//11
    public void newClientWithScorecardCcon01s2(Harness harness, @Key(banker) Credentials credentials) {

        HKO101_S001_MainClientPage window = bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant);

        //set scorecard CCON01S2 in ILODS

        window.then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplication(applicant, DEFAULT_LOAN_AMOUNT));
    }

    @TestCase(name = "New client with second Express loan application")//14
    public void newClientWithTwoExpressLoans(Harness harness, @Key(banker) Credentials credentials, @Key(secondBanker) Credentials secondCredentials) {
        int firstLoanAmount = 150000;

        OPN008_S001_ExpressSignaturePage firstBankerWindow =
                bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant)
                        .then(bankaTasks.newProduct().createSBA())
                        .then(bankaTasks.expresTasks().createNewExpressApplication(applicant, firstLoanAmount, DEFAULT_TERM, HCisAllStavZadosti.APPLICATION_CONDITIONALLY_APROVED))
                        .then(bankaTasks.expresTasks().createAccountsForNewExpressUntilSignature(SECOND_BANKER_NAME));

        bankaTasks.newProduct().openUfoAndVerifyApplicationAsSecondBanker(secondCredentials, harness.get(NEP006_S001_ExpressScoringResultPage.APP_ID_STORE));
        firstBankerWindow
                .then(bankaTasks.expresTasks().finishAccountCreationAfterSecondBankerSignature(HCisAllStavZadosti.LOAN_DRAWN))
                .then(bankaTasks.expresTasks().createNewExpressApplicationWithExistingAndPrefilledData(DEFAULT_LOAN_AMOUNT, SHORT_TERM, HCisAllStavZadosti.APPLICATION_DENIED));
    }

    @Defect("Not finished")
    @TestCase(name = "FlexiCredit clip in IB")//19
    public void flexiCreditClipIb(Harness harness, @Key(banker) Credentials credentials) {

        String secondBankerName = "Šubrt";// "Gelnar";

        OPN008_S001_ExpressSignaturePage firstBankerWindow =
                bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant)
                        .then(bankaTasks.flexiCreditTasks().createNewFlexiCreditApplication(applicant, DEFAULT_FLEXI_LOAN_AMOUNT, HCisAllStavZadosti.APPLICATION_CONDITIONALLY_APROVED))
                        .then(bankaTasks.flexiCreditTasks().createAccountsForNewFlexiCreditWithExistingBuAndIbUntilSignature(DEFAULT_FLEXI_LOAN_AMOUNT, secondBankerName));

        //second banker signature
        firstBankerWindow.then(bankaTasks.flexiCreditTasks().finishAccountCreation(HCisAllStavZadosti.LOAN_ACCOUNT_OPEN))
                .clickActiveProduct("Internet Banka")
                .onIbDetailPage()
                .storeIbLogin()
                .clickBackButton();

        harness.withIB().openLoginPage()
                .typeUsername(HPO008_IbDetailPage.IB_LOGIN_STORE);
        // .then(harness.tasks().ib().loginWithUsernamePasswordAndMobileKey(harness.get(HPO008_IbDetailPage.IB_LOGIN_STORE), "jJMdR68h", "12341234"));

    }

    @Defect("Not finished")
    @TestCase(name = "FlexiCredit declip") //22
    public void flexiCreditDeclip(Harness harness, @Key(banker) Credentials credentials, @Key(secondBanker) Credentials secondCredentials) {

        //9509149617
        int declipLoanAmount = 20000;

        OPN008_S001_ExpressSignaturePage firstBankerWindow =
                bankaTasks.newProduct().createClientWithIb(harness, credentials, applicant)
                        .then(bankaTasks.flexiCreditTasks().createNewFlexiCreditApplication(applicant, DEFAULT_FLEXI_LOAN_AMOUNT, HCisAllStavZadosti.APPLICATION_CONDITIONALLY_APROVED))
                        .then(bankaTasks.flexiCreditTasks().createAccountsForNewFlexiCreditWithExistingBuAndIbUntilSignature(DEFAULT_FLEXI_LOAN_AMOUNT, SECOND_BANKER_NAME));

        bankaTasks.newProduct().openUfoAndVerifyApplicationAsSecondBanker(secondCredentials, harness.get(NFL006_S001_FlexikreditScoringResultPage.APP_ID_STORE));        //wait for second banker signature

        firstBankerWindow.then(bankaTasks.flexiCreditTasks().finishAccountCreation(HCisAllStavZadosti.LOAN_ACCOUNT_OPEN))
                .clickActiveProduct("FLEXIKREDIT")
                .onFlexiCreditDetailPage()
                .clickDeclipButton()
                .fillLoanAmount(declipLoanAmount)
                .clickSendButton()
                .clickEnd()
                .onFlexiCreditDetailPage()
                .clickBackButton();
    }

    @Defect("Not finished")
    @TestCase(name = "FlexiCredit for Slovak citizen") //23
    public void flexiCreditSlovakCitizen(Harness harness, @Key(banker) Credentials credentials) {

        //OPN008_ExpressSignaturePage firstBankerWindow =
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndSelectOmByOrderAndTurnOffSignpad(credentials, 1));//todo JBR - finish

        //      firstBankerWindow.then(bankaTasks.flexiCreditTasks().finishAccountCreation(expectedAppStat));
    }

}
