package com.casic.missiles.modular.system.util;

import com.alibaba.fastjson.JSONObject;
import com.casic.missiles.modular.config.SluicewellProperties;
import com.casic.missiles.modular.system.model.BusWellInfo;
import com.casic.missiles.core.util.SpringContextHolder;
import com.casic.missiles.core.util.ToolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


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

public class Utility {
    private static final Logger logger = LoggerFactory.getLogger(Utility.class);


    public static Map<String,Object> convertBaiduAPI(String lng, String lat){
        Map<String,Object> result = new HashMap<>();
        SluicewellProperties sluicewellProperties = SpringContextHolder.getBean(SluicewellProperties.class);
        String url = sluicewellProperties.getBaiduUrl();
        String ak = sluicewellProperties.getBaiduAk();
        String coords = lng+","+lat;
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("coords",coords);
            params.put("ak",ak);
            params.put("from",1);
            params.put("to",5);

            String str = HttpUtil.httpGet(url,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<>();
        SluicewellProperties sluicewellProperties = SpringContextHolder.getBean(SluicewellProperties.class);
        String url = sluicewellProperties.getGaodeUrl();
        String ak = sluicewellProperties.getGaodeKey();
        String coords = lng+","+lat;
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("locations",coords);
            params.put("key",ak);
            params.put("coordsys","gps");

            String str = HttpUtil.httpGet(url,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){
        logger.info("开始坐标转换！");
        if(ToolUtil.isNotEmpty(busWellInfo.getCoordinateX()) && ToolUtil.isNotEmpty(busWellInfo.getCoordinateY())){
            Map<String,Object> baiduCoordinate = Utility.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 = Utility.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());
            }
        }
    }
}
