package cz.moneta.test.dsl.hypos.tasks;

import com.fasterxml.jackson.databind.JsonNode;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.support.util.Template;

public class HyposWso2Tasks {
    private final Harness harness;
    public static final String userIdL2Hypos = "900600300";
    private final String tokenMortgages;
    public final String tokenUfo;

    public HyposWso2Tasks(Harness harness) {
        this.harness = harness;
        this.tokenMortgages = harness.tasks().wso2().authorization().getToken("MORTGAGES_HYP");
        this.tokenUfo = harness.tasks().wso2().authorization().getToken("UFO");
    }

    /**
     * 01/ Loads all clients´ products
     * FOR cycle to go through all JSON response objects and if current account found, extract it (see @return)
     *
     * @return clientCurrentAccount - parameter RS_OUACEX within the object same object as RS_OUTYPE=0098 of the API response = current account number
     */
    public String getCurrentAccountNumberIfExists(String clientCif) {
        String clientCurrentAccount = null;
        JsonNode responseBuExists = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mwf.legacy/HUB/MWFEQ5/001")
                .withHeader("USERID_L2", userIdL2Hypos)
                .withHeader("Authorization", "Bearer " + tokenMortgages)
                .withPayloadFromTemplate(Template.fromFilepath("system/wso2/mortgages_cluster/createInternetBankApis/MWFEQ5.json")
                        .setQuoted("CIF", clientCif))
                .post()
                .andAssertStatus(200)
                .extract("Data");

        for (int i = 0; i < responseBuExists.size(); i++) {
            if (responseBuExists.get(i).findValue("RS_OUTYPE").textValue().equals("0098")) {
                clientCurrentAccount = responseBuExists.get(i).findValue("RS_OUACEX").textValue();
                harness.log("Current account already exists. Its number is: " + clientCurrentAccount);
                break;
            }
        }
        return clientCurrentAccount;
    }

    /**
     * 04/ Creates a client in UDEBS, if clientUdebsExistence=FALSE
     */
    public void createClientInUdebs(String clientCif, String clientFirstName, String clientCurrentAccount, String clientLastName, String clientPin, String clientFullName) {
        harness.withWso2()
                .prepareRequest()
                .withPath("/t/mwf.legacy/MWFA02/001")
                .withHeader("USERID_L2", userIdL2Hypos)
                .withHeader("Authorization", "Bearer " + tokenUfo)
                .withPayloadFromTemplate(Template.fromFilepath("system/wso2/mortgages_cluster/createInternetBankApis/MWFAO2.json")
                        .setQuoted("CIF", clientCif)
                        .setQuoted("firstName", clientFirstName)
                        .setQuoted("lastName", clientLastName)
                        .setQuoted("currAcc", clientCurrentAccount)
                        .setQuoted("clientFullName", clientFullName)
                        .setQuoted("pin", clientPin))
                .post()
                .andAssertStatus(200);
        harness.log("Client in UDEBS was created.");
    }

    /**
     * 08/ Insert Internet Bank details to the contract tab Notifications (Sdeleni)
     */
    public void insertInternetBankLoginToCommunication(String contractId, String clientPin, String clientFullName, String clientIbLogin) {
        harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/CRT_PR_SDELENI/001")
                .withHeader("USERID_L2", userIdL2Hypos)
                .withHeader("Authorization", "Bearer " + tokenMortgages)
                .withPayloadFromTemplate(Template.fromFilepath("system/wso2/mortgages_cluster/createInternetBankApis/crt_pr_sdeleni.json")
                        .setQuoted("ContractId", contractId)
                        .setQuoted("Text", String.format("Login do IB pro RC %s (%s) je: %s", clientPin, clientFullName, clientIbLogin)))
                .post()
                .andAssertStatus(200);
        harness.log("Internet Bank login was saved into contract's notifications (Sdeleni).");
    }
}