package org.well.well.core.util;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class PointConvertUtility {
    private static final Logger logger = LoggerFactory.getLogger(PointConvertUtility.class);
    private static final String baiduUrl= "http://api.map.baidu.com/geoconv/v1/";
    private static final String baiduAk="QL9aqGNjgcRAGkt6F0BrsVmfGGOgjVyr";

    private static final String gaodeUrl="https://restapi.amap.com/v3/assistant/coordinate/convert";
    private static final String gaodeKey="ab182d27663bbbaac890730b55db2754";


    public static Map<String,Object> convertBaiduAPI(String lng, String lat){
        Map<String,Object> result = new HashMap<String,Object>();
        String coords = lng+","+lat;
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("coords",coords);
            params.put("ak",baiduAk);
            params.put("from",1);
            params.put("to",5);
            String str = HttpClientUtils.post(baiduUrl,
                    new GsonBuilder().enableComplexMapKeySerialization().
                            create().toJson(params));
            JSONObject jsStr = JSONObject.parseObject(str);
            if(jsStr.getInteger("status")==0){
                JSONObject coordinate = (JSONObject)jsStr.getJSONArray("result").get(0);
                double x = coordinate.getDouble("x");
                double y = coordinate.getDouble("y");
                result.put("x",x);
                result.put("y",y);
                return result;
            }else{
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String,Object> convertGaodeAPI(String lng, String lat){
        Map<String,Object> result = new HashMap<String,Object>();
        String coords = lng+","+lat;
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("locations",coords);
            params.put("key",gaodeKey);
            params.put("coordsys","gps");
            String str = HttpClientUtils.post(gaodeUrl,
                    new GsonBuilder().enableComplexMapKeySerialization().
                            create().toJson(params));
            JSONObject jsStr = JSONObject.parseObject(str);
            if(jsStr.getInteger("status")==1){
                String coordinate = jsStr.getString("locations");
                String[] xy = coordinate.split(",");
                if(xy!=null && xy.length >= 2){
                    result.put("x",xy[0]);
                    result.put("y",xy[1]);
                }

                return result;
            }else{
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static void convertCoordinate(BusWellInfo busWellInfo){
//        if(ToolUtil.isNotEmpty(busWellInfo.getCoordinateX()) && ToolUtil.isNotEmpty(busWellInfo.getCoordinateY())){
//            Map<String,Object> baiduCoordinate = PointConvertUtility.convertBaiduAPI(busWellInfo.getCoordinateX(),busWellInfo.getCoordinateY());
//            if(ToolUtil.isNotEmpty(baiduCoordinate)) {
//                busWellInfo.setLngBaidu(baiduCoordinate.get("x").toString());
//                busWellInfo.setLatBaidu(baiduCoordinate.get("y").toString());
//            }else{
//                logger.error("{}百度坐标转换失败",busWellInfo.getWellCode());
//            }
//
//            Map<String,Object> gaodeCoordinate = PointConvertUtility.convertGaodeAPI(busWellInfo.getCoordinateX(),busWellInfo.getCoordinateY());
//            if(ToolUtil.isNotEmpty(gaodeCoordinate)) {
//                busWellInfo.setLngGaode(gaodeCoordinate.get("x").toString());
//                busWellInfo.setLatGaode(gaodeCoordinate.get("y").toString());
//            }else{
//                logger.error("{}高德坐标转换失败",busWellInfo.getWellCode());
//            }
//        }
//    }
}
