package org.jeecg.modules.spyderUtils.spider;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

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

@Component
public class TrendsSpyder {

    @Autowired
    private ReportSpyder reportSpyder;

    public SpyderReturn companyTrendsSpyderMessage(String url,String type) throws Exception {
        Document document = null;
        String baseUri = null;
        SpyderReturn spyderReturn = new SpyderReturn();
        if(type.equals("1")){
            document = Jsoup.parse(new URL(url), 10000);
            spyderReturn.setBaseUri(document.baseUri());
            spyderReturn.setDocument(document);
        }else{
            try {
                String regex = "^(https?://)?([\\w.-]+)\\.([a-zA-Z]{2,})(/|$)";
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(url);
                if (matcher.find()) {
                    String mainDomain = matcher.group(2) + "." + matcher.group(3);
                    String newUrl = (matcher.group(1) == null ? "http://" : matcher.group(1)) + mainDomain;
                    baseUri = newUrl+"/";
                    spyderReturn.setBaseUri(baseUri);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            String s = reportSpyder.companyReportMessage(url);
            if(!ObjectUtils.isEmpty(s)){
                document = Jsoup.parse(s);
                spyderReturn.setDocument(document);
            }
        }
        return spyderReturn;
    }

}
