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();
    }
}

@@ -1113,16 +1113,30 @@
  %22%E9%83%B7%E7%94%B0%E3%81%BB%E3%81%A5%E3%81%BF%22
+,%22%E5%B1%B1%E5%B4%8E%E3%81%9F%E3%81%8F%E3%81%BF%22,%22%E6%9D%BE%E5%B0%BE%E8%A1%A1%22
 %7D));%0A   

modify sound director api logic