package cz.moneta.test.dataprepare.retail;

import cz.moneta.test.dataprepare.risk.DefaultClientPrepare;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.szrmock.SzrMockData;
import cz.moneta.test.dsl.util.data.client.NewClientWithCurrentAccountInformation;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import org.junit.jupiter.api.BeforeAll;

import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.banker;
import static cz.moneta.test.dsl.util.task.retail.loans.constants.DefaultLoansConstants.*;
import static cz.moneta.test.harness.support.auth.AuthSupport.getCredentials;

@TestScenario(name = "Creating client via WSO2.")
public class Exists extends DefaultClientPrepare {

    static String ufoUserName;

    @BeforeAll
    public void init(Harness harness) {
        ufoUserName = getCredentials(banker, harness).getUsername();
    }

    @TestCase(name = "Cif Only")
    public void newClientCif(Harness harness) {
        NonClient nonClient = createNonClient(harness);
        String cif = harness.dataPrepare().createNewFoCustomerViaWso2AndGetCif(nonClient, ufoUserName);
        logClientData(harness, nonClient, cif);
    }

    @TestCase(name = "Client Cif,BU")
    public void newClientBU(Harness harness) {
        NonClient nonClient = createNonClient(harness);
        NewClientWithCurrentAccountInformation info = harness.dataPrepare().createCifWithCurrentAccountApiSequence(ufoUserName, nonClient);
        logClientData(harness, nonClient, info.getCif());
    }

    @TestCase(name = "Client Cif,BU,IB")
    public void newClientADC(Harness harness) {
        NonClient nonClient = createNonClient(harness);
        NewClientWithCurrentAccountInformation info = harness.dataPrepare().createNewFoCustomerWithCurrentAccountAndIbViaWso2(nonClient, ufoUserName);
        logClientData(harness, nonClient, info.getCif());
        harness.log(String.format("IB username: %s",info.getCredentials().getUsername()));
        harness.log(String.format("IB password: %s",info.getCredentials().getPassword()));
    }
    @TestCase(name = "Client Cif,BU,IB")
    public void newClientADCWithSZR(Harness harness) {
        NonClient nonClient = createNonClient(harness);
        NewClientWithCurrentAccountInformation info = harness.dataPrepare().createNewFoCustomerWithCurrentAccountAndIbViaWso2(nonClient, ufoUserName);
        SzrMockData szrData = SzrMockData.builder()
                .client(nonClient)
                .cif(info.getCif())
                .build();
        harness.tasks().szrMock().idAndPassE2E(szrData);
        logClientData(harness, nonClient, info.getCif());
        harness.log(String.format("IB username: %s",info.getCredentials().getUsername()));
        harness.log(String.format("IB password: %s",info.getCredentials().getPassword()));
    }

    @TestCase(name = "Client Cif,BU,BU,IB")
    public void newClientDoubleCurrentAccount(Harness harness) {
        NonClient nonClient = createNonClient(harness);
        NewClientWithCurrentAccountInformation info = harness.dataPrepare().createNewFoCustomerWithCurrentAccountAndIbViaWso2(nonClient, ufoUserName);
        logClientData(harness, nonClient, info.getCif());
        harness.log(String.format("IB username: %s",info.getCredentials().getUsername()));
        harness.log(String.format("IB password: %s",info.getCredentials().getPassword()));
    }

    private NonClient createNonClient(Harness harness) {
        return harness.data().newNonClient()
                .withIdCard(DEFAULT_ID_CARD)
                .withBirthNumber(harness.tasks().common().getRcMinMaxAge(20, 50))
                .withResidencyAddress(DEFAULT_CLIENT_ADDRESS)
                .withEmail(DEFAULT_EMAIL)
                .withFixPhoneNumberWork("603235689")
                .withFixPhoneNumberHome("603235689")
                .withMobileNumber("603235689")
                .withNetMonthlyIncome(DEFAULT_INCOME)
                .withHomeIncome(DEFAULT_INCOME);
    }

    private void logClientData(Harness harness, NonClient nonClient, String cif) {
        harness.log(String.format("Name and Surname: %s %s", nonClient.firstName(), nonClient.lastName()));
        harness.log(String.format("Birth Number: %s", nonClient.birthNumber()));
        harness.log(String.format("Cif: %s", cif));
    }
}