/*
 * Copyright 2016 Lukáš Petrovický
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.triceo.robozonky;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.triceo.robozonky.remote.Investment;
import com.github.triceo.robozonky.remote.Rating;
import com.github.triceo.robozonky.remote.RiskPortfolio;
import com.github.triceo.robozonky.remote.Statistics;

public class PortfolioOverview {

    public static PortfolioOverview calculate(final BigDecimal balance, final Statistics stats,
                                              final Collection<Investment> investments) {
        final Map<Rating, BigDecimal> amounts = stats.getRiskPortfolio().stream().collect(
                Collectors.toMap(RiskPortfolio::getRating, risk -> BigDecimal.valueOf(risk.getUnpaid()))
        );
        // make sure ratings are present even when there's 0 invested in them
        Arrays.stream(Rating.values()).filter(r -> !amounts.containsKey(r))
                .forEach(r -> amounts.put(r, BigDecimal.ZERO));
        // make sure the share reflects investments made by ZonkyBot which have not yet been reflected in the API
        investments.forEach(previousInvestment -> {
            final Rating r = previousInvestment.getRating();
            final BigDecimal investment = BigDecimal.valueOf(previousInvestment.getAmount());
            amounts.put(r, amounts.get(r).add(investment));
        });
        return new PortfolioOverview(balance, Collections.unmodifiableMap(amounts));
    }

    private final BigDecimal czkAvailable, czkInvested;
    private final Map<Rating, BigDecimal> czkInvestedPerRating;

    private PortfolioOverview(final BigDecimal czkAvailable, final Map<Rating, BigDecimal> czkInvestedPerRating) {
        this.czkAvailable = czkAvailable;
        this.czkInvested = Util.sum(czkInvestedPerRating.values());
        this.czkInvestedPerRating = czkInvestedPerRating;
    }

    public BigDecimal getCzkAvailable() {
        return this.czkAvailable;
    }

    public BigDecimal getCzkInvested() {
        return this.czkInvested;
    }

    public BigDecimal getCzkInvested(final Rating r) {
        return this.czkInvestedPerRating.getOrDefault(r, BigDecimal.ZERO);
    }

    public BigDecimal getShareOnInvestment(final Rating r) {
        return this.getCzkInvested(r).divide(this.getCzkInvested(), 4, RoundingMode.HALF_EVEN);
    }

    // FIXME optimize
    public Map<Rating, BigDecimal> getSharesOnInvestment() {
        return Arrays.stream(Rating.values())
                .collect(Collectors.toMap(Function.identity(), r -> this.getShareOnInvestment(r)));
    }
}

@@ -1303,24 +1303,90 @@
 estments) %7B%0A
+        // first figure out how much we have in outstanding loans%0A
         fina
@@ -1595,225 +1595,12 @@
  // 
-make sure ratings are present even when there's 0 invested in them%0A        Arrays.stream(Rating.values()).filter(r -%3E !amounts.containsKey(r))%0A                .forEach(r -%3E amounts.put(r, BigDecimal.ZERO));%0A        //
+then
  mak
@@ -2189,16 +2189,78 @@
 rRating;
+%0A    private final Map%3CRating, BigDecimal%3E sharesOnInvestment;
 %0A%0A    pr
@@ -2532,24 +2532,413 @@
 dPerRating;%0A
+        if (this.czkInvested.intValue() == 0) %7B%0A            this.sharesOnInvestment = Collections.emptyMap();%0A        %7D else %7B%0A            this.sharesOnInvestment = Arrays.stream(Rating.values()).collect(Collectors.toMap(%0A                    Function.identity(),%0A                    r -%3E this.getCzkInvested(r).divide(this.czkInvested, 4, RoundingMode.HALF_EVEN))%0A            );%0A        %7D%0A
     %7D%0A%0A    p
@@ -3318,301 +3318,155 @@
 his.
-getCzkInvested(r).divide(this.getCzkInvested(), 4, RoundingMode.HALF_EVEN);%0A    %7D%0A%0A    // FIXME optimize%0A    public Map%3CRating, BigDecimal%3E getSharesOnInvestment() %7B%0A        return Arrays.stream(Rating.values())%0A                .collect(Collectors.toMap(Function.identity(), r -%3E
+sharesOnInvestment.getOrDefault(r, BigDecimal.ZERO);%0A    %7D%0A%0A    public Map%3CRating, BigDecimal%3E getSharesOnInvestment() %7B%0A        return
  this.
-getS
+s
 hare
+s
 OnIn
@@ -3477,13 +3477,8 @@
 ment
-(r)))
 ;%0A  

Optimize PortfolioOverview internals