package com.yunfeiyun.agmp.common.utils.weather;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@Slf4j
public class WeatherUtil {

    public static final String WEATHER_API = "http://open.nyzhwlw.com:10001/yf_weather";

    public static String getWeather(WeatherReqVo weatherReqVo)  {

        HttpURLConnection conn;
        try {
            URL url = new URL(WEATHER_API);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(JSON.toJSONBytes(weatherReqVo));
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            log.error("请求构造失败",e);
            return null;
        }
        try(InputStream in = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))){
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            return result.toString();
        }catch (Exception e){
            log.error("请求失败",e);
            return null;
        }finally {
            conn.disconnect();
        }
    }

    public static String getWeatherNearlySevenDays(WeatherReqVo weatherReqVo)  {
        return request(weatherReqVo);
    }

    public static String request(WeatherReqVo weatherReqVo)  {
        HttpURLConnection conn;
        try {
            URL url = new URL(WEATHER_API);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(JSON.toJSONBytes(weatherReqVo));
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            log.error("请求构造失败",e);
            return null;
        }
        try(InputStream in = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))){
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            return result.toString();
        }catch (Exception e){
            log.error("请求失败",e);
            return null;
        }finally {
            conn.disconnect();
        }
    }

    public static String appendRequestParam(Object... params){
        StringBuilder requestParam = new StringBuilder();

        for(Object o :params){
            if(null == o){
                continue;
            }
            Class<?> clazz = o.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                try {
                    field.setAccessible(true);
                    String propertyName = field.getName();
                    Object propertyValue = field.get(o);
                    if(null !=  propertyValue){
                        requestParam.append("&").append(propertyName).append("=").append(propertyValue);
                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
       ;
        return WEATHER_API+ requestParam.substring(1);
    }



}
