package com.usky.common.log.aspect;

import cn.hutool.http.HttpUtil;
import com.ruoyi.common.core.utils.ip.IpUtils;
import com.usky.common.log.service.WjConfig;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/*
 * 获取地址类
 *
 * @author xy
 */

/*
public class AddressUtils {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    // IP地址查询
    public static String IP_URL = "http://whois.pconline.com.cn/ip.jsp?ip=";

    // 未知地址
    public static final String UNKNOWN = "获取地理位置异常";

    // 检查响应是否是503 Service Temporarily Unavailable
    private static final String SERVICE_UNAVAILABLE_HTML = "503 Service Temporarily Unavailable";

    public static String getRealAddressByIP(String ip) {
        // 内网不查询
        if (IpUtils.internalIp(ip)) {
            return "内网IP";
        }
        // 检查地址服务是否启用
        if (WjConfig.isAddressEnabled()) {
            try {
                String url = IP_URL + ip;
                String rspStr = HttpUtil.get(url);
                log.info("ip is {}, repStr is {}, url is {}", ip, rspStr, url);

                // 检查响应是否为空或者503错误
                if (Strings.isNullOrEmpty(rspStr) || rspStr.contains(SERVICE_UNAVAILABLE_HTML)) {
                    log.error("获取地理位置异常 {}, {}", ip, rspStr);
                    return UNKNOWN;
                }
                // 假设返回的字符串是直接的地理位置信息
                return rspStr.trim();
            } catch (Exception e) {
                log.error("获取地理位置异常 {}", ip, e);
            }
        }
        return UNKNOWN;
    }
}
*/

public class AddressUtils {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    // 腾讯地图API URL
    public static String IP_URL = "https://apis.map.qq.com/ws/location/v1/ip";
    public static String API_KEY = "TSIBZ-NZSCU-H5SVQ-GUBLG-G6HFH-7LFSZ";

    // 未知地址
    public static final String UNKNOWN = "获取地理位置异常";

    public static String getRealAddressByIP(String ip) {
        // 内网不查询
        if (IpUtils.internalIp(ip)) {
            return "内网IP";
        }
        // 检查地址服务是否启用
        if (WjConfig.isAddressEnabled()) {
            try {
                // 构建请求URL
                String url = String.format("%s?ip=%s&key=%s", IP_URL, ip, API_KEY);
                String rspStr = HttpUtil.get(url); // 使用GET请求发送数据
                log.info("ip is {}, repStr is {}", ip, rspStr);

                // 检查响应是否为空
                if (rspStr == null || rspStr.isEmpty()) {
                    log.error("获取地理位置异常 {}", ip);
                    return UNKNOWN;
                }

                // 解析JSON响应
                JSONObject jsonObject = new JSONObject(rspStr);
                if (jsonObject.getInt("status") != 0) {
                    log.error("获取地理位置失败: {}", jsonObject.getString("message"));
                    return UNKNOWN;
                }

                // 提取地址信息
                JSONObject adInfo = jsonObject.getJSONObject("result").getJSONObject("ad_info");
                String nation = adInfo.getString("nation");
                String province = adInfo.getString("province");
                String city = adInfo.getString("city");
                String district = adInfo.optString("district", "");

                return String.format("%s%s%s%s", nation, province, city, district).trim();
            } catch (JSONException e) {
                log.error("JSON 解析异常", e);
                return UNKNOWN;
            } catch (Exception e) {
                log.error("获取地理位置异常 {}", ip, e);
                return UNKNOWN;
            }
        }
        return UNKNOWN;
    }
}
