package org.jeecg.modules.spyderUtils.spider;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.config.TenantContext;
import org.jeecg.modules.demo.linkpersonalbankcardwhitelist.entity.LinkPersonalBankCardWhiteList;
import org.jeecg.modules.demo.linkpersonalbankcardwhitelist.service.ILinkPersonalBankCardWhiteListService;
import org.jeecg.modules.demo.linkpersonalidnumberwhitelist.entity.LinkPersonalIdNumberWhiteList;
import org.jeecg.modules.demo.linkpersonalidnumberwhitelist.service.ILinkPersonalIdNumberWhiteListService;
import org.jeecg.modules.demo.linkpersonalphonewhitelist.entity.LinkPersonalPhoneWhiteList;
import org.jeecg.modules.demo.linkpersonalphonewhitelist.service.ILinkPersonalPhoneWhiteListService;
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.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class JsoupUtilsService {

    String styleCss = "background: yellow;border-style: solid;border-color: red;border-width: 2px;padding: 5px;margin: 5px";

    @Autowired
    private ILinkPersonalBankCardWhiteListService iLinkPersonalBankCardWhiteListService;

    @Autowired
    private ILinkPersonalIdNumberWhiteListService iLinkPersonalIdNumberWhiteListService;

    @Autowired
    private ILinkPersonalPhoneWhiteListService iLinkPersonalPhoneWhiteListService;
    /**
     * 获取页面当中的的银行卡号，和标注号的HTML文本
     * @return String
     */
    public JsoupReturn getBankCardNumber(String html,String tenantId){
        JsoupReturn jsoupReturn = new JsoupReturn();
        //获取所有身份证号
        List<String> htmlList = new ArrayList<String>();
        List<String> aList = new ArrayList<String>();
        List<String> strList = new ArrayList<String>();
        Pattern pattern = Pattern.compile("\\b(62[0-9]{14})\\b");
        Matcher matcher = pattern.matcher(html);
        String rHtml = html;
        Integer index = 0;
        while (matcher.find()) {
            String cardNumber = matcher.group(1);
            if (!ObjectUtils.isEmpty(cardNumber)){
                QueryWrapper<LinkPersonalBankCardWhiteList> queryWrapper = new QueryWrapper<LinkPersonalBankCardWhiteList>();
                queryWrapper.eq("white_content",cardNumber);
                Page<LinkPersonalBankCardWhiteList> page = new Page<LinkPersonalBankCardWhiteList>(1, 1);
                TenantContext.setTenant(tenantId);
                IPage<LinkPersonalBankCardWhiteList> pageList = iLinkPersonalBankCardWhiteListService.page(page, queryWrapper);
                if(pageList.getRecords().size() > 0){
                    continue;
                }
                String cardNumberHtml =  "<span id='bankCard"+index+"' title='"+cardNumber+"' style='"+this.styleCss+"'>"+cardNumber+"</span>";
                String ahtml = "<a href='#bankCard"+index+"'>"+cardNumber+"</a>";
                rHtml = rHtml.replaceAll(cardNumber,cardNumberHtml);
                htmlList.add(cardNumberHtml);
                aList.add(ahtml);
                strList.add(cardNumber);
                index++;
            }
        }
        jsoupReturn.setHtml(rHtml);
        jsoupReturn.setHtmlList(htmlList);
        jsoupReturn.setAList(aList);
        jsoupReturn.setStrList(strList);
        return jsoupReturn;
    }
    /**
     * 获取页面当中的的身份证号，和标注号的HTML文本
     * @return String
     */
    public JsoupReturn getIdNumber(String html,String tenantId){
        JsoupReturn jsoupReturn = new JsoupReturn();
        //获取所有身份证号
        List<String> htmlList = new ArrayList<String>();
        List<String> aList = new ArrayList<String>();
        List<String> strList = new ArrayList<String>();
        String regex = "(?<![\\d\\w])\\d{17}[\\dX](?![\\d\\w])";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(html);
        String rHtml = html;
        Integer index = 0;
        log.info("==============获取页面当中的的身份证号，和标注号的HTML文本==============");
        while (matcher.find()) {
            String idNumber = matcher.group();
            if(!ObjectUtils.isEmpty(idNumber)){
                QueryWrapper<LinkPersonalIdNumberWhiteList> queryWrapper = new QueryWrapper<LinkPersonalIdNumberWhiteList>();
                queryWrapper.eq("white_content",idNumber);
                Page<LinkPersonalIdNumberWhiteList> page = new Page<LinkPersonalIdNumberWhiteList>(1, 1);
                TenantContext.setTenant(tenantId);
                IPage<LinkPersonalIdNumberWhiteList> pageList = iLinkPersonalIdNumberWhiteListService.page(page, queryWrapper);
                if(pageList.getRecords().size() > 0){
                    continue;
                }
                String idNumberHtml =  "<span id='idNumber"+index+"' title='"+idNumber+"' style='"+this.styleCss+"'>"+idNumber+"</span>";
                String ahtml = "<a href='#idNumber"+index+"'>"+idNumber+"</a>";
                rHtml = rHtml.replaceAll(idNumber,idNumberHtml);
                htmlList.add(idNumberHtml);
                aList.add(ahtml);
                strList.add(idNumber);
                log.info(idNumberHtml);
                log.info(ahtml);
                log.info(idNumber);
                index++;
            }

        }
        jsoupReturn.setHtml(rHtml);
        jsoupReturn.setHtmlList(htmlList);
        jsoupReturn.setAList(aList);
        jsoupReturn.setStrList(strList);
        return jsoupReturn;
    }
    /**
     * 获取页面当中的的电话号，和标注号的HTML文本
     * @return String
     */
    public JsoupReturn getPhone(String html,String tenantId){
        JsoupReturn jsoupReturn = new JsoupReturn();
        String regex = "\\b((\\+?86|\\(\\+86\\))?(1[3456789]\\d{9}|0[1-9][0-9]{1,2}-\\d{7,8}))\\b";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(html);
        //获取所有手机号和电话号
        List<String> htmlList = new ArrayList<String>();
        List<String> aList = new ArrayList<String>();
        List<String> strList = new ArrayList<String>();
        String rHtml = html;
        Integer index = 0;
        while (matcher.find()) {
            String groupPhone = matcher.group();
            if(!ObjectUtils.isEmpty(groupPhone)){
                QueryWrapper<LinkPersonalPhoneWhiteList> queryWrapper = new QueryWrapper<LinkPersonalPhoneWhiteList>();
                queryWrapper.eq("white_content",groupPhone);
                Page<LinkPersonalPhoneWhiteList> page = new Page<LinkPersonalPhoneWhiteList>(1, 1);
                TenantContext.setTenant(tenantId);
                IPage<LinkPersonalPhoneWhiteList> pageList = iLinkPersonalPhoneWhiteListService.page(page, queryWrapper);
                if(pageList.getRecords().size() > 0){
                    continue;
                }
                String groupPhoneHtml =  "<span id='phoneNumber"+index+"' title='"+groupPhone+"' style='"+this.styleCss+"'>"+groupPhone+"</span>";
                String ahtml = "<a href='#phoneNumber"+index+"'>"+groupPhone+"</a>";
                rHtml = rHtml.replaceAll(groupPhone,groupPhoneHtml);
                htmlList.add(groupPhoneHtml);
                aList.add(ahtml);
                strList.add(groupPhone);
                index++;
            }
        }
        jsoupReturn.setHtml(rHtml);
        jsoupReturn.setHtmlList(htmlList);
        jsoupReturn.setAList(aList);
        jsoupReturn.setStrList(strList);
        return jsoupReturn;
    }
    /**
     * 获取页面当中的所有链链，和标注号的HTML文本
     * @return String
     */
    public JsoupReturn getLink(String html,String tenantId){
        JsoupReturn jsoupReturn = new JsoupReturn();
        List<String> outerList = new ArrayList<String>();
        List<String> LinkList = new ArrayList<String>();
        Document doc = Jsoup.parse(html);
        Elements links = doc.select("a[href]");
        Elements images = doc.select("img[src]");
        Elements imageDataS = doc.select("img[data-src]");
        Elements videos = doc.select("video[src]");
        Elements videoDataS = doc.select("video[data-src]");
        Elements js = doc.select("script[src]");
        Elements css = doc.select("link[rel=stylesheet]");
        Elements iframes = doc.select("iframe[src]");
        Elements iframeDataS = doc.select("iframe[data-src]");
        for (Element link : links) {
            LinkList.add(link.toString());
            String href = link.attr("href");
            outerList.add(href);
        }
        for (Element image : images) {
            LinkList.add(image.toString());
            String src = image.attr("src");
            outerList.add(src);
        }
        for (Element imageData : imageDataS) {
            LinkList.add(imageData.toString());
            String dataSrc = imageData.attr("data-src");
            outerList.add(dataSrc);
        }
        for (Element video : videoDataS) {
            LinkList.add(video.toString());
            String src = video.attr("data-src");
            outerList.add(src);
        }
        for (Element video : videos) {
            LinkList.add(video.toString());
            String src = video.attr("src");
            outerList.add(src);
        }
        for (Element jss : js) {
            LinkList.add(jss.toString());
            String jssSrc = jss.attr("src");
            outerList.add(jssSrc);
        }
        for (Element csss : css) {
            LinkList.add(csss.toString());
            String rel = csss.attr("rel");
            String href = csss.attr("href");
            outerList.add(rel);
            outerList.add(href);
        }
        for (Element iframe : iframes) {
            LinkList.add(iframe.toString());
            String src = iframe.attr("src");
            outerList.add(src);
        }
        for (Element iframe : iframeDataS) {
            LinkList.add(iframe.toString());
            String src = iframe.attr("data-src");
            outerList.add(src);
        }
        for (String link : LinkList) {
            String linkStr = "<span title='"+link+"' style='"+this.styleCss+"'>"+link+"</span>";
            html = html.replaceAll(link,linkStr);
        }
        jsoupReturn.setStrList(outerList);
        jsoupReturn.setHtml(html);
        return jsoupReturn;
    }

    public JsoupReturn getOuterLink(String html,String baseLink,String tenantId){
        JsoupReturn jsoupReturn = new JsoupReturn();
        List<String> outerList = new ArrayList<String>();
        List<String> LinkList = new ArrayList<String>();
        Document doc = Jsoup.parse(html);
        Elements links = doc.select("a[href]");
        Elements images = doc.select("img[src]");
        Elements videos = doc.select("video[src]");
        Elements js = doc.select("script[src]");
        Elements css = doc.select("link[rel=stylesheet]");
        Elements iframes = doc.select("iframe[src]");
        for (Element link : links) {
            LinkList.add(link.toString());
            String href = link.attr("href");
            outerList.add(href);
        }
        for (Element image : images) {
            LinkList.add(image.toString());
            String src = image.attr("src");
            outerList.add(src);
        }
        for (Element video : videos) {
            LinkList.add(video.toString());
            String src = video.attr("src");
            outerList.add(src);
        }
        for (Element jss : js) {
            LinkList.add(jss.toString());
            String jssSrc = jss.attr("src");
            outerList.add(jssSrc);
        }
        for (Element csss : css) {
            LinkList.add(csss.toString());
            String rel = csss.attr("rel");
            String href = csss.attr("href");
            outerList.add(rel);
            outerList.add(href);
        }
        for (Element iframe : iframes) {
            LinkList.add(iframe.toString());
            String src = iframe.attr("src");
            outerList.add(src);
        }
        for (String link : LinkList) {
            String linkStr = "<span title='"+link+"' style='"+this.styleCss+"'>"+link+"</span>";
            html = html.replaceAll(link,linkStr);
        }
        jsoupReturn.setStrList(outerList);
        jsoupReturn.setHtml(html);
        return jsoupReturn;
    }


}
