package org.springblade.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class BaiduApiUtil {

    //百度服务端AK
    private static final String API_AK = "S2KnLxdhNEgwWNTdFdR7CwMkdRqNIHib";

    /**
     * 根据区域代码获取当天天气
     * @param districtId 区域代码
     */
    public static Map<String, String> getTodayWeather(String districtId){
        Map<String, String> map = new HashMap<>();
        String getUrl = "http://api.map.baidu.com/weather/v1/?data_type=all&district_id=" + districtId + "&ak=" + API_AK;
        try{
            URL url = new URL(getUrl);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
            String res;
            StringBuilder sb = new StringBuilder();
            while ((res = in.readLine()) != null) {
                sb.append(res.trim());
            }
            log.info("获取当天天气 ======= sb.toString():" + sb.toString());
            JSONObject jsonData = JSONObject.parseObject(sb.toString());
            JSONObject result = (JSONObject) jsonData.get("result");
            JSONObject now = (JSONObject) result.get("now");
            JSONArray forecasts = (JSONArray) result.get("forecasts");
            JSONObject forecast = (JSONObject)forecasts.get(0);
            map.put("temp", now.get("temp").toString());
            map.put("weather", forecast.get("text_day").toString());
            map.put("high", forecast.get("high").toString());
            map.put("low", forecast.get("low").toString());
            map.put("windLevel",now.get("wind_class").toString());
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
        return map;
    }

    /**
     * 根据项目位置获取经纬度
     */
    public static Map<String, Object> geocoding(String address)
            throws IOException {
        Map<String, Object> map = new HashMap<>();
        BufferedReader in = null;
        String getUrl = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + API_AK;
        URL url = new URL(getUrl);
        in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
        String res;
        StringBuilder sb = new StringBuilder("");
        while ((res = in.readLine()) != null) {
            sb.append(res.trim());
        }
        JSONObject jsonData = JSONObject.parseObject(sb.toString());
        JSONObject result = (JSONObject) jsonData.get("result");
        JSONObject location = (JSONObject) result.get("location");
        map.put("lat", location.get("lat").toString());
        map.put("lng", location.get("lng").toString());
        return map;
    }

    /**
     * 根据经纬度坐标解析地址详情
     */
    public static Map<String, String> getPosition(String LatitudeAndLongitude)
            throws IOException {
        Map<String, String> map = new HashMap<>();
        String getUrl = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=" + API_AK + "&output=json&coordtype=wgs84ll&location=" + LatitudeAndLongitude;
        URL url = new URL(getUrl);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
        String res;
        StringBuilder sb = new StringBuilder("");
        while ((res = in.readLine()) != null) {
            sb.append(res.trim());
        }
        JSONObject jsonData = JSONObject.parseObject(sb.toString());
        JSONObject result = (JSONObject) jsonData.get("result");
        JSONObject addressComponent = (JSONObject) result.get("addressComponent");
        map.put("province", addressComponent.get("province").toString());
        map.put("city", addressComponent.get("city").toString());
        map.put("district", addressComponent.get("district").toString());
        map.put("adcode", addressComponent.get("adcode").toString());
        return map;
    }

}
