package com.ema.common.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 获取客户端IP地址浏览器等网络信息工具类
 */
public class NetworkUtil {

    public static HttpServletRequest getHttpServletRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.isNull(requestAttributes))
            return null;
        ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
        return attributes.getRequest();
    }

    public static String getClientIp(HttpServletRequest request) {
        if (Objects.isNull(request)) return "Unknown";

        // 从HttpServletRequest中获取客户端IP地址
        String ip = request.getHeader("X-Forwarded-For");
        if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取操作系统
     *
     * @return
     */
    public static String getOs(HttpServletRequest request) {
        String os = "Unknown";

        if (Objects.isNull(request)) return os;

        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null) {
            // 检测 Windows 系统
            if (userAgent.contains("Windows")) {
                os = "Windows";
            }
            // 检测 Mac OS
            else if (userAgent.contains("Macintosh")) {
                os = "Mac OS";
            }
            // 检测 Linux 系统
            else if (userAgent.contains("Linux")) {
                os = "Linux";
            }
            // 检测 Android 系统
            else if (userAgent.contains("Android")) {
                os = "Android";
            }
            // 检测 iOS 系统
            else if (userAgent.contains("iPhone") || userAgent.contains("iPad")) {
                os = "iOS";
            }
        }

        return os;
    }

    /**
     * 获取浏览器信息
     *
     * @return
     */
    public static String getBrowser(HttpServletRequest request) {

        String browser = "Unknown";
        if (Objects.isNull(request)) return browser;

        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null) {
            // 检测 Chrome 浏览器
            if (userAgent.contains("Chrome")) {
                browser = "Chrome";
            }
            // 检测 Firefox 浏览器
            else if (userAgent.contains("Firefox")) {
                browser = "Firefox";
            }
            // 检测 Safari 浏览器
            else if (userAgent.contains("Safari") && !userAgent.contains("Chrome")) {
                browser = "Safari";
            }
            // 检测 Internet Explorer
            else if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                browser = "Internet Explorer";
            }
            // 检测 Edge 浏览器
            else if (userAgent.contains("Edg")) {
                browser = "Edge";
            }
            // 其他浏览器可继续添加
        }

        return browser;
    }

}

