package com.zhiqiyun.open.core.spiders;

import com.alibaba.fastjson.JSON;
import com.zhiqiyun.open.core.models.sentiment.SentimentSpiderResult;
import com.zhiqiyun.open.core.service.SentimentSpiderResultService;
import com.zhiqiyun.open.utils.DateUtil;
import org.apache.commons.codec.digest.DigestUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SpiderEventPipeline implements Pipeline {

    private final String[] keywords;
    private final Long eventId;
    private final Long siteRuleId;

    private final SentimentSpiderResultService sentimentSpiderResultService;

    public SpiderEventPipeline(String[] keywords, Long eventId, Long siteRuleId, SentimentSpiderResultService sentimentSpiderResultService) {
        this.sentimentSpiderResultService = sentimentSpiderResultService;
        this.keywords = keywords;
        this.eventId = eventId;
        this.siteRuleId = siteRuleId;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        Map<String, Object> dataMap = resultItems.getAll();

        String url = resultItems.getRequest().getUrl();
        String id = DigestUtils.md5Hex(url.replace("http://", "").replace("https://", ""));

        String title = dataMap.getOrDefault("title", "").toString();
        String keywords = dataMap.getOrDefault("keywords", "").toString();
        String description = dataMap.getOrDefault("description", "").toString();
        String bodyText = dataMap.getOrDefault("bodyText", "").toString();
        String html = dataMap.getOrDefault("html", "").toString();
        List<String> listFragments = JSON.parseArray(dataMap.getOrDefault("listFragments", "[]").toString(), String.class);


        boolean flag = true;
        for (String keyword : this.keywords) {
            if (!bodyText.contains(keyword)) {
                flag = false;
                break;
            }
        }


        List<String> fragments = new ArrayList<>();
        for (String s : listFragments) {
            boolean isContains = false;
            for (String keyword : this.keywords) {
                if (s.contains(keyword)) {
                    isContains = true;
                    s = s.replace(keyword, "<em>" + keyword + "</em>");
                }
            }
            if (isContains) {
                fragments.add(s);
            }
        }


        if (flag) {
            SentimentSpiderResult result = new SentimentSpiderResult();
            result.setId(id);
            result.setEventId(eventId);
            result.setSiteRuleId(siteRuleId);
            result.setUrl(url);
            result.setTitle(title);
            result.setKeywords(keywords);
            result.setDescription(description);
            result.setFragments(fragments);
            result.setBodyText(bodyText);
            result.setHtml(html);
            result.setSpiderTime(DateUtil.current());
            result.setUpdateTime(DateUtil.current());
            this.sentimentSpiderResultService.saveOrUpdate(result);
        }
    }
}
