package com.heroku.myorchestrator.consumers.specific.external;

import com.heroku.myorchestrator.config.enumerate.Kind;
import com.heroku.myorchestrator.consumers.SnapshotRouteBuilder;
import com.heroku.myorchestrator.util.actions.MasterUtil;
import com.heroku.myorchestrator.util.consumers.IronmqUtil;
import com.heroku.myorchestrator.util.content.GoogleWikiTitle;
import java.util.ArrayList;
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 SnapshotAmiamiOriginalTitlesConsumer extends SnapshotRouteBuilder {

    @Override
    protected Optional<Document> doSnapshot(Exchange exchange, Document document) {
        try {
            mergeNewTitles(exchange, document);
            updateWikiTitles(document);
            return Optional.ofNullable(document);
        } catch (Exception ex) {
            IronmqUtil.sendError(this.getClass(), "doSnapshot", exchange, ex);
            return Optional.empty();
        }
    }

    public void mergeNewTitles(Exchange exchange, Document document) throws Exception {
        MasterUtil util = new MasterUtil(exchange);
        List<Map<String, Object>> originalTitlesList, amiamiItemList;
        try {
            originalTitlesList = util.findLatest().get()
                    .get("data", List.class);
        } catch (Exception ex0) {
            originalTitlesList = new ArrayList<>();
        }
        amiamiItemList = util.kind(Kind.amiami_item).findLatest().get()
                .get("data", List.class);
        Set originalTitlesSet = originalTitlesList
                .stream().map((map) -> map.get("amiami_title"))
                .collect(Collectors.toSet());
        amiamiItemList.stream()
                .map((map) -> (String) map.get("orig"))
                .filter((title) -> title.length() > 0)
                .collect(Collectors.toSet())
                .stream()
                .filter((title) -> !originalTitlesSet.contains(title))
                .map((title) -> {
                    Map<String, Object> map = new LinkedHashMap<>();
                    map.put("amiami_title", title);
                    return map;
                })
                .forEach(originalTitlesList::add);
        document.append("data", originalTitlesList);
    }

    public void updateWikiTitles(Document document) {
        List<Map<String, Object>> titles = document.get("data", List.class);
        final GoogleWikiTitle gwt = new GoogleWikiTitle();
        titles.stream().filter((map) -> !map.containsKey("wiki_title"))
                .limit(10).forEach((map) -> {
            String amiamiTitle = (String) map.get("amiami_title");
            List<String> wikiTitles
                    = new ArrayList<>(gwt.google(amiamiTitle).get());
            map.put("wiki_title", wikiTitles);
        });
        titles.stream().forEach((map) -> {
            List<String> wikiTitles = (List<String>) map.get("wiki_title");
            Set<String> collect = wikiTitles.stream().map((title) -> title.replace("_", " "))
                    .collect(Collectors.toSet());
            map.put("wiki_title", collect);
        });
        document.put("data", titles);
    }
}

@@ -3012,327 +3012,8 @@
 %7D);%0A
-        titles.stream().forEach((map) -%3E %7B%0A            List%3CString%3E wikiTitles = (List%3CString%3E) map.get(%22wiki_title%22);%0A            Set%3CString%3E collect = wikiTitles.stream().map((title) -%3E title.replace(%22_%22, %22 %22))%0A                    .collect(Collectors.toSet());%0A            map.put(%22wiki_title%22, collect);%0A        %7D);%0A
     

remove unnecessary logic