package cz.moneta.test.dsl.ufo.banka.tasks.expres;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.fon.TIS050_S001_SignerSelectionPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.NEP001_S004_ApplicationDataPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.LoanData;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP001_S001_NewExpressPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP006_S001_ExpressScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP063_S001_RealityPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.DOK001_S001_SelectionOfSubmittedDocumentsPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisAllLoanObject;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisProductClass;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisUcelUveruExpres;
import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;

import java.util.function.Function;

import static cz.moneta.test.dsl.util.AddressUtils.buildStreetWithNumbers;

public class HousingLoanTasks {

    private final Harness harness;
    public static final String HOUSING_LOAN_REALITY_STORE_KEY = "HOUSING_LOAN_REALITY";
    public static final String CONTRACT_DRAFT_DOCUMENT_NAME = "Návrh smlouvy";
    public static final String ESIP_DOCUMENT_NAME = "ESIP";

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

    public Function<NEP001_S001_NewExpressPage, NEP063_S001_RealityPage> fillHousingLoanData(LoanData loanData) {
        return start -> start
                .clickHousingLoanCheckbox()
                .selectRealityType(loanData.getHousingLoanData().getRealityType().getValue())
                .selectOwnershipType(loanData.getHousingLoanData().getOwnershipType().getValue())
                .selectLoanPurposeType(loanData.getHousingLoanData().getPurposeType().getValue())
                .selectFixation(loanData.getHousingLoanData().getFixation().getValue())
                .fillAmount(loanData.getAmount())
                .selectTerm(loanData.getTerm())
                .clickContinue()
                .onRealityPage();
    }

    public Function<NEP063_S001_RealityPage, DOK001_S001_SelectionOfSubmittedDocumentsPage> fillRealityData(LoanData data) {
        return start -> {
            String reality;
            if (data.getHousingLoanData().getPurposeType() == HCisUcelUveruExpres.CONSTRUCTION ||
                    data.getHousingLoanData().getPurposeType() == HCisUcelUveruExpres.CONSTRUCTION_WITH_ENERGY_SAVINGS) {
                start.fillRealityLandRegistryArea(data.getHousingLoanData().getLandRegistryArea())
                        .fillRealityPlotNumber(data.getHousingLoanData().getPlotNumber())
                        .clickFindRealityLandRegistryArea();
                reality = data.getHousingLoanData().getLandRegistryArea() + ", " + data.getHousingLoanData().getPlotNumber();
            } else {
                if (data.getHousingLoanData().isRealityCustomAddress()) {
                    start.fillRealityAddressStreet(data.getHousingLoanData().getRealityCustomAddress().getStreet())
                            .fillRealityAddressDescriptiveNumber(data.getHousingLoanData().getRealityCustomAddress().getDescriptiveNumber())
                            .fillRealityAddressOrientationNumber(data.getHousingLoanData().getRealityCustomAddress().getOrientationNumber())
                            .fillRealityAddressCity(data.getHousingLoanData().getRealityCustomAddress().getCity())
                            .fillRealityAddressZip(data.getHousingLoanData().getRealityCustomAddress().getZip());
                    reality = buildStreetWithNumbers(data.getHousingLoanData().getRealityCustomAddress());
                } else {
                    start.clickSameAddress();
                    reality = buildStreetWithNumbers(data.getClient().residencyAddress());
                }

                if (data.getHousingLoanData().getRealityType() == HCisAllLoanObject.APARTMENT) {
                    start.fillRealityAddressApartmentNumber(data.getHousingLoanData().getApartmentNumber());
                    reality += ", byt č.: " + data.getHousingLoanData().getApartmentNumber();
                }
            }
            harness.store(HOUSING_LOAN_REALITY_STORE_KEY, reality);
            return start.clickConfirmRealityCheck().clickContinue();
        };
    }

    public Function<PSP004_S001_PrintPage, PSP004_S001_PrintPage> printPreContract() {
        return page -> page
                .saveDocumentUrl(CONTRACT_DRAFT_DOCUMENT_NAME)
                .closePrintWindow()
                .onPrintPage()
                .saveDocumentUrl(ESIP_DOCUMENT_NAME)
                .closePrintWindow()
                .onContractSignPage()
                .clickCenterButton()
                .onSecondBankerVerificationStatusPage()
                .checkVerificationStatus("Zavstupované dokumenty / doklady byly v pořádku.")
                .clickContinue()
                .onPrintPage();
    }

    public Function<HLO001_S001_ClientSearchPage, NEP006_S001_ExpressScoringResultPage> housingLoanE2ETask(LoanData data) {
        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        ExpressTasks expressTasks = bankaTasks.expresTasks();
        return page -> {
            NEP001_S004_ApplicationDataPageTransitions applicationDataPageTransitions = page
                    .then(bankaTasks.search().findByRC(data.getClient().birthNumber()))
                    .createNewProduct(HCisProductClass.EXPRESS)
                    .onExpressPage()
                    .then(expressTasks.fillAddCoApplicants(data.getCoApplicants(), "Bratr/Sestra"))
                    .then(fillHousingLoanData(data))
                    .then(fillRealityData(data))
                    .then(expressTasks.fillIdPage())
                    .then(expressTasks.fillApplicantPage(data.getClient()))
                    .then(expressTasks.fillCoApplicants(data.getCoApplicants()))
                    .onApplicationDataPage()
                    .then(expressTasks.fillApplicationDataPage(data.isInsurance(), data.getInsuranceName()));

            TIS050_S001_SignerSelectionPage signerSelectionPage = data.isInsurance()
                    ? applicationDataPageTransitions.onMeetingRecordPage()
                    .then(expressTasks.fillMeetingRecordPage())
                    : applicationDataPageTransitions.onSignerSelectionPage();

            return signerSelectionPage
                    .then(expressTasks.signDocuments(data.isInsurance()))
                    .waitForStatusAndRefresh("PODMÍNĚNÝ SOUHLAS")
                    .then(expressTasks.createAccountRequiredOrFirstFirstOption(data.getAmount(), data.getTerm()))
                    .then(expressTasks.verification("Sudra"))
                    .then(printPreContract())
                    .then(expressTasks.printContract());
        };
    }
}
