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/";
    /*
     * 下载a标签对应的文件
     * */
    public String rexStringHtml(String html) throws Exception {
        Document doc = Jsoup.parse(html);
        try {
            Elements links = doc.select("a[href]");
            System.out.println("==============");
            System.out.println("=============="+links.text());
            for (Element link : links) {
                String text = link.attr("href");
                String name = updateFile(text);
                link.attr("href",name);
            }
            Elements linksrc = doc.select("img[src]");
            for (Element element : linksrc) {
                String textsrc = element.attr("src");
                String name = updateFile(textsrc);
                element.attr("src",name);
            }
            Elements linkimg = doc.select("IMG[src]");
            for (Element element : linkimg) {
                String textsrc = element.attr("src");
                String name = updateFile(textsrc);
                element.attr("src",name);
            }
            Elements linkvideo = doc.select("video[src]");
            for (Element element : linkvideo) {
                String shtml = element.toString();
                String[] split1 = html.split("<video");
                System.out.println("+++++++++++++++++++++++++++");
                String s1 = split1[0];
                System.out.println(s1);
                String s2 = split1[1];
                String s3 = s2.split("</video>")[1];
                System.out.println(s3);

                String textsrc = element.attr("src");
                try {
                    String[] splitMp4 = textsrc.split("id=");
                    String str = splitMp4[1];
                    String[] split = str.split("&type=");
                    String mp4 = split[0]+".mp4";
                    System.out.println("====================="+mp4);
                    String iframeHtml = "<p>\n" +
                            "<iframe frameborder=\"0\" src=\"/masvod/public/video/player.html?data-src=oldvideo/"+mp4+"\" width=\"640\" height=\"480\" style=\"width:640px;height:480px;margin:auto;text-align: center;display:block\"></iframe>\n" +
                            "</p><p>\n" +
                            "<br/>\n" +
                            "</p>";
                    html = s1 + iframeHtml + s3;
                    System.out.println("=============================new ");
                    System.out.println(html);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        Document parse = Jsoup.parse(html);
        try {
            Elements linkH = parse.select("a[href]");
            for (Element element : linkH) {
                String href = element.attr("href");
                int i = href.indexOf("down.asp");
                if(i >= 0){
                    continue;
                }
                int i1 = href.indexOf("_data/");
                if(i1 >=0){
                    continue;
                }
                int ia = href.indexOf("_Data/");
                if(ia >=0){
                    continue;
                }
                int ii = href.indexOf("/app-editor/");
                if(ii >=0){
                    continue;
                }

                int iiii = href.indexOf("_data/file");
                if(iiii >=0){
                    continue;
                }

                int iiiii = href.indexOf("/uploads/soft/");
                if(iiiii >=0){
                    continue;
                }
                int iiiiii = href.indexOf("common/preview_resource.action");
                if(iiiiii >=0){
                    continue;
                }
                String fileType = getFileTypeText(href);
                if(fileType != null && fileType != ""){
                    element.attr("appendix","true");
                    element.attr("needdownload","true");
                }
            }
            Elements linkImg = parse.select("img[src]");
            for (Element element : linkImg) {
                String src = element.attr("src");
                int i = src.indexOf("images/doc.gif");
                if(i >= 0){
                    continue;
                }
                String fileType = getFileTypeText(src);
                if(fileType != null && fileType != ""){
                    element.attr("needdownload","true");
                }
            }
            Elements linkHImg = parse.select("IMG[src]");
            for (Element element : linkHImg) {
                String src = element.attr("src");
                String fileType = getFileTypeText(src);
                if(fileType != null && fileType != ""){
                    element.attr("needdownload","true");
                }
            }
            Elements linkHvideo= parse.select("video[src]");
            for (Element element : linkHvideo) {
                String src = element.attr("src");
                String fileType = getFileTypeText(src);
                if(fileType != null && fileType != ""){
                    element.attr("needdownload","true");
                }
            }
        }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"
                ;
        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;
    }

 }
