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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.LoanData;
import cz.moneta.test.dsl.util.BankaUtils;
import cz.moneta.test.dsl.util.data.client.NonClient;

import java.util.List;

import static cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPage.DOCUMENT_URLS_STORE_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.loans.NEP017_S014_SelectApprovedVariantsPage.CHOSEN_LOAN_OPTION_STORE_KEY;
import static cz.moneta.test.dsl.ufo.banka.tasks.expres.ExpresLoanDataPreparationTasks.CLIENT_CIF_STORE_KEY;
import static cz.moneta.test.dsl.ufo.banka.tasks.expres.ExpressTasks.*;
import static cz.moneta.test.dsl.ufo.banka.tasks.expres.HousingLoanTasks.*;
import static cz.moneta.test.dsl.util.AddressUtils.buildStreetWithNumbers;

public class ExpresLoanLoggingTasks {
    private final Harness harness;

    public ExpresLoanLoggingTasks(Harness harness) {
        this.harness = harness;
    }
        public static final String UFO_SESSION_ID_STORE_KEY = "reports.tmfj.execution.commentufosessionid";


    public String getLogExpresDataString(LoanData data) {
        return "\n" +
                (harness.get(APPL_KEY_STORE_KEY, true) != null ? "appl_key: " + harness.get(APPL_KEY_STORE_KEY).toString() + "\n" : "") +
                (harness.get(UFO_SESSION_ID_STORE_KEY, true) != null ? "sessionId: " + harness.get(UFO_SESSION_ID_STORE_KEY) + "\n" : "") +
                harness.tasks().ufo().banka().expresTasks().housingLoanLoggingTasks().logExpressTestData(data) + "\n" +
                (harness.get(CHOSEN_LOAN_OPTION_STORE_KEY, true) != null ? "   " + harness.get(CHOSEN_LOAN_OPTION_STORE_KEY) + "\n" : "") +
                BankaUtils.formatDocumentUrlsWithNames(harness.get(DOCUMENT_URLS_STORE_KEY, true));
    }

    public String getLogExpresHomeDataString(LoanData data) {
        return "\n" +
                (harness.get(APPL_KEY_STORE_KEY, true) != null ? "appl_key: " + harness.get(APPL_KEY_STORE_KEY).toString() + "\n" : "") +
                (harness.get(UFO_SESSION_ID_STORE_KEY, true) != null ? "sessionId: " + harness.get(UFO_SESSION_ID_STORE_KEY) + "\n" : "") +
                harness.tasks().ufo().banka().expresTasks().housingLoanLoggingTasks().logHousingLoanTestData(data) + "\n" +
                (harness.get(HOUSING_LOAN_REALITY_STORE_KEY, true) != null ? "   reality: " + harness.get(HOUSING_LOAN_REALITY_STORE_KEY, true) + "\n" : "") +
                (harness.get(CHOSEN_LOAN_OPTION_STORE_KEY, true) != null ? "   " + harness.get(CHOSEN_LOAN_OPTION_STORE_KEY) + "\n" : "") +
                BankaUtils.formatDocumentUrlsWithNames(harness.get(DOCUMENT_URLS_STORE_KEY, true));
    }

    private String logExpressTestData(LoanData data) {
        return loanDataString(harness.get(CLIENT_CIF_STORE_KEY, true), data);
    }

    private String logHousingLoanTestData(LoanData data) {
        return logExpressTestData(data) + housingLoanDataString(data);
    }

    private String loanDataString(String cif, LoanData data) {
        return "CIF: " + cif + "\n" +
                "Applicant:\n" +
                "   Name and Surname: " + data.getClient().firstName() + " " + data.getClient().lastName() + "\n" +
                "   Birth Number: " + data.getClient().birthNumber() + "\n" +
                "   Address: " + buildStreetWithNumbers(data.getClient().residencyAddress()) +
                getCoApplicantDataString(data.getCoApplicants()) +
                "\nLoan data:\n" +
                "   amount: " + data.getAmount() + "\n" +
                "   term: " + data.getTerm();
    }

    private String housingLoanDataString(LoanData data) {
        return
                "\n   realityType: " + data.getHousingLoanData().getRealityType().getValue() + "\n" +
                        "   ownershipType: " + data.getHousingLoanData().getOwnershipType().getValue() + "\n" +
                        "   loanPurpose: " + data.getHousingLoanData().getPurposeType().getValue();
    }

    private String getCoApplicantDataString(List<NonClient> coApplicants) {
        String text = "";
        if (coApplicants != null && !coApplicants.isEmpty()) {
            for (NonClient coApplicant : coApplicants) {
                text += "\nCoApplicant:\n" +
                        "    Name and Surname: " + coApplicant.firstName() + " " + coApplicant.lastName() + "\n" +
                        "    Birth Number: " + coApplicant.birthNumber() + "\n" +
                        "    Address: " + buildStreetWithNumbers(coApplicant.residencyAddress());

            }
        }
        return text;
    }
}
