package com.trs.web2frame.tool.tyjyh.ggzyzheng;

import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class JsoupUtilsView {
    public static String filePath = "D:/ruoyi/updatePathggzy/";
    public static String filePathHHHT = "D:/ruoyi/updatePathhhht/";
    public static String filePathBaotou = "D:/ruoyi/updatePathBaotou/";
    public static String filePathEEDS = "D:/ruoyi/updatePathEEDS/";
    public static String filePathHLBE = "D:/ruoyi/updatePathHLBE/";
    public static String filePathXLGLM = "D:/ruoyi/updatePatXLGLM/";
    public static String filePathWHS = "D:/ruoyi/updatePatWHS/";
    public static String filePathCFS = "D:/ruoyi/updatePatCFS/";
    public static String filePathWLCB = "D:/ruoyi/updatePatWLCB/";
    public static String filePathTLS = "D:/ruoyi/updatePatTLS/";
    public static String filePathBynr = "D:/ruoyi/updatePatBynr/";
    public static String filePathALS = "D:/ruoyi/updatePatALS/";
    public static String filePathXAM = "D:/ruoyi/updatePatXAM/";
    /*
     * 下载a标签对应的文件
     * */
    public String rexStringHtml(String html,String filePath) throws Exception {
        Document parse = Jsoup.parse(html);
        try {
            Elements links = parse.select("a[href]");
            for (Element link : links) {
                try {
                    String text = link.attr("href");
                    int i = text.indexOf("http");
                    if(i >=0){
                        continue;
                    }
                    String name = updateFile(filePath+text);
                    link.attr("href",name);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            Elements linksrc = parse.select("img[src]");
            for (Element element : linksrc) {
               try {
                   String textsrc = element.attr("src");
                   String name = updateFile(filePath+textsrc);
                   element.attr("src",name);
               }catch (Exception e){

               }
            }
            Elements linkimg = parse.select("IMG[src]");
            for (Element element : linkimg) {
                try {
                    String textsrc = element.attr("src");
                    String name = updateFile(filePath+textsrc);
                    element.attr("src",name);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            Elements linkH = parse.select("a[href]");
            for (Element element : linkH) {
                try {
                    String href = element.attr("href");
                    int i = href.indexOf("http");
                    if(i >=0){
                        continue;
                    }
                    int ii = href.indexOf("file://C:");
                    if(ii >=0){
                        continue;
                    }
                    String fileType = getFileTypeText(href);
                    if(fileType != null && fileType != ""){
                        element.attr("appendix","true");
                        element.attr("needdownload","true");
                    }
                }catch (Exception ee){
                    ee.printStackTrace();
                }
            }
            Elements linkImg = parse.select("img[src]");
            for (Element element : linkImg) {
                try {
                    String src = element.attr("src");
                    String fileType = getFileTypeText(src);
                    if(fileType != null && fileType != ""){
                        element.attr("needdownload","true");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            Elements linkHImg = parse.select("IMG[src]");
            for (Element element : linkHImg) {
                try {
                    String src = element.attr("src");
                    String fileType = getFileTypeText(src);
                    if(fileType != null && fileType != ""){
                        element.attr("needdownload","true");
                    }
                }catch (Exception ee){
                    ee.printStackTrace();
                }
            }
            Elements linkHvideo= parse.select("video[src]");
            for (Element element : linkHvideo) {
                try {
                    String src = element.attr("src");
                    String fileType = getFileTypeText(src);
                    if(fileType != null && fileType != ""){
                        element.attr("needdownload","true");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return parse.toString();
    }

    private String getFileTypeText(String text) {
        String suffixes="avi,mpeg,3gp,mp3,mp4,wav,jpeg,gif,jpg,png,apk,exe,pdf,rar,zip,docx,doc,txt,xls,rar,ppt,XLS,RAR,PDF," +
                "AVI,MPEG,3GP,MP3,MP4,WAV,JPEG,GIF,JPG,PNG,APK,EXE,PDF,RAR,ZIP,DOCX,DOC,TXT,XLS,RAR,PPT,XLS,RAR,PDF,PPT,XLSX,xlsx,xlsx"
                ;
        String[] split = suffixes.split(",");
        String type = null;
        for (String st : split) {
            int i = text.indexOf(st);
            if(i >=0){
                type = st;
            }
        }
        return type;
    }

    public String updateFile(String url) throws Exception {
        System.out.println("==============");
        String fileNameRe = null;
        try {
            if(url != null && url != ""){
                System.out.println(url);
                Dispatch dispatch = WCMServiceCaller.UploadFile(url);
                System.out.println("-----------"+dispatch.getUploadShowName());
                fileNameRe = dispatch.getUploadShowName();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return fileNameRe;
    }

 }
