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

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsoupUtilsU {

    public Document setUrl(Document document , String baseUri){
        Elements tags = document.select("link[href],img[src],*[style*=background-image],script[src],td[background],a[href],iframe[src]");
        try {
            for (Element tag : tags) {
                String attr = tag.attr("a");
                if (attr == null || attr.isEmpty()) continue;
                if (!attr.startsWith("http") && !attr.startsWith("data")) {
                    String absUrl = new URI(baseUri).resolve(attr).toString();
                    absUrl = new URI(absUrl).normalize().toString();
                    tag.attr("href", absUrl);
                }
            }
        }catch (Exception e){
        }
        try {
            for (Element tag : tags) {
                String attr = tag.attr("href");
                if (attr == null || attr.isEmpty()) continue;
                if (!attr.startsWith("http") && !attr.startsWith("data")) {
                    String absUrl = new URI(baseUri).resolve(attr).toString();
                    absUrl = new URI(absUrl).normalize().toString();
                    tag.attr("href", absUrl);
                }
            }
        }catch (Exception e){
        }
        try {
            for (Element tag : tags) {
                String attr = tag.attr("src");
                if (attr == null || attr.isEmpty()) continue;
                if (!attr.startsWith("http") && !attr.startsWith("data")) {
                    String absUrl = new URI(baseUri).resolve(attr).toString();
                    absUrl = new URI(absUrl).normalize().toString();
                    tag.attr("src", absUrl);
                }
            }
        }catch (Exception e){
        }
        try {
            for (Element tag : tags) {
                String attr = tag.attr("background");
                if (attr == null || attr.isEmpty()) continue;
                if (!attr.startsWith("http") && !attr.startsWith("data")) {
                    String absUrl = new URI(baseUri).resolve(attr).toString();
                    absUrl = new URI(absUrl).normalize().toString();
                    tag.attr("background", absUrl);
                }
            }
        }catch (Exception e){
        }
        try {
            for (Element tag : tags) {
                try {
                    String attr = tag.attr("iframe");
                    if (attr == null || attr.isEmpty()) continue;
                    if (!attr.startsWith("http") && !attr.startsWith("data")) {
                        String absUrl = new URI(baseUri).resolve(attr).toString();
                        absUrl = new URI(absUrl).normalize().toString();
                        tag.attr("iframe", absUrl);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
        }
        try {
            for (Element tag : tags) {
                try {
                    String attr = tag.attr("iframe");
                    if (attr == null || attr.isEmpty()) continue;
                    if (!attr.startsWith("http") && !attr.startsWith("data")) {
                        String absUrl = new URI(baseUri).resolve(attr).toString();
                        absUrl = new URI(absUrl).normalize().toString();
                        tag.attr("src", absUrl);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
        }
        try {
            for (Element tag : tags) {
                try {
                    String attr = tag.attr("img");
                    if (attr == null || attr.isEmpty()) continue;
                    if (!attr.startsWith("http") && !attr.startsWith("data")) {
                        String absUrl = new URI(baseUri).resolve(attr).toString();
                        absUrl = new URI(absUrl).normalize().toString();
                        tag.attr("src", absUrl);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
        }
        System.out.println(document);
        return document;
    }

    public String getBaseUrl(String url){
        String regex = "^(https?://)?([\\w.-]+)\\.([a-zA-Z]{2,})(/|$)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);
        String baseUri = "";
        if (matcher.find()) {
            String mainDomain = matcher.group(2) + "." + matcher.group(3);
            String newUrl = (matcher.group(1) == null ? "http://" : matcher.group(1)) + mainDomain;
            baseUri = newUrl+"/";
        }
        return baseUri;
    }
}
