package cz.moneta.test.dataprepare.ml.hypos;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.hypos.tasks.HyposDbTasks;
import cz.moneta.test.dsl.util.data.account.AccountDetail;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;

import java.util.Map;

@TestScenario(name = "Create current account and Internet Bank login")
public class CreateIbAtomPrepare {

    @TestCase(name = "Create current account and Internet Bank login", environments = {Environment.TST1, Environment.PPE, Environment.FVE})
    public void createCaAndIbToHyposClient(Harness harness) {
        Map<String, String> personDetails = new HyposDbTasks(harness).getClientDetailsByPin(harness.getSystemOrConfigValue("yamlData"));

        harness.log("1/8 Started checking whether a current account exists (MWFEQ5)");
        String clientCurrentAccount = harness.tasks().hypos().hyposWso2Tasks().getCurrentAccountNumberIfExists(personDetails.get("CIF"));
        if (clientCurrentAccount == null) {
            harness.log("2/8 No current account exists yet. It will be created (CRT_ACCOUNT_ICB)");
            clientCurrentAccount = harness.tasks().wso2().client().createAccountAndGetAccountNumber(
                    personDetails.get("CIF"),
                    personDetails.get("FULL_NAME"),
                    new AccountDetail("1201004", "413"),
                    "GEBTHRZ");
        } else {
            harness.log("2/8 New current account was NOT created.");
        }

        harness.log("3/8 Started checking whether the client already exists in UDEBS (CHK_CLIENT_UDEBS_EXISTENCE)");
        boolean existClientInUdebs = harness.tasks().wso2().ib().existClientInUdebs(personDetails.get("CIF"));
        if (!existClientInUdebs) {
            harness.log("4/8 Client does not exist in UDEBS, will be created (MWFA02)");
            harness.tasks().hypos().hyposWso2Tasks().createClientInUdebs(
                    personDetails.get("CIF"),
                    personDetails.get("FIRST_NAME"),
                    clientCurrentAccount,
                    personDetails.get("LAST_NAME"),
                    personDetails.get("PIN"),
                    personDetails.get("FULL_NAME"));
        } else {
            harness.log("4/8 Client already exists in UDEBS - MWFA02 was not called.");
        }

        harness.log("5/8 Started creating IB (CRT_IB_ACCT)");
        harness.tasks().wso2().ib().createIb(
                harness.data().newNonClient().withMobileNumber("777888999"),
                personDetails.get("CIF"));

        harness.log("6/8 Started to acquire login name to IB (GET_EM_IB_LOGINNAME)");
        String clientIbLogin = harness.tasks().wso2().ib().getInternetBankLoginName(personDetails.get("CIF"));

        harness.log("7/8 Started reseting password to IB (password/get-detail)");
        harness.tasks().wso2().idm().resetIbUserPasswordByUsername(clientIbLogin);

        harness.log("8/8 Started inserting IB login credentials into contract's notifications (crt_pr_sdeleni)");
        harness.tasks().hypos().hyposWso2Tasks().insertInternetBankLoginToCommunication(
                personDetails.get("CTR_CODE"),
                personDetails.get("PIN"),
                personDetails.get("FULL_NAME"),
                clientIbLogin);
    }
}