package com.sckw.core.web.request;

import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.util.Enumeration;
import java.util.Map;

/**
 * @desc 请求校验
 * @author zk
 * @date 2020/08/19
 */
public class RequestUtil {

    /**
     * 获取参数值
     * @param request 请求
     * @param key 键
     * @throws Exception
     */
    public static String getParam(HttpServletRequest request, String key) throws Exception{
        String value = request.getHeader(key);
        if (StringUtils.isBlank(value)){
            value = request.getParameter(key);
        }
        return value;
    }

    /**
     * @description 获取请求参数
     * @author zk
     * @date 2020/8/17 18:24
     * @param
     * @return
     **/
    public static Map<String, Object> getParams(HttpServletRequest request){
        Enumeration<?> pNames = request.getParameterNames();
        Map<String, Object> params = CollectionUtils.createHashMap();
        while (pNames.hasMoreElements()) {
            String pName = (String) pNames.nextElement();
            Object pValue = request.getParameter(pName);
            params.put(pName, pValue);
        }
        return params;
    }

    /**
     * @description 获取请求参数
     * @author zk
     * @date 2020/8/17 18:24
     * @param
     * @return

    public static Object getBodyParams(HttpServletRequest request){
        Object params;
        try {
            RequestWrapper requestWrapper = new RequestWrapper(request);
            String reqBody = requestWrapper.getBodyString(request);
            params = JSONObject.parse(reqBody);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return params;
    }**/

    /**
     * @Description 获取 Nginx代理后的真实ip
     * @author jiangwei
     * @date 2019/9/11 10:46
     */
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        String unKnown = "unKnown";
        if (StringUtils.isNotBlank(ip) && !unKnown.equalsIgnoreCase(ip)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if (index != -1) {
                return ip.substring(0, index);
            } else {

                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isNotBlank(ip) && !unKnown.equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }


    /**
     * 拦截器中，通过@Autowired注入失效，需要通过以下方式获取service对象
     * @param clazz  对象class类
     * @param request http request
     * @param <T>  对象类别
     * @return 返回一个对象
     */
    public static <T> T getObject(Class<T> clazz, HttpServletRequest request) {
        BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
        return factory.getBean(clazz);
    }
}
