package cz.moneta.test.dsl.util.task.ihub;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.mwf.MWF111;
import cz.moneta.test.dsl.mwf.MWFX85;
import cz.moneta.test.dsl.mwf.calculatetotalexposure.MWFQ68;
import cz.moneta.test.dsl.mwf.createaccount.MWF172;
import cz.moneta.test.dsl.mwf.createclientcomoda.MWFQ35;
import cz.moneta.test.dsl.mwf.createclientcomoda.MWFQ35Response;
import cz.moneta.test.dsl.mwf.createclientinudebs.MWFA02;
import cz.moneta.test.dsl.mwf.createcustomer.MWF109;
import cz.moneta.test.dsl.mwf.createinternetbanka.MWFA03;
import cz.moneta.test.dsl.mwf.editpage.MWFAJ2;
import cz.moneta.test.dsl.mwf.employeecount.MWFKE1;
import cz.moneta.test.dsl.mwf.existingclientudebs.MWF391;
import cz.moneta.test.dsl.mwf.gdprconsent.MWF6FA;
import cz.moneta.test.dsl.mwf.gdprconsent.MWF6FAResponse;
import cz.moneta.test.dsl.mwf.generateloginpasswordib.MWFIZ8;
import cz.moneta.test.dsl.mwf.relationclientbanker.MWFDL7;
import cz.moneta.test.dsl.mwf.subsidyaccount.MWF133;
import cz.moneta.test.dsl.mwf.taxresidency.MWFIL7;
import cz.moneta.test.dsl.mwf.taxresidency.MWFIL7Data;
import cz.moneta.test.dsl.mwf.taxresidency.MWFIL7Response;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisAllObrazovkySME;
import cz.moneta.test.dsl.ufo.mwf.MWFHK8;
import cz.moneta.test.dsl.ufo.mwf.MWFHK8Response;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.dsl.util.data.account.AccountDetail;
import cz.moneta.test.dsl.util.data.client.NewClient;
import cz.moneta.test.dsl.util.data.client.NonClient;
import org.jooq.Record;
import org.jooq.Result;

import java.util.ArrayList;
import java.util.List;

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

@SuppressWarnings("all")
@Deprecated
public class IHubTasks {

    private final Harness harness;
    private final String prefixMobileKey = "420";

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

    @Deprecated
    public String createCifFo(NonClient client) {
        return harness.withIHub()
                .prepareRequestAsUfo(MWF109.class)
                .RodneCislo(client.birthNumber())
                .DatumNarozeni(TestUtils.dateFromString(client.birthDate(), "d.M.yyyy")) //YYYY-MM-DD
                .DatumExpiraceKontaktnichUdaju(DateUtils.getRawTodayDateIncrementedByDays(730))
                .PlatnostOPdo(client.idCardExpiredRaw())
                .DatumPotvrzeniPrijmu(client.dateOfIssueConfirmationOfReceipt())
                .DatumZalozeniCIFu(DateUtils.getTodayRawDate())
                .SerieACisloOP(client.idCardNumber())
                .CisloPasu(client.passportId())
                .PlatnostPasuDo(client.passportExpiryDateRaw())
                .PasStatVydal(CZECH_REPUBLIC_CODE)
                .OPStatVydal(CZECH_REPUBLIC_CODE)
                .PrvniJmeno(client.firstName())
                .Prijmeni(client.lastName())
                .Co(client.residencyAddress().getOrientationNumber())
                .Cp(client.residencyAddress().getDescriptiveNumber())
                .Ulice(client.residencyAddress().getStreet())
                .Obec(client.residencyAddress().getCity())
                .KlasifikaceKlienta("0")
                .KodObchodnihoMista(MMB_BBC_OM)
                .MistoNarozeni(client.placeOfBirth())
                .Psc(client.residencyAddress().getZip())
                .NazevZamestnavatele(client.employer().getName())
                .Zamico(client.employer().getIco())
                .ZAMUlice(client.employer().getAddress().getStreet())
                .Zamco(client.employer().getAddress().getOrientationNumber())
                .Zamcp(client.employer().getAddress().getDescriptiveNumber())
                .Zampsc(client.employer().getAddress().getZip())
                .ZamObec(client.employer().getAddress().getCity())
                .PlatnostKontaktnichUdaju("1")
                .TelefonMobilni("420" + client.mobileNumber())
                .DatumNastehovani(client.currentAddressSinceRaw())
                .RodinnyStav(client.maritalStatus().getHCisValue())
                .Pohlavi("1")
                .ZamestnanOd(client.occupationEmploymentFromRaw())
                .CistyMesicniPrijem((double) client.netMonthlyIncomeInThousands())
                .PocetVyzivovanychDeti(Long.valueOf(client.dependants()))
                .TelefonDomu("420" + client.fixPhoneNumberHome())
                .TelefonZamestnani("420" + client.employer().getPhoneNumber())
                .NejvyssiDosazeneVzdelani("4")
                .EkonomickySektor("44")
                .DruhPrijmuKlienta(client.occupationStatus().getHCisValue())
                .DruhBydleni(client.accommodationType().getHCisValue())
                .PlatnostKontaktnichUdaju("1")
                .SocialneEkonomickaSkupina("1")
                .ZkraceneJmeno("ZkrJm")
                .TypKlienta("1")
                .Zeme(CZECH_REPUBLIC_CODE)
                .ZemeNarozeni(CZECH_REPUBLIC_CODE)
                .StatniPrislusnost(CZECH_REPUBLIC_CODE)
                .VztahKBance(client.relationshipToTheBank().getMetaCodeValue())
                .EMail(client.email())
                .NoEmailFlag(0l)
                .Povolani(client.occupationType().getHCisValue())
                .send()
                .Cif();
    }

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

