package org.jeecg.sy.java;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

// 爬虫用的类
public class getData {

    @NotNull
    public static List<weather_1> getDatas(String time) throws MalformedURLException {
        String html = getHtml(new URL("https://www.tianqi24.com/haerbin/history" + time + ".html"));
        // 找到class=col7的ul标签
        String ul = html.substring(html.indexOf("class=\"col6\"") + 14);
        // 截断，并找到下一个ul标签
        ul = ul.substring(0, ul.indexOf("</ul>"));
        // 切割每个li标签，输出其中div的内容
        String[] lis = ul.split("<li>");
        List<weather_1> list = new ArrayList<>();
        for (int i = 2; i < lis.length; i++) {
            // 找到所有的div里的内容
            String[] li = lis[i].split("<div");
            // 循环输出
            weather_1 data = new weather_1();
            int j = 0;
            for (String str : li) {
                // 找到div>和</div之间的内容
                int start = str.indexOf(">");
                int end = str.indexOf("</div");
                if (start == -1 || end == -1)
                    continue;
                if (j == 0) {
                    // 时间为time前四位+
                    if (time.equals("")) {
                        time = "2024";
                    }
                    data.setData(time.substring(0, 4) + "-" + str.substring(start + 1, end));
                } else if (j == 1) {
                    String s = str.substring(start + 1, end);
                    //需要额外处理一下
                    //去除空格和&nbsp;和<span>和</span>和<b>和</b>
                    s = s.replaceAll("&nbsp;", "");
                    s = s.replaceAll(" ", "");
                    s = s.replaceAll("<span>", "");
                    s = s.replaceAll("</span>", "");
                    s = s.replaceAll("<b>", "");
                    s = s.replaceAll("</b>", "");
                    data.setDayOrNight(s);
                } else if (j == 2) {
                    data.setHighTemperature(str.substring(start + 1, end));
                } else if (j == 3) {
                    data.setLowTemperature(str.substring(start + 1, end));
                } else if (j == 4) {
                    data.setAir(str.substring(start + 1, end));
                } else if (j == 5) {
                    data.setWindSpeed(str.substring(start + 1, end));
                } else if (j == 6) {
                    data.setTotalRainfall(str.substring(start + 1, end));
                }
                j++;
            }
            list.add(data);
        }
        return list;
    }

    @NotNull
    public static List<weather_2> getDatas2() throws MalformedURLException {
        String html = getHtml(new URL("https://www.tianqishi.com/haerbin.html"));
        //  找到class="weather_fifteen_l-1"和</div>之间的内容
        String div = html.substring(html.indexOf("class=\"weather_fifteen_l-1\"") + 27);
        div = div.substring(0, div.indexOf("</div>"));
        List<weather_2> list = new ArrayList<>();
        // 按照<dl切割
        String[] dls = div.split("<dl>");
        for (int i = 1; i <= 7; i++) {
            // 找到所有的<dd>里的内容,输出第1，4，5个
            String[] dl = dls[i].split("<dd>");
            // 切除</dd>标签和空格
            String data = dl[1].replaceAll("</dd>", "").replaceAll(" ", "");
            //去掉空格后面的星期
            data = data.split("　")[0];
            String weather = dl[4].replaceAll("</dd>", "").replaceAll(" ", "");
            String temperature = dl[5].replaceAll("</dd>", "").replaceAll(" ", "");
            // 去除掉~之前的字符（只保留后面的温度）
            temperature = temperature.split("~")[1];
            temperature = temperature.replaceAll("℃", "");

            weather_2 weather_2 = new weather_2();
            weather_2.setData(data);
            weather_2.setWeather(weather);
            weather_2.setTemperature(temperature);
            list.add(weather_2);
        }
        return list;
    }

    // 获取网页
    public static String getHtml(URL url) {
        StringBuilder response = new StringBuilder();
        try {

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为GET
            connection.setRequestMethod("GET");

            // 设置请求头信息
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");

            // 获取响应码
            int responseCode = connection.getResponseCode();

            // 如果响应码为200，表示请求成功
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 获取响应流
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;

                // 读取响应内容
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
            } else {
                System.out.println("请求失败，响应码：" + responseCode);
            }

            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.toString();
    }
}
