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 lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
public class WeiboPipeline implements Pipeline {

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

    private final SentimentSpiderResultService sentimentSpiderResultService;

    public WeiboPipeline(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);
        List<String> listTitles = JSON.parseArray(dataMap.getOrDefault("listTitles", "[]").toString(), String.class);
        List<String> listIds = JSON.parseArray(dataMap.getOrDefault("listIds", "[]").toString(), String.class);
        List<String> listCreatedAts = JSON.parseArray(dataMap.getOrDefault("listCreatedAts", "[]").toString(), String.class);


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


        for (int index = 0; index < listFragments.size(); index++) {
            try {
                String fragment = listFragments.get(index);
                String emFragment = listFragments.get(index);
                String title = listTitles.get(index);
                String id = listIds.get(index);
                String createdAt = listCreatedAts.get(index);

                boolean isContains = false;
                for (String keyword : this.keywords) {
                    if (fragment.contains(keyword)) {
                        isContains = true;
                        emFragment = emFragment.replace(keyword, "<em>" + keyword + "</em>");
                    }
                }

                if (isContains) {
                    SentimentSpiderResult result = new SentimentSpiderResult();
                    result.setId(id);
                    result.setEventId(eventId);
                    result.setSiteRuleId(siteRuleId);
                    result.setUrl(String.format("https://m.weibo.cn/detail/%s", id));
                    result.setTitle(title);
                    result.setKeywords(fragment);
                    result.setDescription(fragment);
                    result.setFragments(Collections.singletonList(emFragment));
                    result.setBodyText(fragment);
                    result.setHtml(fragment);
                    result.setSpiderTime(DateUtil.current());

                    result.setUpdateTime(DateUtil.current());

                    this.sentimentSpiderResultService.saveOrUpdate(result);
                }
            } catch (Exception e) {
                log.error("", e);
            }
        }


    }
}
