package com.cslg.ppa.service.GetWebArticle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cslg.ppa.common.utils.DateUtil;
import com.cslg.ppa.common.utils.RegexUtil;
import com.cslg.ppa.dto.GetArticleInfoDTO;
import com.cslg.ppa.entity.ArticleInfo;
import com.cslg.ppa.entity.SourceInfo;
import com.cslg.ppa.mapper.SourceInfoMapper;
import com.cslg.ppa.service.ArticleInfoService;
import com.cslg.ppa.service.commom.DifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class GetCNIPRArticleService {
    private final ArticleInfoService articleInfoService;
    @Autowired
    private DifyService difyService;
    @Autowired
    private SourceInfoMapper sourceInfoMapper;

    private static final String LINK_SELECTOR = "a[href]";
    private static final String TITLE_SELECTOR = "a";

    /***
     * 中国知识产权网
     * @throws IOException
     */
    @Scheduled(cron = "0 0 4 * * ?")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public void crawCniprArticle() throws IOException {
        System.out.println(new Date() + "CNIPR-Begin");
        SourceInfo sourceInfo = sourceInfoMapper.selectOne(new LambdaQueryWrapper<SourceInfo>()
                .eq(SourceInfo::getWebType, 3));
        if (ObjectUtils.isNotEmpty(sourceInfo)) {
            String baseUrl = sourceInfo.getSourceUrl();
            Integer sourceInfoId = sourceInfo.getId();
            List<String> list = new ArrayList<>();
            try {
                Thread.sleep(1000);
                // 使用Jsoup连接并解析网页
                Document doc = Jsoup.connect(baseUrl)
                        .timeout(20000) // 增加超时时间
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                        .followRedirects(true) // 跟随重定向
                        .get();

                // 改进的新闻列表抓取逻辑
                Elements newsItems = doc.select(LINK_SELECTOR);
                for (Element item : newsItems) {
                    try {
                        Element titleElement = item.selectFirst(TITLE_SELECTOR);
                        Element linkElement = item.selectFirst(LINK_SELECTOR);

                        if (titleElement != null && linkElement != null) {
                            String title = titleElement.text().trim();
                            if (StringUtils.equals("通知公告", title) || StringUtils.equals("新闻资讯", title)) {
                                String link = linkElement.absUrl("href"); // 获取绝对URL
                                list.add(link);
                            }
                        }
                    } catch (Exception e) {
                        log.warn("解析单个新闻项时出错: ", e);
                    }
                }
            } catch (Exception ignored) {

            }

            if (!CollectionUtils.isEmpty(list)) {
                List<String> collect = list.stream().distinct().collect(Collectors.toList());
                for (String url : collect) {
                    try {
                        this.crawCniprArticleDetail(url, sourceInfoId, 6);
                    } catch (IOException ignored) {

                    }
                }
            }
        }
        System.out.println(new Date() + "CNIPR-End");
    }

    public void crawCniprArticleDetail(String baseUrl,Integer sourceId, Integer type) throws IOException {
        // 使用Jsoup连接并解析网页
        Document doc = Jsoup.connect(baseUrl)
                .timeout(20000) // 增加超时时间
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .followRedirects(true) // 跟随重定向
                .get();

        // 改进的新闻列表抓取逻辑
        Element container = doc.selectFirst("div.bd");
        Elements linkElements = container.select("li");
        List<GetArticleInfoDTO> articleInfoDTOS = new ArrayList<>();
        for (Element linkElement : linkElements) {
            Element element = linkElement.getElementsByTag("a").first();
            if (element == null) {
                continue;
            }
            String link = element.absUrl("href");
            // 如果absUrl没有返回有效链接，尝试其他方式
            if (StringUtils.isEmpty(link)) {
                link = element.attr("href");
            }
            if (link.startsWith("/")) {
                // 提取基础URL的协议和域名
                int endIndex = baseUrl.indexOf("/", 8); // 跳过http://或https://
                String baseDomain = endIndex > 0 ? baseUrl.substring(0, endIndex) : baseUrl;
                link = baseDomain + link;
            } else if (!link.startsWith("http")) {
                // 处理相对路径
                int lastSlash = baseUrl.lastIndexOf("/");
                String basePath = lastSlash > 0 ? baseUrl.substring(0, lastSlash + 1) : baseUrl + "/";
                link = basePath + link;
            }

            try {
                GetArticleInfoDTO articleInfoDTO = this.addCniprArticle(link, sourceId, type);
                if (ObjectUtils.isNotEmpty(articleInfoDTO)) {
                    articleInfoDTOS.add(articleInfoDTO);
                }
            } catch (Exception ignored) {
            }
        }
        articleInfoService.batchAddArticleInfo(articleInfoDTOS);
    }

    public GetArticleInfoDTO addCniprArticle(String baseUrl, Integer sourceId, Integer type) throws IOException {
        // 使用Jsoup连接并解析网页
        Document doc = Jsoup.connect(baseUrl)
                .timeout(15000) // 增加超时时间
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .followRedirects(true) // 跟随重定向
                .get();

        Element initElement = doc.selectFirst("div.zx_xqcont_cent");
        Element dateElement = initElement.selectFirst("div.type1_btm");
        String pubTime = dateElement.select("div.time").first().text();
        String dateStr = RegexUtil.extractDate(pubTime);
        String yesterdayDateStr = DateUtil.getYesterdayDateStr();
        if (!StringUtils.equals(dateStr, yesterdayDateStr)) {
            return null;
        }
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = dateFormat.parse(dateStr);
        } catch (Exception e) {
            return null;
        }

        Element titleElement = initElement.selectFirst("div.xq_cont_title");
        String title = titleElement.select("p").first().text();
        // 检查文章是否已存在
        long count = articleInfoService.count(new LambdaQueryWrapper<ArticleInfo>()
                .eq(ArticleInfo::getTitle, title));
        if (count > 0) {
            return null;
        }

        String digest = "";
        List<String> list = new ArrayList<>();
        Element element1 = doc.selectFirst("div.TRS_Editor");
        Elements elements = element1.select("p");
        for (Element element : elements) {
            String text = element.text();
            if (StringUtils.isNotEmpty(text)) {
                list.add(text);
            }
        }
        if (CollectionUtils.isEmpty(list)) {
            String articleContent = element1.text();
            list.add(articleContent);
            digest = StringUtils.join(list, "\n");
        } else {
            digest = StringUtils.join(list, "\n");
        }
        String condensedAbstract = "";
        try {
            condensedAbstract = difyService.getCondensedAbstract(digest);
        } catch (Exception e) {
            condensedAbstract = "";
        }
        GetArticleInfoDTO articleInfoDTO = new GetArticleInfoDTO();
        articleInfoDTO.setCategoryId(type);
        articleInfoDTO.setSourceId(sourceId);
        articleInfoDTO.setArticleUrl(baseUrl);
        articleInfoDTO.setTitle(title);
        articleInfoDTO.setPublicTime(date);
        articleInfoDTO.setDigest(condensedAbstract);
        return articleInfoDTO;
    }
}
