package com.cool.store.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

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

/**
 * @author zhangchenbiao
 * @FileName: GeoMapUtil
 * @Description:
 * @date 2024-04-23 11:26
 */
public class GeoMapUtil {

    private static final String AMAP_API_URL = "https://restapi.amap.com/v3/geocode/regeo";
    private static final String API_KEY = "0102f13948f9fba9c979aacd72eb01f1";

    public static void main(String[] args) {
        AddressInfo addressInfo = reverseGeoCoding("30.41875", "120.2985");
        System.out.println(JSONObject.toJSONString(addressInfo));
        System.out.println(JSONObject.toJSONString(reverseGeoCoding("30.41", "120.29")));
    }

    public static AddressInfo reverseGeoCoding(String latitude, String longitude) {
        BufferedReader in = null;
        try {
            URL url = new URL(AMAP_API_URL + "?location=" + longitude + "," + latitude + "&output=json&key=" + API_KEY);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder content = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                JSONObject jsonObject = JSONObject.parseObject(content.toString());
                // 提取并构建AddressInfo对象
                AddressInfo addressInfo = extractAddressInfo(jsonObject.getJSONObject("regeocode"));
                return addressInfo;
            }
        } catch (IOException e) {
            System.out.println("Error occurred while making the request: " + e.getMessage());
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private static AddressInfo extractAddressInfo(JSONObject geoJson) {
        if (geoJson==null){
            return new AddressInfo();
        }
        JSONObject addressComponent = geoJson.getJSONObject("addressComponent");
        // 根据实际响应结构解析省市区街道信息，此处仅为示例
        String province = (String) addressComponent.get("province");
        String city = addressComponent.get("city") instanceof String?(String) addressComponent.get("city"):province;
        String district = (String) addressComponent.get("district");
        String township = (String) addressComponent.get("township");
        String address = geoJson.getString("formatted_address");
        return new AddressInfo(province, city, district, township, address);
    }

    @Data
    public static class AddressInfo {
        private String province;
        private String city;
        private String district;
        private String township;
        private String address;
        public AddressInfo(){
        }

        public AddressInfo(String province, String city, String district, String township, String address) {
            this.province = province;
            this.city = city;
            this.district = district;
            this.township = township;
            this.address = address;
        }
    }
}