        harness.withIHub()
                .prepareRequestAsUfo(MWFA03.class)
                .cif(cif)
                .denniLimit(dayLimit)
                .denniLimitAnoNe("1")
                .kanal("3401001")
                .maIBsKreditkou("1")
                .obchodniMistoAlternativni(MMB_BBC_OM)
                .obchodniMistoKod(MMB_BBC_OM)
                .pozadavekAktivace(activationRequest)
                .prava("13")
                .predvolbaMobKey(prefixMobileKey)
                .sMSInfo(false)
                .souhlasSMobilnimKlicem("1")
                .telefoniCisloMobKey(client.mobileNumber())
                .tydenniLimit(weekLimit)
                .tydenniLimitAnoNe("0")
                .zacatekPoplatkovani(DateUtils.getTodayRawDate())
                .zpusobAutentizace("2")
                .zpusobCertifikace("1")
                .send();
    }

    @Deprecated
    public void generateLoginPasswordIb(String cif) {
        harness.withIHub()
                .prepareRequestAsUfo(MWFIZ8.class)
                .cif(cif)
                .channel("3401001")
                .uctovatPoplatek("0")
                .sendType(0l)
                .send();
    }

    @Deprecated
    public boolean existClientInUdebs(String cif) {
        int existence = harness.withIHub()
                .prepareRequestAsUfo(MWF391.class)
                .cif(cif)
                .send()
                .existujeKlient()
                .intValue();

        return existence == 1;
    }

    @Deprecated
    public void createClientInUdebs(NonClient nonClient, String cif) {
        String adresat = nonClient.firstName() + " " + nonClient.lastName();
        String streetWithDescriptionNumber = nonClient.residencyAddress().getStreet() + " " + nonClient.residencyAddress().getDescriptiveNumber();

        harness.withIHub()
                .prepareRequestAsUfo(MWFA02.class)
                .typUctu("2")
                .email(nonClient.email())
                .obchodniMistoKod(MMB_BBC_OM)
                .adresat(adresat)
                .prijmeni(nonClient.firstName())
                .jmeno(nonClient.lastName())
                .obec(nonClient.residencyAddress().getCity())
                .typKlienta("1")
                .cif(cif)
                .psc(nonClient.residencyAddress().getZip())
                .mobil(nonClient.mobileNumber())
                .ulice(streetWithDescriptionNumber)
                .osobniCislo(nonClient.birthNumber())
                .zeme(nonClient.residencyAddress().getCountry())
                .posta(nonClient.residencyAddress().getCity())
                .zkraceneJmeno("ZkrJm")
                .mobilPredvolba(prefixMobileKey)
                .send();
    }

    @Deprecated
    public String createCifFop(NonClient fop, String bankerUsername) {
        String name = fop.firstName() + " " + fop.lastName();
        String shortenName = name.substring(0, Math.min(name.length(), 18));
        return harness.withIHub()
                .prepareRequestAsUfo(MWF111.class)
                .cp(fop.residencyAddress().getDescriptiveNumber())
                .datumExpiraceKontaktnichUdaju(DateUtils.getRawTodayDateIncrementedByDays(730))
                .datumPlatnostiOP(fop.idCardExpiredRaw())
                .datumPodnikaniOd(fop.occupationEmploymentFromRaw())
                .datumZalozeniCIFu(TestUtils.getTodayAsDate())
                .eMail(fop.email())
                .ekonomickySektor("25")
                .ico(fop.ico())
                .jmeno(fop.firstName())
                .kodObchodnihoMista(MMB_BUDEJOVICE_OM)
                .okec("022000")
                .oROddil("123")
                .oRSoud("1")
                .oRVlozka("123")
                .obchodniJmeno(name)
                .obec(fop.residencyAddress().getCity())
                .psc(fop.residencyAddress().getZip())
                .platnostKontaktnichUdaju("1")
                .pobytCR("1")
                .posta(fop.residencyAddress().getCity())
                .pravniForma("101")
                .prijmeni(fop.lastName())
                .ratingKlienta("0")
                .rezidence("1")
                .rodneCislo(fop.birthNumber())
                .serieACisloOP(fop.idCardNumber())
                .typKlienta("2")
                .ulice(fop.residencyAddress().getStreet())
                .ulozPSCBezKontroly("Y")
                .urednik(bankerUsername)
                .zeme(CZECH_REPUBLIC_CODE)
                .zkraceneJmeno(shortenName)
                .send()
                .cif();
    }

    @Deprecated
    public void relationFoFop(String cifFo, String cifFop) {
        harness.withIHub()
                .prepareRequestAsUfo(MWFX85.class)
                .cif(cifFo)
                .cif2(cifFop)
                .vztah1("KLO")
                .vztah2("KLP")
                .send();
    }

    @Deprecated
    public MWFIL7Response addCrAsTaxResidency(String cifFo) {
        List<MWFIL7Data> rowOne = new ArrayList<>();
        rowOne.add(MWFIL7Data.builder().rsItaxRsdncCntryCode(CZECH_REPUBLIC_CODE));

        return harness.withIHub().prepareRequestAsUfo(MWFIL7.class)
                .cif(cifFo)
                .zakladniAtributyFlag("0")
                .zemePobocekFlag("0")
                .zemeDanoveRegistraceFlag("1")
                .zemeCRSFlag("0")
                .data(rowOne)
                .send();
    }

    @Deprecated
    public MWF6FAResponse agreeWithGdprAcceptance(String cifFo) {
        return harness.withIHub().prepareRequestAsUfo(MWF6FA.class)
                .businessType("GEMCB")
                .clientInstId(cifFo)
                .clientInstSysId("ICB0")
                .clientType("1")
                .consTypeCommBankSecretByUnsecured("1")
                .consentBranchId(MMB_BBC_OM)
                .consentChannelId("POS")
                .consentMode("SET")
                .consentSetId("SET_CONCERN_CONSENTS")
                .consentSignTime(DateUtils.getRawTodayDateIncrementedByDays(-1))
                .consentEmployeeId("502613326")
                .send();
    }

    @Deprecated
    public boolean existInOds(String birthNumber) {
        return 0 != harness.withIHub()
                .prepareRequestAsUfo(MWFHK8.class)
                .Rc(birthNumber)
                .UcelDotazu("9")
                .BusinessRequestor("GEMCB")
                .AdresyOutputSysIDs("ICB|VIS|HYP|CMD")
                .IDZdrojovehoSystemuOUTPUT("ICB0")
                .MaximalniPocetZaznamu(50l)
                .PriznakBankovniTajemstvi("1")
                .PriznakRozsirenyVystup("1")
                .TypyOsobyFiltr("1|2|5")
                .AdresyOutputAktualniFlag("1")
                .send()
                .Data()
                .size();
    }

    @Deprecated
    public String createAccountAndGetAccountNumber(String cif, String name, AccountDetail accountDetail, String bankerUsername) {
        return harness.withIHub()
                .prepareRequestAsUfo(MWF172.class)
                .aAZeme(CZECH_REPUBLIC_CODE)
                .cif(cif)
                .datumOtevreni(DateUtils.getTodayRawDate())
                .distribuceVypisu("3")
                .obchodniJmeno(name)
                .obchodniMistoAlternativni(MMB_BBC_OM)
                .obchodniMistoKod(MMB_BBC_OM)
                .periodaVypisu("2")
                .poplatkovyPlan(accountDetail.getFeePlan())
                .postovniPoukazka("0")
                .pouzitaAA("1")
                .primarniUrednik(bankerUsername)
                .produktKod(accountDetail.getProductCode())
                .vypisNaKonciMesice(false)
                .send()
                .cisloUctu();
    }

    @Deprecated
    public String createAccountAndGetAccountNumber(NewClient newCustomer, AccountDetail accountDetail, String bankerUsername) {
        String name = newCustomer.getClientData().firstName() + " " + newCustomer.getClientData().lastName();
        return createAccountAndGetAccountNumber(newCustomer.getCif(), name, accountDetail, bankerUsername);
    }

    @Deprecated
    public void subsidyCzAccount(String accountNumber, double amount) {
        harness.withIHub()
                .prepareRequestAsUfo(MWF133.class)
                .cisloUctuInterni("16621848")
                .castkaTransakce(amount)
                .cisloUctuPrijemceInterni(accountNumber)
                .send();
    }

    @Deprecated
    public void subsidyEurAccount(String accountNumber, double amount) {
        harness.withIHub()
                .prepareRequestAsUfo(MWF133.class)
                .cisloUctuInterni("211187143")
                .castkaTransakce(amount)
                .cisloUctuPrijemceInterni(accountNumber)
                .kodMenyTransakce(Long.valueOf(23))
                .send();
    }

    @Deprecated
    public void setEmployeeCount(Long personId, Long empolyeeCount) {
        harness.withIHub()
                .prepareRequestAsUfo(MWFKE1.class)
                .personId(personId)
                .employeeCount(empolyeeCount)
                .employeeCountValidByUserId(GEBTHA6_SSO)
                .employeeCountValidDate(DateUtils.getTodayRawDate())
                .send();
    }

    @Deprecated
    public MWFHK8Response findUnifiedClient(String ico) {
        return harness.withIHub()
                .prepareRequestAsUfo(MWFHK8.class)
                .BusinessRequestor("GEM")
                .Ico(ico)
                .IDZdrojovehoSystemu("CMD")
                .UcelDotazu("1")
                .send();
    }

    @Deprecated
    public MWFQ35Response createClientComoda(Long uniPtKey) {
        return harness.withIHub()
                .prepareRequestAsUfo(MWFQ35.class)
                .business("GEM")
                .duvodDotazuDoODS("1")
                .uniptKey(uniPtKey)
                .send();
    }

    @Deprecated
    public Long setComodaData(String ico) {
        Long clientId = createAndGetClientId(ico);
        if (clientId.intValue() != -1) {
            setEmployeeCount(clientId, Long.valueOf(1));
        }
        return clientId;
    }

    @Deprecated
    public Long createAndGetClientId(String ico) {
        MWFHK8Response MWFHK8response = findUnifiedClient(ico);
        if (MWFHK8response.PocetNalezenychZaznamu().intValue() == 1) {
            MWFQ35Response MWFQ35response = createClientComoda(MWFHK8response.Data().get(0).RsIdunifikovanehoklienta());
            return MWFQ35response.iDOsoba();
        }
        return Long.valueOf(-1);
    }

    @Deprecated
    public void calculateTotalExposurePerson(Long personId) {
        harness.withIHub()
                .prepareRequestAsUfo(MWFQ68.class)
                .iDOsoba(personId);
    }

    @Deprecated
    public void setCommercialBankerToClient(String ico) {
        Result<Record> records = harness.tasks().ods().findInstPt(ico, "CMD0");
        if (records.isNotEmpty()) {
            String instPtKey = records.getValue(0, "INST_PT_KEY").toString();
            harness.log("INST_PT_KEY: {}", instPtKey);
            harness.withIHub()
                    .prepareRequestAsUfo(MWFDL7.class)
                    .delFlag("0")
                    .instPtKey(instPtKey)
                    .privatniBankerSSO("123060132")
                    .srcSysId("SFC01")
                    .typVazby("3")
                    .send();
        }
    }

    @Deprecated
    public void editPage(Long idNavrh, HCisAllObrazovkySME page, String state) {
        harness.withIHub()
                .prepareRequestAsUfo(MWFAJ2.class)
                .iDNavrh(idNavrh)
                .rSObrazovka(page.getValue())
                .rSStav(state)
                .send();
    }

}
