package eu.mhsl.minenet.minigames.score;

import eu.mhsl.minenet.minigames.util.MapUtil;
import net.minestom.server.entity.Player;

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class LowestPointsWinScore extends PointsWinScore {
    @Override
    protected void insertResultImplementation(Set<Player> p, int currentPoints) {
        Set<Player> combined = this.scoreOrder.entrySet().stream()
            .filter(entrySet -> Objects.equals(entrySet.getValue(), currentPoints))
            .map(Map.Entry::getKey)
            .findFirst()
            .orElseGet(() -> {
                Set<Player> s = new HashSet<>();
                this.scoreOrder.put(s, currentPoints);
                return s;
            });
        combined.addAll(p);

        this.scoreOrder = MapUtil.sortReversedByValue(this.scoreOrder);
        this.getScores().clear();
        this.scoreOrder.forEach((player, integer) -> this.getScores().addFirst(player));
    }
}
