package com.ruoyi;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
//        String wrongWord = "蒙古自治区";
//        String content = "中华人民共和国内蒙古自治区呼和浩特市"; // 在 "蒙古自治区" 前后添加空格

/*        String content = "统修复、综合治理具有里程碑式的重要意义。项目总投资<span>4.43</span>"; // 在 "蒙古自治区" 前后添加空格
        String wrongWord = "疫情爆发";
        List<String> strings = segmentText(content);
        List<String> strings2 = segmentText(wrongWord);
        for(String word:strings){
            if (!wrongWord.contains(word)) {
                System.out.println(1);
            }
        }*/

//        // 打印长度和内容
//        System.out.println("wrongWord: [" + wrongWord + "], 长度: " + wrongWord.length());
//        System.out.println("content: [" + content + "], 长度: " + content.length());
//
//        // 构建正则表达式
//
////        String regex = "\\b" + Pattern.quote(wrongWord) + "\\b";
//        String regex = Pattern.quote(wrongWord) ;
//        System.out.println("regex: " + regex);
//
//        // 编译和匹配
//        Pattern pattern = Pattern.compile(regex);
//        Matcher matcher = pattern.matcher(content);
//
//        // 查找匹配
//        if (matcher.find()) {
//            System.out.println("找到匹配的单词: " + matcher.group());
//        } else {
//            System.out.println("未找到匹配的单词");
//        }

        HttpURLConnection connection = null;
        try {
            String url = "http://www.chifeng.gov.cn/zwgk/xxgkzl/zfxxgknb/2024zfxxgknb/szf_750/202503/t20250306_2545851.html";
            URL parsedUrl = new URL(url);

            // Configure to ignore SSL verification for HTTPS URLs
            if (parsedUrl.getProtocol().equalsIgnoreCase("https")) {
                // Create a trust manager that does not validate certificate chains
                TrustManager[] trustAllCerts = new TrustManager[]{
                        new X509TrustManager() {
                            public X509Certificate[] getAcceptedIssuers() {
                                return new X509Certificate[0];
                            }
                            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                            }
                            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                            }
                        }
                };

                // Install the all-trusting trust manager
                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

                // Create all-trusting host name verifier
                HostnameVerifier allHostsValid = (hostname, session) -> true;
                HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
            }

            connection = (HttpURLConnection) parsedUrl.openConnection();
            connection.setRequestMethod("HEAD"); // Use HEAD method to only get headers
            connection.setConnectTimeout(60000); // Set connection timeout
            connection.setReadTimeout(60000); // Set read timeout
            connection.setInstanceFollowRedirects(true); // Follow redirects

            int responseCode = connection.getResponseCode();
            System.out.println(responseCode == HttpURLConnection.HTTP_NOT_FOUND || responseCode >= 400);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        }

    }

    public static  List<String> segmentText(String text) {
        List<Term> termList = HanLP.segment(text);
        List<String> words = new ArrayList<>();
        for (Term term : termList) {
            words.add(term.word);
        }
        return words;
    }
}