package cz.moneta.test.dsl.util.data.hypos;

import cz.moneta.test.dsl.hypos.contractPrepare.data.HyposContractYamlData;
import cz.moneta.test.dsl.util.data.client.HyposClient;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

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

@Builder
@Getter
public class HyposContract {
    private List<HyposClient> clients;
    private Realty realty;
    private int mortgageLoanAmount;
    private int mortgageLoanMaturity;
    private int mortgageOwnResources;
    private int mortgageOtherResources;
    private String loanProduct;
    private String loanAction;
    private String loanObjectType;
    private String purchaseReason;
    private List<LoanPurpose> loanPurposes;
    private List<DrawingCondition> drawingConditions;
    private HouseholdIncome firstHouseholdIncome;
    private HouseholdIncome secondHouseholdIncome;
    @Setter
    private String contractId;
    @Setter
    private String currentAccountNumber;

    /**
     * Creating new loan purposes for Hypos tests.
     * @return List<LoanPurpose> with yaml data.
     */
    public List<LoanPurpose> createLoanPurposes(HyposContractYamlData hyposContractYamlData) {
        List<LoanPurpose> loanPurposes = new ArrayList<>();
        for (int i = 0; i < hyposContractYamlData.getLoanPurposes().size(); i++) {
            LoanPurpose loanPurpose = LoanPurpose.builder()
                    .tranche(hyposContractYamlData.getLoanPurposes().get(i).getTranche())
                    .type(hyposContractYamlData.getLoanPurposes().get(i).getType())
                    .paymentType(hyposContractYamlData.getLoanPurposes().get(i).getPaymentType())
                    .amount(hyposContractYamlData.getLoanPurposes().get(i).getAmount())
                    .build();
            loanPurposes.add(loanPurpose);
        }
        return loanPurposes;
    }

    /**
     * Creating new drawing conditions for Hypos tests.
     * @return List<DrawingCondition> with yaml data.
     */
    public List<DrawingCondition> createDrawingConditions(HyposContractYamlData hyposContractYamlData) {
        List<DrawingCondition> drawingConditions = new ArrayList<>();
        for (int i = 0; i < hyposContractYamlData.getDrawingConditions().size(); i++) {
            List<String> tranches = new ArrayList<>();
            if(hyposContractYamlData.getDrawingConditions().get(i).getTranches() != null) {
                tranches.addAll(hyposContractYamlData.getDrawingConditions().get(i).getTranches());
            }
            DrawingCondition drawingCondition = DrawingCondition.builder()
                    .condition(hyposContractYamlData.getDrawingConditions().get(i).getCondition())
                    .tranches(tranches)
                    .type(hyposContractYamlData.getDrawingConditions().get(i).getType())
                    .build();
            drawingConditions.add(drawingCondition);
        }
        return drawingConditions;
    }

    /**
     * Creating new contract for Hypos tests.
     * @return HyposContract with yaml data.
     */
    public HyposContract createHyposContract(HyposContractYamlData hyposContractYamlData, List<HyposClient> hyposClients, Realty realty) {
        return HyposContract.builder()
                .clients(hyposClients)
                .realty(realty)
                .mortgageLoanAmount(hyposContractYamlData.getMortgageLoanAmount())
                .mortgageLoanMaturity(hyposContractYamlData.getMortgageLoanMaturity())
                .mortgageOwnResources(hyposContractYamlData.getMortgageOwnResources())
                .mortgageOtherResources(hyposContractYamlData.getMortgageOtherResources())
                .loanProduct(hyposContractYamlData.getLoanProduct())
                .loanAction(hyposContractYamlData.getLoanAction())
                .loanObjectType(hyposContractYamlData.getLoanObjectType())
                .purchaseReason(hyposContractYamlData.getPurchaseReason())
                .firstHouseholdIncome(HouseholdIncome.builder()
                        .withIncome(hyposContractYamlData.getHouseholdIncome().get(0).getWithIncome())
                        .withoutIncome(hyposContractYamlData.getHouseholdIncome().get(0).getWithoutIncome())
                        .build())
                .secondHouseholdIncome(HouseholdIncome.builder()
                        .withIncome(hyposContractYamlData.getHouseholdIncome().get(1).getWithIncome())
                        .withoutIncome(hyposContractYamlData.getHouseholdIncome().get(1).getWithoutIncome())
                        .build())
                .loanPurposes(this.createLoanPurposes(hyposContractYamlData))
                .drawingConditions(this.createDrawingConditions(hyposContractYamlData))
                .build();
    }
}