package com.sifang.modules.customer.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

public class GaodeUtil {

    static String key = "91a9d9a4a3f682821918cf37c0e32f2d"; //在高德地图控制台我的应用添加应用获取key


    /**
     * 获取两点间距离，单位米
     *
     * @param startLonLat
     * @param endLonLat
     * @return
     */
    public static Long getDistance(String startLonLat, String endLonLat) {
        //返回起始地startAddr与目的地endAddr之间的距离，单位：米
        Long result = 0L;
        String queryUrl = "http://restapi.amap.com/v3/distance?key=" + key + "&origins=" + startLonLat + "&destination=" + endLonLat;
        String queryResult = getResponse(queryUrl);
        JSONObject jo = JSONObject.parseObject(queryResult);
        JSONArray ja = jo.getJSONArray("results");
        Object obj = JSONObject.parseObject(ja.getString(0)).get("distance");
        if (obj == null) {
            return result;
        }
        result = Long.parseLong(obj.toString());
        return result;
    }

    /**
     * 地理编码，根据地址获取经纬度
     *
     * @param address
     * @return
     */
    public static String getLonLat(String address) {
        if(StringUtil.isBlank(address)){
            return "";
        }

        String lonLat = "";

        // 高德地图地理编码API
        String queryUrl = String.format("https://restapi.amap.com/v3/geocode/geo?address=" + address + "&output=json&key=" + key);
        try{
            String queryResult = getResponse(queryUrl);
            JSONObject jo = JSONObject.parseObject(queryResult);
            JSONArray ja = jo.getJSONArray("geocodes");
            Object obj = JSONObject.parseObject(ja.getString(0)).get("location");
            if (obj == null) {
                return lonLat;
            }
            lonLat = obj.toString();
            return lonLat;
        }catch (Exception e){
            e.printStackTrace();
            return lonLat;
        }
    }



    /**
     * 逆地理编码，根据经纬度获取地址
     *
     * @param longitude
     * @return
     */
    public static String getAddress(String longitude) {

        String address = "";

        // 高德地图地理编码API
        String queryUrl = String.format("https://restapi.amap.com/v3/geocode/regeo?output=json&key=" + key + "&radius=1000&extensions=base&location=%s", longitude);

        String queryResult = getResponse(queryUrl);
        JSONObject jo = JSONObject.parseObject(queryResult);
        JSONObject ja = jo.getJSONObject("regeocode");
        String obj = ja.getString("formatted_address");
        if (obj == null) {
            return address;
        }
        address = obj;
        return address;
    }



    /**
     * 用JAVA发起http请求，并返回json格式的结果
     * @param serverUrl
     * @return
     */
    private static String getResponse(String serverUrl) {
        //用JAVA发起http请求，并返回json格式的结果
        StringBuffer result = new StringBuffer();
        try {
            URL url = new URL(serverUrl);
            URLConnection conn = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            in.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    public static void main(String[] args) {


        //获取地址1的经纬度
      /*  String lonlat = getAddress("118.391734,31.369373");
        System.out.println(lonlat);*/

        //获取地址2的经纬度
         String lonlat1 = getLonLat("安徽省芜湖市鸠江区湖龙街道胜滨湖畔小区B9单元401");
        System.out.println(lonlat1);

  /*      //逆地理编码
        double longitude = Double.valueOf(lonlat.substring(0,lonlat.indexOf(",")));
        double latitude = Double.valueOf(lonlat.substring(lonlat.indexOf(",")+1,lonlat.length()));

        String gaodeResult = getAddress(longitude, latitude);

        System.out.println(gaodeResult);

        //比较两地间距离
        String startLonLat = lonlat;
        String endLonLat = lonlat1;

        Long dis = getDistance(startLonLat, endLonLat);
        System.out.println(dis);*/



    }

}
