package io.korobi.mongo;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import javax.inject.Inject;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class KeyedChannelBlacklist implements IChannelBlacklist {

    private MongoDatabase database;
    private Map<String, Set<String>> cachedChannels;

    @Inject
    public KeyedChannelBlacklist(MongoDatabase database) {
        this.database = database;
        createCache();
    }

    private void createCache() {
        Map<String, Set<String>> cache = new HashMap<>();
        MongoCollection<Document> channels = database.getCollection("channels");
        FindIterable<Document> allChannels = channels.find();
        Stream<Document> stream = StreamSupport.stream(allChannels.spliterator(), false);
        stream.collect(Collectors.groupingBy(
            channel -> channel.getString("network"),
            HashMap::new,
            Collector.of(
                HashSet::new, Set::add,
                (left, right) -> {
                    left.addAll(right);
                    return left;
                },
                Collections::unmodifiableSet
            )
        ));

        for (Document doc : allChannels) {
            if (doc.containsKey("key") && doc.get("key") != null) {
                createOrAppendForNetwork(doc.getString("network"), doc.getString("channel"), cache);
            }
        }
    }

    private <T> Stream<T> stream(Iterable<T> iterable) {
        return StreamSupport.stream(
                Spliterators.spliteratorUnknownSize(
                        iterable.iterator(),
                        Spliterator.ORDERED
                ),
                false
        );
    }

    private void createOrAppendForNetwork(String network, String channel, Map<String, Set<String>> cache) {
        if (cache.containsKey(network)) {
            cache.get(network).add(channel);
        } else {
            HashSet<String> channelSet = new HashSet<>();
            channelSet.add(channel);
            cache.put(network, channelSet);
        }
    }

    public boolean isBlacklisted(String channel, String network) {
        return false;
    }
}

@@ -1379,61 +1379,27 @@
     
-for (Document doc : allChannels) %7B%0A            if (do
+stream.filter(c -%3E 
 c.co
@@ -1418,18 +1418,16 @@
 ey%22) && 
-do
 c.get(%22k
@@ -1443,798 +1443,128 @@
 ull)
- %7B%0A                createOrAppendForNetwork(doc.getString(%22network%22), doc.getString(%22channel%22), cache);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private %3CT%3E Stream%3CT%3E stream(Iterable%3CT%3E iterable) %7B%0A        return StreamSupport.stream(%0A                Spliterators.spliteratorUnknownSize(%0A                        iterable.iterator(),%0A                        Spliterator.ORDERED%0A                ),%0A                false%0A        );%0A    %7D%0A%0A    private void createOrAppendForNetwork(String network, String channel, Map%3CString, Set%3CString%3E%3E cache) %7B%0A        if (cache.containsKey(network)) %7B%0A            cache.get(network).add(channel);%0A        %7D else %7B%0A            HashS
+;%0A        System.out.println(stream.count());%0A        System.out.println(stream.findFirst().get().g
 et
-%3C
 String
-%3E channelSet = new HashSet%3C%3E();%0A            channelSet.add(channel);%0A            cache.put(network, channelSet);%0A        %7D
+(%22channel%22));
 %0A   

Add debug logging