package com.ho.common.tools.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ho.common.tools.entity.MyAddress;
import com.ho.common.tools.entity.WeatherRespVo;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 天气工具类--高德地图
 */
@Slf4j
public class WeatherUntils {

    static String secretKey = "d9b7b41dc870889b566118003fe426f7";

    /**
     * 获取天气
     * @param cityCode
     * @param extensions 可选值：base/all base:返回实况天气 all:返回预报天气
     * @return
     * @throws IOException
     */
    public static JSONObject getWeatherInfo(String cityCode,String extensions) throws IOException {
        String urlString = "https://restapi.amap.com/v3/weather/weatherInfo?city="+cityCode+"&key="+secretKey+"&extensions="+extensions;
        System.out.println("请求经纬度url:" + urlString);
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setRequestMethod("GET");
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        String line;
        String res = "";
        while ((line = in.readLine()) != null) {
            res += line + "\n";
        }
        in.close();
        JSONObject jsonObject = JSONObject.parseObject(res);
        return jsonObject;
    }

    /**
     * 根据adCode获取预测天气
     * @param adCode
     * @return
     */
    public static WeatherRespVo getWeatherRespVo(String adCode){
        WeatherRespVo weatherRespVo = null;
        try {
            JSONObject result = getWeatherInfo(adCode,"all");
            JSONArray forecasts = (JSONArray)result.get("forecasts");
            if(null != forecasts && forecasts.size()>0){
                JSONObject forecast = (JSONObject)forecasts.get(0);
                JSONArray casts = (JSONArray)forecast.get("casts");
                String city = String.valueOf(forecast.get("city"));
                if (null != casts && casts.size()>0){
                    JSONObject cast = (JSONObject)casts.get(0);
                    weatherRespVo = new WeatherRespVo();
                    weatherRespVo.setCityzh(city);
                    weatherRespVo.setMaxTemperature(new BigDecimal(String.valueOf(cast.get("daytemp"))));
                    weatherRespVo.setMinTemperature(new BigDecimal(String.valueOf(cast.get("nighttemp"))));
                    weatherRespVo.setSkyCon(String.valueOf(cast.get("dayweather")));
                    weatherRespVo.setSpeedAndDirection(String.valueOf(cast.get("daywind"))+String.valueOf(cast.get("daypower"))+"级");
                    weatherRespVo.setUpdateTime(System.currentTimeMillis());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("getAddress error:{}",e.getMessage());
        }
        return weatherRespVo;
    }

    public static void main(String[] args) throws IOException {
        WeatherRespVo weatherRespVo = getWeatherRespVo("320106");
        System.out.println(weatherRespVo);
    }


}
