package com.ljs.ootp.ai.value;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.ljs.ootp.ai.io.Printable;
import com.ljs.ootp.ai.player.Player;
import com.ljs.ootp.ai.selection.Mode;
import com.ljs.ootp.ai.player.Slot;
import com.ljs.ootp.ai.site.Site;
import java.io.PrintWriter;

/**
 *
 * @author lstephen
 */
public class LeagueReplacementLevel implements Printable {

    private Site site;

    private PlayerValue value;

    private Iterable<Player> players;

    private LeagueReplacementLevel(Site site, PlayerValue value, Iterable<Player> players) {
        this.site = site;
        this.value = value;
        this.players = players;
    }

    public Integer getReplacementLevel(final Slot s, Function<Player, Integer> value) {
        Iterable<Player> playersAtSlot =
            Iterables.filter(players, new Predicate<Player>() {
                public boolean apply(Player p) {
                    return Slot.getPrimarySlot(p) == s;
                }
            });

        Integer replacementLevelRank = Mode.REGULAR_SEASON.getAllSlots().count(s) * Iterables.size(site.getTeamIds());

        return value.apply(
            Iterables.getFirst(
                Iterables.skip(
                    Ordering
                        .natural()
                        .reverse()
                        .onResultOf(value)
                        .sortedCopy(playersAtSlot),
                    replacementLevelRank),
                null));
    }

    @Override
    public void print(PrintWriter w) {
        w.println();
        w.println("** League Replacement Level **");
        w.print("Current: ");
        for (Slot s : Slot.values()) {
            if (s == Slot.P) {
                continue;
            }
            w.print(String.format(" %s:%3d ", s, getReplacementLevel(s, value.getNowValue())));
        }
        w.println();
        w.print("Future:  ");
        for (Slot s : Slot.values()) {
            if (s == Slot.P) {
                continue;
            }
            w.print(String.format(" %s:%3d ", s, getReplacementLevel(s, value.getFutureValue())));
        }
        w.println();
    }

    public static LeagueReplacementLevel create(Site site, PlayerValue value, Iterable<Player> players) {
        return new LeagueReplacementLevel(site, value, players);
    }

}

@@ -102,24 +102,71 @@
 .Predicate;%0A
+import com.google.common.collect.ImmutableSet;%0A
 import com.g
@@ -342,22 +342,19 @@
 .ai.
-selection.Mode
+player.Slot
 ;%0Aim
@@ -370,35 +370,38 @@
 ljs.ootp.ai.
-player.Slot
+selection.Mode
 ;%0Aimport com
@@ -922,23 +922,27 @@
        I
-ter
+mmut
 able
+Set
 %3CPlayer%3E
@@ -958,16 +958,53 @@
 tSlot =%0A
+            ImmutableSet.copyOf(%0A    
         
@@ -1071,24 +1071,28 @@
             
+  
+  
 public boole
@@ -1108,24 +1108,28 @@
 Player p) %7B%0A
+    
             
@@ -1180,32 +1180,36 @@
                 
+    
 %7D%0A            %7D)
@@ -1206,17 +1206,22 @@
         
-%7D
+    %7D)
 );%0A%0A    
@@ -1641,16 +1641,137 @@
         
+ replacementLevelRank %3E playersAtSlot.size()%0A                        ? playersAtSlot.size() - 1%0A                        :
  replace

Fix bug if there weren't enough players in the league to determine a league replacement level