package cn.superdesk.uniorder.common.rest.request;

import cn.superdesk.uniorder.common.rest.request.holder.RequestHolder;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: AOP相关的工具
 * @Author: linjinyu
 * @CreateDate: 2021/10/27 下午4:43
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/27 下午4:43
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class RequestUtil {

	public static String getParameters() {

		HttpServletRequest request = RequestHolder.getRequest();
		if (null == request) {
			return null;
		}

		Enumeration<String> paraNames = request.getParameterNames();
		if (paraNames == null) {
			return null;
		}

		StringBuilder sb = new StringBuilder();
		while (paraNames.hasMoreElements()) {
			String paraName = paraNames.nextElement();
			sb.append("&").append(paraName).append("=").append(request.getParameter(paraName));
		}

		return sb.toString();
	}

	public static Map<String, String[]> getParametersMap() {
		HttpServletRequest request = RequestHolder.getRequest();
		if (null == request) {
			return new HashMap<>();
		}
		return request.getParameterMap();
	}

	public static String getHeader(String headerName) {
		HttpServletRequest request = RequestHolder.getRequest();
		if (null == request) {
			return null;
		}
		return request.getHeader(headerName);
	}

	public static String getReferer() {
		return getHeader("Referer");
	}

	public static String getUa() {
		return getHeader("User-Agent");
	}

	public static String getIp() {
		HttpServletRequest request = RequestHolder.getRequest();
		if (null == request) {
			return null;
		}
		return IpUtils.getRealIp(request);
	}

	public static String getRequestUrl() {
		HttpServletRequest request = RequestHolder.getRequest();
		if (null == request) {
			return null;
		}
		return request.getRequestURL().toString();
	}

	public static String getMethod() {
		HttpServletRequest request = RequestHolder.getRequest();
		if (null == request) {
			return null;
		}
		return request.getMethod();
	}

	public static boolean isAjax(HttpServletRequest request) {
		if (null == request) {
			request = RequestHolder.getRequest();
		}
		if (null == request) {
			return false;
		}
		return "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))
				|| request.getParameter("ajax") != null;

	}


	/****************************************** 关于 http 读request body start ******************************************/

	public static JSONObject getRequestJsonObject(HttpServletRequest request) throws IOException {
		String json = getRequestJsonString(request);
		return JSONObject.parseObject(json);
	}

	/**
	 * 获取 request 中 json 字符串的内容
	 *
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static String getRequestJsonString(HttpServletRequest request) throws IOException {

		String submitMehtod = request.getMethod();

		// GET
		if (submitMehtod.equals("GET")) {
			return new String(request.getQueryString().getBytes("iso-8859-1"), "utf-8").replaceAll("%22", "\"");

		}
		// POST
		else {
			return getRequestPostStr(request);
		}
	}

	/**
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException {
		int contentLength = request.getContentLength();
		if (contentLength < 0) {
			return null;
		}
		byte buffer[] = new byte[contentLength];
		for (int i = 0; i < contentLength; ) {
			int readlen = request.getInputStream().read(buffer, i,
					contentLength - i);
			if (readlen == -1) {
				break;
			}
			i += readlen;
		}
		return buffer;
	}

	/**
	 * 获取 post 请求内容
	 *
	 * @param request
	 * @return
	 */
	public static String getRequestPostStr(HttpServletRequest request) {
		try {
			byte   buffer[]     = getRequestPostBytes(request);
			String charEncoding = request.getCharacterEncoding();
			if (charEncoding == null) {
				charEncoding = "UTF-8";
			}
			return new String(buffer, charEncoding);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


}
