package org.innovateuk.ifs.project.grantofferletter.viewmodel;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static java.util.Collections.singletonList;

/*
 * Holder of values for the other costs rows on GOL finance tables, which are handled differently
 */
public class OtherCostsRowModel {

    private final String otherCostName;
    private Map<String, List<BigDecimal>> otherCostValues;

    public OtherCostsRowModel(String otherCostName,
                              Map<String, List<BigDecimal>> otherCostValues) {
        this.otherCostName = otherCostName;
        this.otherCostValues = otherCostValues;
    }

    public String getOtherCostName() {
        return otherCostName;
    }

    public Map<String, List<BigDecimal>> getOtherCostValues() {
        return otherCostValues;
    }

    public void addToCostValues(String orgName, BigDecimal cost) {
        if(otherCostValues.keySet().contains(orgName)) {
            otherCostValues.get(orgName).add(cost);
        } else {
            otherCostValues.put(orgName, singletonList(cost));
        }
    }

    public Collection<String> getOrganisations() {
        return otherCostValues.keySet();
    }

    public BigDecimal getCostValuesForOrg(String organisation) {
        return otherCostValues.get(organisation)
                .stream()
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}

@@ -1438,11 +1438,342 @@
 );%0A    %7D
+%0A%0A    public BigDecimal getTotal() %7B%0A        return otherCostValues == null ?%0A                BigDecimal.ZERO :%0A                otherCostValues%0A                        .values()%0A                        .stream()%0A                        .flatMap(List::stream)%0A                        .reduce(BigDecimal.ZERO, BigDecimal::add);%0A    %7D
 %0A%7D%0A

Add support for totals to other costs rows