package org.jeecg.modules.spyderUtils.spider;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.config.TenantContext;
import org.jeecg.modules.demo.externalinternalsite.service.IExternalInternalSiteService;
import org.jeecg.modules.demo.linkcaptureerrorlog.service.ILinkCaptureErrorLogService;
import org.jeecg.modules.demo.linkcaptureerrormessage.service.ILinkCaptureErrorMessageService;
import org.jeecg.modules.demo.linkerrorwordstatistics.service.ILinkErrorWordStatisticsService;
import org.jeecg.modules.demo.linkpublishingtimegrasping.entity.LinkPublishingTimeGrasping;
import org.jeecg.modules.demo.linkpublishingtimegrasping.service.ILinkPublishingTimeGraspingService;
import org.jeecg.modules.demo.linkpublishingtitle.entity.LinkPublishingTitle;
import org.jeecg.modules.demo.linkpublishingtitle.service.ILinkPublishingTitleService;
import org.jeecg.modules.demo.linkwhiteword.entity.LinkWhiteWord;
import org.jeecg.modules.demo.linkwhiteword.service.ILinkWhiteWordService;
import org.jeecg.modules.demo.mainsite.service.IMainSiteService;
import org.jeecg.modules.demo.pagecontentcapture.entity.PageContentCapture;
import org.jeecg.modules.demo.pagecontentcapture.service.IPageContentCaptureService;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class CrawlingUtils {

    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private ISysDictItemService iSysDictItemService;
    @Autowired
    private ILinkPublishingTimeGraspingService iLinkPublishingTimeGraspingService;
    @Autowired
    private ILinkPublishingTitleService linkPublishingTitleService;

    @Autowired
    private ILinkWhiteWordService iLinkWhiteWordService;


    public String getPublishingTitle(String main_site_url,Document document){
        //获取文章发布的标题
        QueryWrapper<LinkPublishingTitle> linkPublishingTitleQueryWrapper =new QueryWrapper<LinkPublishingTitle>();
        linkPublishingTitleQueryWrapper.eq("main_site_url",main_site_url);
        List<LinkPublishingTitle> linkPublishingTitles = linkPublishingTitleService.list(linkPublishingTitleQueryWrapper);
        String title = null;
        for (LinkPublishingTitle linkPublishingTitle : linkPublishingTitles) {
            String typeCssXpath = linkPublishingTitle.getTypeCssXpath();
            try {
                if(typeCssXpath.equals("selector")){
                    Elements select = document.select(linkPublishingTitle.getElementPositioningType());
                    String string = select.toString();
                    String text = Jsoup.parse(string).text();
                    if(text != null && text != ""){
                        title = text;
                    }
                }
                if(typeCssXpath.equals("xpath")){
                    // 5.将document转换成xpath能解析的文档树结构
                    JXDocument jdoc = new JXDocument(document.getAllElements()); // 此处可以使用xpath语法
                    // 6.使用xpath语法来解析html文档来完成数据清洗
                    List<JXNode> elNodes = jdoc.selN(linkPublishingTitle.getElementPositioningType());
                    for (JXNode elNode : elNodes) {
                        String string = elNode.toString();
                        String text = Jsoup.parse(string).text();
                        if(text != null && text != ""){
                            title = text;
                        }
                    }
                }
            }catch (Exception e){
            }
        }
        return title;
    }

    public String getPublishingTitle(String main_site_url,Document document,String  tenantId){
        //获取文章发布的标题
        QueryWrapper<LinkPublishingTitle> linkPublishingTitleQueryWrapper =new QueryWrapper<LinkPublishingTitle>();
        linkPublishingTitleQueryWrapper.eq("main_site_url",main_site_url);
        TenantContext.setTenant(tenantId);
        List<LinkPublishingTitle> linkPublishingTitles = linkPublishingTitleService.list(linkPublishingTitleQueryWrapper);
        String title = null;
        for (LinkPublishingTitle linkPublishingTitle : linkPublishingTitles) {
            String typeCssXpath = linkPublishingTitle.getTypeCssXpath();
            try {
                if(typeCssXpath.equals("selector")){
                    Elements select = document.select(linkPublishingTitle.getElementPositioningType());
                    String string = select.toString();
                    String text = Jsoup.parse(string).text();
                    if(text != null && text != ""){
                        title = text;
                    }
                }
                if(typeCssXpath.equals("xpath")){
                    // 5.将document转换成xpath能解析的文档树结构
                    JXDocument jdoc = new JXDocument(document.getAllElements()); // 此处可以使用xpath语法
                    // 6.使用xpath语法来解析html文档来完成数据清洗
                    List<JXNode> elNodes = jdoc.selN(linkPublishingTitle.getElementPositioningType());
                    for (JXNode elNode : elNodes) {
                        String string = elNode.toString();
                        String text = Jsoup.parse(string).text();
                        if(text != null && text != ""){
                            title = text;
                        }
                    }
                }
            }catch (Exception e){
            }
        }
        return title;
    }

    public Date getPublishingTimeGrasping(String main_site_url,Document document){
        QueryWrapper<LinkPublishingTimeGrasping> linkPublishingTimeGraspingQueryWrapper =new QueryWrapper<LinkPublishingTimeGrasping>();
        linkPublishingTimeGraspingQueryWrapper.eq("main_site_url",main_site_url);
        //获取文章发布的时间
        List<LinkPublishingTimeGrasping> linkPublishingTimeGraspings = iLinkPublishingTimeGraspingService.list(linkPublishingTimeGraspingQueryWrapper);
        Date parse = null;
        String time = null;
        for (LinkPublishingTimeGrasping linkPublishingTimeGrasping : linkPublishingTimeGraspings) {
            String typeCssXpathTime = linkPublishingTimeGrasping.getTypeCssXpath();
            try {
                if(typeCssXpathTime.equals("selector")){
                    Elements select = document.select(linkPublishingTimeGrasping.getElementPositioningType());
                    String string = select.toString();
                    String text = Jsoup.parse(string).text();
                    if(text != null && text != ""){
                        time = text;
                    }
                }
                if(typeCssXpathTime.equals("xpath")){
                    // 5.将document转换成xpath能解析的文档树结构
                    JXDocument jdoc = new JXDocument(document.getAllElements()); // 此处可以使用xpath语法
                    // 6.使用xpath语法来解析html文档来完成数据清洗
                    List<JXNode> elNodes = jdoc.selN(linkPublishingTimeGrasping.getElementPositioningType());
                    for (JXNode elNode : elNodes) {
                        String string = elNode.toString();
                        String text = Jsoup.parse(string).text();
                        if(text != null && text != ""){
                            time = text;
                        }
                    }
                }
            }catch (Exception e){
            }
        }
        System.out.println("================================="+time);
        //格式化时间
        if(time != null && time != "" && time.length() != 0){
            for (LinkPublishingTimeGrasping linkPublishingTimeGrasping : linkPublishingTimeGraspings) {
                String timeFormat = linkPublishingTimeGrasping.getTimeFormat();
                QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<SysDict>();
                sysDictQueryWrapper.eq("dict_code","time_type");
                List<SysDict> listSysDict = sysDictService.list(sysDictQueryWrapper);
                SysDict sysDict = null;
                if(listSysDict.size() >= 1){
                    sysDict = listSysDict.get(0);
                }
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<SysDictItem>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId());
                sysDictItemQueryWrapper.eq("item_value",timeFormat);
                List<SysDictItem> sysDictItems = iSysDictItemService.list(sysDictItemQueryWrapper);
                for (SysDictItem sysDictItem : sysDictItems) {
                    System.out.println(sysDictItem);
                    try {
                        String itemText = sysDictItem.getItemText();
                        //正则表达式
                        String description = sysDictItem.getDescription();
                        Pattern r = Pattern.compile(description);
                        Matcher m = r.matcher(time);
                        String dateStr = null;
                        if (m.find()) {
                            dateStr = m.group(0);
                        }
                        System.out.println(dateStr);
                        if(dateStr != null && dateStr != ""){
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(itemText);
                            parse = simpleDateFormat.parse(dateStr);
                            System.out.println(dateStr);
                            System.out.println(itemText);
                            System.out.println(parse);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }

        return parse;
    }
    public Date getPublishingTimeGrasping(String main_site_url,Document document,String  tenantId){
        QueryWrapper<LinkPublishingTimeGrasping> linkPublishingTimeGraspingQueryWrapper =new QueryWrapper<LinkPublishingTimeGrasping>();
        linkPublishingTimeGraspingQueryWrapper.eq("main_site_url",main_site_url);
        //获取文章发布的时间
        TenantContext.setTenant(tenantId);
        List<LinkPublishingTimeGrasping> linkPublishingTimeGraspings = iLinkPublishingTimeGraspingService.list(linkPublishingTimeGraspingQueryWrapper);
        Date parse = null;
        String time = null;
        for (LinkPublishingTimeGrasping linkPublishingTimeGrasping : linkPublishingTimeGraspings) {
            String typeCssXpathTime = linkPublishingTimeGrasping.getTypeCssXpath();
            try {
                if(typeCssXpathTime.equals("selector")){
                    Elements select = document.select(linkPublishingTimeGrasping.getElementPositioningType());
                    String string = select.toString();
                    String text = Jsoup.parse(string).text();
                    if(text != null && text != ""){
                        time = text;
                    }
                }
                if(typeCssXpathTime.equals("xpath")){
                    // 5.将document转换成xpath能解析的文档树结构
                    JXDocument jdoc = new JXDocument(document.getAllElements()); // 此处可以使用xpath语法
                    // 6.使用xpath语法来解析html文档来完成数据清洗
                    List<JXNode> elNodes = jdoc.selN(linkPublishingTimeGrasping.getElementPositioningType());
                    for (JXNode elNode : elNodes) {
                        String string = elNode.toString();
                        String text = Jsoup.parse(string).text();
                        if(text != null && text != ""){
                            time = text;
                        }
                    }
                }
            }catch (Exception e){
            }
        }
        System.out.println("================================="+time);
        //格式化时间
        if(time != null && time != "" && time.length() != 0){
            for (LinkPublishingTimeGrasping linkPublishingTimeGrasping : linkPublishingTimeGraspings) {
                String timeFormat = linkPublishingTimeGrasping.getTimeFormat();
                QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<SysDict>();
                sysDictQueryWrapper.eq("dict_code","time_type");
                List<SysDict> listSysDict = sysDictService.list(sysDictQueryWrapper);
                SysDict sysDict = null;
                if(listSysDict.size() >= 1){
                    sysDict = listSysDict.get(0);
                }
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<SysDictItem>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId());
                sysDictItemQueryWrapper.eq("item_value",timeFormat);
                List<SysDictItem> sysDictItems = iSysDictItemService.list(sysDictItemQueryWrapper);
                for (SysDictItem sysDictItem : sysDictItems) {
                    System.out.println(sysDictItem);
                    try {
                        String itemText = sysDictItem.getItemText();
                        //正则表达式
                        String description = sysDictItem.getDescription();
                        Pattern r = Pattern.compile(description);
                        Matcher m = r.matcher(time);
                        String dateStr = null;
                        if (m.find()) {
                            dateStr = m.group(0);
                        }
                        System.out.println(dateStr);
                        if(dateStr != null && dateStr != ""){
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(itemText);
                            parse = simpleDateFormat.parse(dateStr);
                            System.out.println(dateStr);
                            System.out.println(itemText);
                            System.out.println(parse);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }

        return parse;
    }
    public PageContentCapture JumpLinkWhiteWord(String ret_msg,String main_site_url,PageContentCapture pageContentCapture){
        //跳过白名单词汇表
        QueryWrapper<LinkWhiteWord> linkWhiteWordQueryWrapper = new QueryWrapper<LinkWhiteWord>();
        linkWhiteWordQueryWrapper.eq("main_site_url",main_site_url);
        List<LinkWhiteWord> linkWhiteWordList = iLinkWhiteWordService.list(linkWhiteWordQueryWrapper);
        for (LinkWhiteWord linkWhiteWord : linkWhiteWordList) {
            int index = ret_msg.indexOf(linkWhiteWord.getWord());
            if(index > 0){
                Document parseHtml = Jsoup.parse(ret_msg);
                Elements elements = parseHtml.select("span.proofreadSignClass.errorWordMark");
                for (Element element : elements) {
                    String text = element.text();
                    if(text.equals(linkWhiteWord.getWord())){
                        String string1 = parseHtml.toString();
                        String replace = string1.replace(element.toString(), linkWhiteWord.getWord());
                        pageContentCapture.setRetMsg(replace);
                        pageContentCapture.setErrorCount(0);
                    }
                }
            }
        }
        return pageContentCapture;
    }
    public PageContentCapture JumpLinkWhiteWord(String ret_msg,String main_site_url,PageContentCapture pageContentCapture,String  tenantId){
        //跳过白名单词汇表
        QueryWrapper<LinkWhiteWord> linkWhiteWordQueryWrapper = new QueryWrapper<LinkWhiteWord>();
        linkWhiteWordQueryWrapper.eq("main_site_url",main_site_url);
        TenantContext.setTenant(tenantId);
        List<LinkWhiteWord> linkWhiteWordList = iLinkWhiteWordService.list(linkWhiteWordQueryWrapper);
        for (LinkWhiteWord linkWhiteWord : linkWhiteWordList) {
            int index = ret_msg.indexOf(linkWhiteWord.getWord());
            if(index > 0){
                Document parseHtml = Jsoup.parse(ret_msg);
                Elements elements = parseHtml.select("span.proofreadSignClass.errorWordMark");
                for (Element element : elements) {
                    String text = element.text();
                    if(text.equals(linkWhiteWord.getWord())){
                        String string1 = parseHtml.toString();
                        String replace = string1.replace(element.toString(), linkWhiteWord.getWord());
                        pageContentCapture.setRetMsg(replace);
                        pageContentCapture.setErrorCount(0);
                    }
                }
            }
        }
        return pageContentCapture;
    }




}
