package com.heroku.myapp.myorchestrator.consumers.snapshot.seiyu;

import com.heroku.myapp.commons.config.enumerate.Kind;
import com.heroku.myapp.commons.consumers.SnapshotQueueConsumer;
import com.heroku.myapp.commons.util.actions.MasterUtil;
import com.heroku.myapp.commons.util.content.DocumentUtil;
import com.heroku.myapp.commons.util.content.MapList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.camel.Exchange;
import org.bson.Document;
import org.springframework.stereotype.Component;

@Component
public class SnapshotSoundDirectorStatisticsConsumer extends SnapshotQueueConsumer {

    @Override
    protected Optional<Document> doSnapshot(Exchange exchange) {
        MasterUtil util = new MasterUtil(exchange);
        Set<String> ignore = new HashSet<>(Arrays.asList(new String[]{"藤原啓治", "井上和彦 (声優)","千葉繁","宮村優子 (声優)"}));
        MapList list = util.mapList(Kind.pages_mutual_sound_director).filteredList((map) -> {
            List<String> director = (List<String>) map.get("director");
            return !director.stream().allMatch((d) -> ignore.contains(d));
        });
        int size = list.size();
        List<Map<String, Object>> result = list.stream().flatMap((map) -> {
            List<String> director = (List<String>) map.get("director");
            String title = (String) map.get("title");
            return director.stream().map((name) -> {
                Map<String, String> part = new LinkedHashMap<>();
                part.put("key", name);
                part.put("title", title);
                return part;
            });
        }).collect(Collectors.groupingBy((part) -> part.get("key")))
                .entrySet().stream()
                .filter((entry) -> !ignore.contains(entry.getKey()))
                .map((entry) -> {
                    String director = entry.getKey();
                    Set<String> titles = entry.getValue().stream().map((m) -> m.get("title"))
                            .collect(Collectors.toSet());
                    Map<String, Object> resultMap = new LinkedHashMap<>();
                    resultMap.put("director", director);
                    resultMap.put("count", titles.size());
                    resultMap.put("percentage", titles.size() * 100d / size);
                    return resultMap;
                }).sorted((Map<String, Object> o1, Map<String, Object> o2) -> {
            double d1 = (double) o1.get("percentage");
            double d2 = (double) o2.get("percentage");
            if (d1 == d2) {
                return 0;
            } else if (d1 > d2) {
                return -1;
            } else {
                return 1;
            }
        }).collect(Collectors.toList());
        return new DocumentUtil(result).nullable();
    }
}

@@ -995,14 +995,16 @@
 %E5%84%AA)%22,
+ 
 %22%E5%8D%83%E8%91%89%E7%B9%81%22,
+ 
 %22%E5%AE%AE%E6%9D%91%E5%84%AA
@@ -1010,16 +1010,93 @@
 %E5%84%AA%E5%AD%90 (%E5%A3%B0%E5%84%AA)%22
+, %22%E6%B4%A5%E4%B9%85%E4%BA%95%E6%95%99%E7%94%9F%22, %22%E5%B1%B1%E7%94%B0%E9%99%BD%22, %22%E5%B9%BE%E5%8E%9F%E9%82%A6%E5%BD%A6%22, %22%E6%9D%BF%E5%9E%A3%E4%BC%B8%22, %22%E6%B0%B4%E5%B3%B6%E5%8A%AA%22, %22%E9%AB%98%E6%9D%BE%E4%BF%A1%E5%8F%B8%22, %22%E7%A8%B2%E5%9E%A3%E9%9A%86%E8%A1%8C%22, %22%E4%BD%90%E8%97%A4%E9%A0%86%E4%B8%80%22, %22%E4%B8%89%E3%83%84%E7%9F%A2%E9%9B%84%E4%BA%8C%22,%22%E5%A1%A9%E5%B1%8B%E7%BF%BC%22
 %7D));%0A   

modify SnapshotSoundDirectorStatisticsConsumer logic