package com.codvision.authcore.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
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.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 请求工具类
 *
 * @author lingee
 * @date 2023/7/18
 */
public class RequestUtil {

    public static final String ClientIp = "client-ip";

    public static String getIp() {
        final HttpServletRequest httpServletRequest = getRequest();
        if (httpServletRequest == null) {
            return null;
        }
        return getIp(httpServletRequest);
    }

    public static String getFromHead(String name) {
        if (StrUtil.isBlank(name)) {
            return null;
        }
        final HttpServletRequest httpServletRequest = getRequest();
        if (httpServletRequest == null) {
            return null;
        }
        return httpServletRequest.getHeader(name);
    }

    public static String getFromCookie(HttpServletRequest request, String cookieName) {
        Cookie cookie = ServletUtil.getCookie(request, cookieName);
        if (cookie != null) {
            return cookie.getValue();
        }
        return null;
    }

    private static String getIp(HttpServletRequest httpServletRequest) {
        String ip = httpServletRequest.getHeader(ClientIp);
        if (StrUtil.isBlank(ip)) {
            ip = ServletUtil.getClientIP(httpServletRequest);
        }
        return ip;
    }

    public static String getRequestPath() {
        final HttpServletRequest httpServletRequest = getRequest();
        if (httpServletRequest == null) {
            return null;
        }
        return httpServletRequest.getServletPath();
    }

    public static String getContextPath() {
        final HttpServletRequest request = getRequest();
        if (request == null) {
            return "/";
        }
        return request.getContextPath();
    }

    public static HttpServletRequest getRequest() {
        RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
        if (attrs == null) {
            return null;
        }
        final ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attrs;
        return servletRequestAttributes.getRequest();
    }

    private static String getStrFromRequest(final String key) {
        final Object v = getObject(key);
        if (v == null) {
            return null;
        }
        return Objects.toString(v);
    }

    public static <T> T getObject(final String key) {
        RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
        if (attrs == null) {
            return null;
        }
        Object obj = attrs.getAttribute(key, RequestAttributes.SCOPE_REQUEST);
        if (obj == null) {
            return null;
        }
        return (T) obj;
    }

    public static <T> void setObject(final HttpServletRequest request, final String key, final T obj) {
        request.setAttribute(key, obj);
    }

    public static boolean isExcludedUri(String uri, String excludedUris) {
        if (StringUtils.isBlank(uri) || StringUtils.isBlank(excludedUris)) {
            return false;
        }
        PathMatcher matcher = new AntPathMatcher();
        for (String ex : excludedUris.split(",")) {
            uri = uri.trim();
            ex = ex.trim();
            if (matcher.match(ex.toLowerCase(), uri.toLowerCase())) {
                return true;
            }
        }
        return false;
    }
}
