package com.yida.data.common.core.utils;


import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yida.data.common.core.exception.FebsException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LocationUtil {

    private static final String GAO_DE_TRANSLATE_URL = "http://apilocate.amap.com/position";
    private static final String GAO_DE_GECODE_URL = "https://restapi.amap.com/v3/geocode/regeo";
    private static final String GAO_DE_WEATHER_URL = "https://restapi.amap.com/v3/weather/weatherInfo";

    private static final String KEY = "4ffd32ffc1c5143860283ff130aa2db3";

    public static JSONObject translateBts(String mcc, String mnc, String lac, String cid, Integer signal) {
        JSONObject params = new JSONObject() {{
            set("key", KEY);
            set("accesstype", 0);
            set("bts", StrUtil.join(",", mcc, mnc, lac, cid, signal));
        }};
        String res = HttpUtil.get(GAO_DE_TRANSLATE_URL, params);
        JSONObject resJson = JSONUtil.parseObj(res);
        if (!resJson.getInt("status").equals(1)) {
            log.error("高德获取位置失败,params:[{}],res:[{}]", params, res);
            throw new FebsException("获取位置失败");
        }
        log.info("params:[{}],res:[{}]", params, resJson);
        return resJson.getJSONObject("result");
    }

    public static JSONObject translateWifimac(String macs) {
        JSONObject params = new JSONObject() {{
            set("key", KEY);
            set("accesstype", 1);
            set("macs", macs);
        }};
        String res = HttpUtil.get(GAO_DE_TRANSLATE_URL, params);
        JSONObject resJson = JSONUtil.parseObj(res);
        if (!resJson.getInt("status").equals(1)) {
            log.error("高德获取位置失败,params:[{}],res:[{}]", params, res);
            throw new FebsException("获取位置失败");
        }
        log.info("params:[{}],res:[{}]", params, resJson);
        return resJson.getJSONObject("result");
    }

    /**
     * 经纬度转换位置
     *
     * @return
     */
    public static String locationToName(Double lng, Double lat) {
        JSONObject params = new JSONObject() {{
            set("key", KEY);
            set("location", lng.toString() + "," + lat.toString());
        }};
        String res = HttpUtil.get(GAO_DE_GECODE_URL, params);
        JSONObject resJson = JSONUtil.parseObj(res);
        if (!resJson.getInt("status").equals(1)) {
            log.error("高德获取位置信息失败,params:[{}],res:[{}]", params, res);
            throw new FebsException("获取位置信息失败");
        }
        log.info("params:[{}],res:[{}]", params, resJson);
        return resJson.getJSONObject("regeocode").getStr("formatted_address");
    }

    public static JSONObject getWeather(String cityCode) {
        JSONObject params = new JSONObject() {{
            set("key", KEY);
            set("city", cityCode);
        }};
        String res = HttpUtil.get(GAO_DE_WEATHER_URL, params);
        JSONObject resJson = JSONUtil.parseObj(res);
        if (!resJson.getInt("status").equals(1)) {
            log.error("高德获取天气失败,params:[{}],res:[{}]", params, res);
            throw new FebsException("获取天气失败");
        }
        log.info("params:[{}],res:[{}]", params, resJson);
        return resJson.getJSONArray("lives").getJSONObject(0);
    }

    public static void main(String[] args) {
        JSONObject object = translateBts("460", "11", "39681", "165127698", 99);
        System.out.println("");
    }
}
