package cz.moneta.test.dsl.util.task.wso2.digitalchannels;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import cz.moneta.test.harness.support.util.Template;

import static cz.moneta.test.dsl.util.data.ihub.IHubConstants.MMB_BBC_OM;
import static cz.moneta.test.dsl.util.data.ihub.IHubConstants.MMB_BUDEJOVICE_OM;

public class IbTasks {

    private final Harness harness;

    public IbTasks(Harness harness) {
        this.harness = harness;
    }

    public void createIb(NonClient client, String cif) {
        double dayLimit = 5000;
        long activationRequest = 0;
        double weekLimit = 0;

        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo("/t/digitalchannels.ib/CRT_IB_ACCT/001")
                .addField("CIF", cif)
                .addField("DenniLimit", dayLimit)
                .addField("DenniLimitAnoNe", "1")
                .addField("Kanal", "3401001")
                .addField("MaIBsKreditkou", "1")
                .addField("ObchodniMistoAlternativni", MMB_BBC_OM)
                .addField("ObchodniMistoKod", MMB_BBC_OM)
                .addField("PozadavekAktivace", activationRequest)
                .addField("Prava", "13")
                .addField("PredvolbaMobKey", "420")
                .addField("SMSInfo", false)
                .addField("SouhlasSMobilnimKlicem", "1")
                .addField("TelefoniCisloMobKey", client.mobileNumber())
                .addField("TydenniLimit", weekLimit)
                .addField("TydenniLimitAnoNe", "0")
                .addField("ZacatekPoplatkovani", DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .addField("ZpusobAutentizace", "2")
                .addField("ZpusobCertifikace", "1")
                .post()
                .andAssertStatus(200);
    }

    public void generateLoginPasswordIb(String cif) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo("/t/digitalchannels.sb/UFO_PASSWORD_RESET/002/")
                .addField("cif", cif)
                .addField("channelId", "3401001")
                .addField("envelopeId", "0")
                .post()
                .andAssertStatus(200);
    }

    public boolean existClientInUdebs(String cif) {
        int existence = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo("/t/customer.cluster/CHK_CLIENT_UDEBS_EXISTENCE/001")
                .addField("CIF", cif)
                .post()
                .andAssertStatus(200)
                .extract("ExistujeKlient")
                .asInt();

        return existence == 1;
    }

    public void createClientInUdebs(NonClient nonClient, String cif) {
        createClientInUdebs(nonClient, cif, false);
    }

    public void createFopClientInUdebs(NonClient nonClient, String cif) {
        createClientInUdebs(nonClient, cif, true);
    }

    private void createClientInUdebs(NonClient nonClient, String cif, boolean fop) {
        String streetWithDescriptionNumber = nonClient.residencyAddress().getStreet() + " " + nonClient.residencyAddress().getDescriptiveNumber();

        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo("/t/mwf.legacy/MWFA02/001")
                .addField("Adresat", nonClient.fullName())
                .addField("CIF", cif)
                .addField("Email", nonClient.email())
                .addField("Jmeno", nonClient.firstName())
                .addField("Mobil", nonClient.mobileNumber())
                .addField("MobilPredvolba", "420")
                .addField("ObchodniMistoKod", MMB_BBC_OM)
                .addField("Obec", nonClient.residencyAddress().getCity())
                .addField("OsobniCislo", fop ? nonClient.ico() : nonClient.birthNumber())
                .addField("PSC", nonClient.residencyAddress().getZip())
                .addField("Posta", nonClient.residencyAddress().getCity())
                .addField("Prijmeni", nonClient.lastName())
                .addField("TypKlienta", fop ? "2" : "1")
                .addField("TypUctu", fop ? "32" : "2")
                .addField("Ulice", streetWithDescriptionNumber)
                .addField("Zeme", nonClient.residencyAddress().getCountry())
                .addField("ZkraceneJmeno", fop ? "ZkrJmFop" : "ZkrJm")
                .post()
                .andAssertStatus(200);
    }

    public String getInternetBankLoginName(String clientCif) {
        RawRestRequest.Response response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/digitalchannels.sb/GET_EM_IB_LOGINNAME/001")
                .withHeader("USERID_L2", harness.tasks().hypos().hyposWso2Tasks().userIdL2Hypos)
                .withHeader("Authorization", "Bearer " + harness.tasks().hypos().hyposWso2Tasks().tokenUfo)
                .withPayloadFromTemplate(Template.fromFilepath("system/wso2/mortgages_cluster/createInternetBankApis/get_em_ib_loginname.json")
                        .setQuoted("CIF", clientCif))
                .post()
                .andAssertStatus(200);
        String clientIbLogin = response.extract("LOGINNAME").toString().replace('"', ' ').trim();
        harness.log("Internet bank login is: " + clientIbLogin);
        return clientIbLogin;
    }

    public void createSb(String cif) {
        double dayLimit = 5000;

        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo("/t/digitalchannels.sb/DO_CREATE_SB_CHANNEL/001")
                .addField("CIF", cif)
                .addField("Channel", "7070001")
                .addField("LimitDaily", dayLimit)
                .addField("LimitDailyFlag", "1")
                .addField("BranchOfficeCode", MMB_BUDEJOVICE_OM)
                .addField("BranchOfficeAlt", MMB_BUDEJOVICE_OM)
                .addField("FreeChannel", null)
                .addField("AuthentisationMethod", null)
                .addField("Rights", "801")
                .addField("CertificationMethod", null)
                .addField("StartChargeFee", DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .addField("Activation", null)
                .addField("EntityId", null)
                .addField("LimitWeekly", null)
                .addField("LimitWeeklyFlag", "0")
                .addField("EnvelopeNumber", null)
                .addField("UserId", null)
                .addField("ContextId", null)
                .post()
                .andAssertStatus(204);
    }
}