package cn.com.ctop.kuaishou.modules.graphql.job;

import cn.com.ctop.kuaishou.modules.graphql.entity.KuaishouCommentConfig;
import cn.com.ctop.kuaishou.modules.graphql.entity.KuaishouCommentStat;
import cn.com.ctop.kuaishou.modules.graphql.entity.KuaishouUserCookie;
import cn.com.ctop.kuaishou.modules.graphql.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class KuaishouCommentHourJob implements Job {
    @Autowired
    private IKuaishouCommentConfigService kuaishouCommentConfigService;
    @Autowired
    private IKuaishouCommentKeywordService kuaishouCommentKeywordService;
    @Autowired
    private IKuaishouCommentStatService kuaishouCommentStatService;
    @Autowired
    private IKuaishouUserCookieService kuaishouUserCookieService;
    @Autowired
    private IKuaishouWebInterfaceService kuaishouWebInterfaceService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        QueryWrapper<KuaishouCommentConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("auto_delete", 1);
        queryWrapper.eq("cycle", "HOUR");
        List<KuaishouCommentConfig> kuaishouCommentConfigList = kuaishouCommentConfigService.list(queryWrapper);
        if (kuaishouCommentConfigList != null && kuaishouCommentConfigList.size() > 0) {
            for (KuaishouCommentConfig config : kuaishouCommentConfigList) {
                QueryWrapper<KuaishouUserCookie> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ksid", config.getKsid());
                KuaishouUserCookie kuaishouUserCookie = kuaishouUserCookieService.getOne(queryWrapper1);

                try {
                    String pcursor = "";
                    do {
                        Map<String, Object> videoMap = kuaishouWebInterfaceService.videoList(kuaishouUserCookie.getUserId(), config.getKsid(), pcursor);
                        JsonNode videoNode = mapToJson(videoMap);
                        pcursor = videoNode.get("data").get("publicFeeds").get("pcursor").asText();
                        Iterator<JsonNode> list = videoNode.get("data").get("publicFeeds").get("list").iterator();
                        while (list.hasNext()) {
                            JsonNode node = list.next();
                            Integer commentCount = node.get("commentCount").asInt();
                            if (commentCount > 0) {
                                String photoId = node.get("photoId").asText();
                                String commentPcursor = "";
                                do {
                                    Map<String, Object> commentMap = kuaishouWebInterfaceService.commentList(kuaishouUserCookie.getUserId(), config.getKsid(), photoId, commentPcursor);
                                    JsonNode commentNode = mapToJson(commentMap);
                                    commentPcursor = commentNode.get("data").get("shortVideoCommentList").get("pcursor").asText();
                                    Iterator<JsonNode> commentList = commentNode.get("data").get("shortVideoCommentList").get("commentList").iterator();
                                    while (commentList.hasNext()) {
                                        JsonNode commentListNode = commentList.next();
                                        Long commentId = commentListNode.get("commentId").asLong();
                                        Map<String, Object> deleteMap = kuaishouWebInterfaceService.commentDelete(kuaishouUserCookie.getUserId(), config.getKsid(), photoId, commentId);
                                        if ((int) deleteMap.get("result") == 1) {
                                            KuaishouCommentStat kuaishouCommentStat = kuaishouCommentStatService.getById(commentId);
                                            if (kuaishouCommentStat == null) {
                                                kuaishouCommentStat = new KuaishouCommentStat();
                                                kuaishouCommentStat.setCommentId(commentId);
                                                kuaishouCommentStat.setDeleteCount(1);
                                                kuaishouCommentStat.setKsid(config.getKsid());
                                                kuaishouCommentStatService.save(kuaishouCommentStat);
                                            } else {
                                                kuaishouCommentStat.setDeleteCount(kuaishouCommentStat.getDeleteCount() + 1);
                                                kuaishouCommentStatService.updateById(kuaishouCommentStat);
                                            }
                                        }
                                    }
                                } while (commentPcursor != null && !"no_more".equals(commentPcursor));
                            }
                        }
                    } while (pcursor != null && !"no_more".equals(pcursor));

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public JsonNode mapToJson(Map<String, Object> map) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(map);
        return mapper.readTree(json);
    }
}
